To have emails from your linux servers accepted by destination email server you need to send it via trusted smarthost (relay). Google mail relay servers are what we need here. Let’s configure our sendmail for sending mail via gmail relay…
Preparation and installation
Sendmail installation:
1 |
yum -y install sendmail mailutils mailx sendmail-bin sendmail-cf cyrus-sasl-plain |
Now issue following command to create Gmail authentication file in a folder in which you will add Gmail user name and password.
1 2 |
mkdir -m 700 /etc/mail/authinfo/ cd /etc/mail/authinfo/ |
Next we need to create an authentication file with following contents. You can name it as you like. Just don’t forget to reflect your changes in sendmail’s config.
In this example I have configured it as gmail-idpass:
Create file:
1 |
nano gmail-idpass |
and add following:
1 |
AuthInfo: "U:root" "I:YOURGMAILID@GMAIL.COM" "P:YOURGMAILPASS" |
[Note: Replace the above with your gmail id and password]
Save and Exit.
In the next step we will need to create a hash map for the above authentication file:
1 |
makemap hash gmail-idpass < gmail-idpass |
Sendmail Configuration
Now add bellow lines into your /etc/mail/sendmail.mc configuration file. Make sure you add them at end, but right above first “MAILER” definition line:
Example your file may look like this before editing (last few lines)
1 2 3 4 5 6 7 8 |
dnl MASQUERADE_DOMAIN(localhost)dnl dnl MASQUERADE_DOMAIN(localhost.localdomain)dnl dnl MASQUERADE_DOMAIN(mydomainalias.com)dnl dnl MASQUERADE_DOMAIN(mydomain.lan)dnl MAILER(smtp)dnl MAILER(procmail)dnl dnl MAILER(cyrusv2)dnl |
You need to add the following lines above MAILER(smtp)dnl line
1 |
nano /etc/mail/sendmail.mc |
now paste following
1 2 3 4 5 6 7 8 9 |
# Adding config for gmail # 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-idpass.db')dnl # End config for gmail # |
Save and exit.
Now your sendmail.mc file will look a like as following after editing (last few lines)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
dnl MASQUERADE_DOMAIN(localhost)dnl dnl MASQUERADE_DOMAIN(localhost.localdomain)dnl dnl MASQUERADE_DOMAIN(mydomainalias.com)dnl dnl MASQUERADE_DOMAIN(mydomain.lan)dnl # Adding config for gmail # 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-idpass.db')dnl # End config for gmail # MAILER(smtp)dnl MAILER(procmail)dnl dnl MAILER(cyrusv2)dnl |
Now we need to re-build sendmail’s configuration. Run following command to do so.
1 |
make -C /etc/mail |
Reload sendmail service:
1 |
/etc/init.d/sendmail reload |
Now it’s time to test it!
Send email from CLI with sendmail
Now we can send email from a command line using mail command:
1 |
echo "Mail Body - Test Message from CENTOS Shell" | mail -s "Subject is test Mail Sending from CLI" -r yourgmailid to-address@example.com |
This will send email to to-address@example.com
We can track sending status in log:
1 |
tail -f /var/log/maillog |
you should look something like this:
1 2 3 4 5 6 7 8 9 10 |
Feb 3 14:34:36 ip-10-20-2-202 sendmail[3442]: u13EYare003442: from=asterisk, size=38130, class=0, nrcpts=1, msgid=<Asterisk-1-899475438-900-1823@ip-10-35-2- Feb 3 14:34:36 ip-10-20-2-202 postfix/smtpd[3443]: connect from localhost[127.0.0.1] Feb 3 14:34:36 ip-10-20-2-202 postfix/smtpd[3443]: A4BDD6CC78: client=localhost[127.0.0.1] Feb 3 14:34:36 ip-10-20-2-202 postfix/cleanup[3446]: A4BDD6CC78: message-id=<Asterisk-1-899475438-900-1823@ip-10-20-2-202> Feb 3 14:34:36 ip-10-20-2-202 postfix/qmgr[1333]: A4BDD6CC78: from=<asterisk@ip-10-35-2-202.eu-west-1.compute.internal>, size=39079, nrcpt=1 (queue active) Feb 3 14:34:36 ip-10-20-2-202 sendmail[3442]: u13EYare003442: to="itgala.xyz" <to-address@example.com>, ctladdr=asterisk (501/502), delay=00:00:00, xdelay=00:0=Sent (Ok: queued as A4BDD6CC78) Feb 3 14:34:36 ip-10-20-2-202 postfix/smtpd[3443]: disconnect from localhost[127.0.0.1] Feb 3 14:34:36 ip-10-20-2-202 postfix/smtp[3447]: connect to aspmx.l.google.com[2a00:1450:400b:c02::1b]:25: Network is unreachable Feb 3 14:34:37 ip-10-20-2-202 postfix/smtp[3447]: A4BDD6CC78: to=<to-address@example.com>, relay=aspmx.l.google.com[74.125.24.27]:25, delay=0.41, delays013750wmf.42 - gsmtp) Feb 3 14:34:37 ip-10-20-2-202 postfix/qmgr[1333]: A4BDD6CC78: removed |