In this article I will show how to configure gmail to be an email relay for your linux server with sendmail MTA. This allow you to send letters 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 not the only Mail Transport Agent that can be configured to use gmail account Postfix, exim , ssmpt etc. can be configured as well. The guide below was created on Debian.
Software Installation
1 |
# apt-get install sendmail mailutils sendmail-bin |
Create Gmail Authentication file
1 2 3 4 |
# mkdir -m 700 /etc/mail/authinfo/ # cd /etc/mail/authinfo/ # touch gmail-auth # vi gmail-auth |
You can use any text editor you like instead of vi. File can have any name, in this example the name is gmail-auth:
1 |
AuthInfo: "U:root" "I:youremail@gmail.com" "P:YOUR PASSWORD" |
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:
1 |
# makemap hash gmail-auth < gmail-auth |
Sendmail Configuration
Put lines bellow into your sendmail.mc configuration file right above first “MAILER” definition line:
1 2 3 4 5 6 7 |
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/authinfo/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:
1 |
# make -C /etc/mail |
Reload sendmail service:
1 |
/etc/init.d/sendmail reload |
and you are done.
Test Configuration
Now you can send an email from your command line using mail command:
1 |
$ echo "sendmail gmail relay test" | mail -s "Sendmail gmail Relay" someemail@domain.com |
If you will experience substitutional delays after executing the above command to most likely cause is that you have not configured your host with FQDN. Check your logs for a warning:
Sendmail: “unqualified hostname unknown; sleeping for retry unqualified hostname