Total Hit Counter

Friday, May 17, 2013

Configuring Gmail as a Sendmail email relay


Introduction


 In this configuration tutorial I will guide you through the process of configuring sendmail to be an email relay for your gmail or google apps account. This allows you to send email from your bash scripts, hosted website or from command line using mail command. Other examples where you can utilize this setting is for a notification purposes such or failed backups etc. Sendmail is just one of many utilities which can be configured to rely on gmail account where the others include postfix, exim , ssmpt etc. In this tutorial I will use Debian and sendmail for this task.

Install prerequisites

# apt-get install sendmail mailutils sendmail-bin 

Create Gmail Authentication file

# mkdir -m 700 /etc/mail/authinfo/
# cd /etc/mail/
next we need to create a auth file with a following content. File can have any name, in this example the name is gmail-auth:
AuthInfo: "U:root" "I:shankar.patel@gmail.com" "P:xxxxxxxxx"
Replace the above email with your gmail or google apps email.
Please note that in the above password example you need to keep 'P:' as it is not a part of the actual password.
In the next step we will need to create a hash map for the above authentication file:
# makemap hash gmail-auth < gmail-auth

Configure your sendmail 

Put bellow lines into your sendmail.mc configuration file right above first "MAILER" definition line:
define(`SMART_HOST',`[smtp.gmail.com]')dnl
define(`RELAY_MAILER_ARGS', `TCP $h 587')dnl
define(`ESMTP_MAILER_ARGS', `TCP $h 587')dnl
define(`confAUTH_OPTIONS', `A p')dnl
TRUST_AUTH_MECH(`EXTERNAL DIGEST-MD5 CRAM-MD5 LOGIN PLAIN')dnl
define(`confAUTH_MECHANISMS', `EXTERNAL GSSAPI DIGEST-MD5 CRAM-MD5 LOGIN PLAIN')dnl
FEATURE(`authinfo',`hash -o /etc/mail/gmail-auth.db')dnl
Do not put the above lines on the top of your sendmail.mc configuration file !
In the next step we will need to re-build sendmail's configuration. To do that execute:
# make -C /etc/mail
Reload sendmail service:
/etc/init.d/sendmail reload
and you are done.

Configuration test

Now you can send an email from your command line using mail command:
echo "Just testing my sendmail gmail relay" | mail -s "Sendmail gmail Relay" shankar.h.patel@yahoo.com


No comments: