Sendmail provides the option to send emails via the external SMTP servers. This tutorial will help you to configure Sendmail to relay email through external SMTP servers like Gmail, Amazon SES, Mailchimp, Mandrill etc. In this tutorial, we are using the Gmail SMTP server to relay emails from Sendmail server.
1. Create Auth File
First create directory for storing authentication files.
sudo mkdir /etc/mail/authinfo sudo chmod 700 /etc/mail/authinfo
Now, create an auth file with any name like smtp-auth and add the following content. Replace the above email with your gmail or google apps email.
sudo vi /etc/mail/authinfo/smtp-auth
AuthInfo: "U:root" "I:[email protected]" "P:PASSWORD"
Now create a hash map file of above created auth file.
makemap hash /etc/mail/authinfo/smtp-auth < /etc/mail/authinfo/smtp-auth
2. Configure Sendmail with SMART_HOST
Now add following configuration lines into your sendmail.mc configuration file below "MAILER" definition.
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/smtp-auth.db')dnl
In the next step we will need to re-build sendmail's configuration. To do that execute:
make -C /etc/mail
3. Verify Setup
After completing all the above configuration's, reload the sendmail service to apply changes.
sudo service sendmail reload
Now send a test email to verify everything is working fine.
echo "Test Email" | mail -s "Subject Here" [email protected]
10 Comments
Very nice guide, thanks! 🙂
P.S. Unrelated to the topic: The submit button on the comment form covers the Captcha, there are only few pixels available to click on it.
Cheers!
In CentOS 7, I needed of the packet cyrus-sasl-plain to send e-mails by relay auth.
I tried it as posted and got a permission denied with and without sudo. This is due to the redirection in the command string.
Step #1 part 4 should be slightly different
sudo /bin/su -c “makemap hash /etc/mail/authinfo/gmail-auth < /etc/mail/authinfo/gmail-auth"
found here: https://serverfault.com/questions/792675/sudo-makemap-permission-denied?noredirect=1&lq=1
Thanks Rahul, it worked beautifully!!!!
I tried this method for sending service letters from DB server through an already configured SMTP on another server. Everything works perfectly!
The only clarification on point 2: I had the following lines in the sendmail.mc file
MAILER_DEFINITIONS
MAILER (`local ‘) dnl
MAILER (`smtp ‘) dnl
So, the code snippet specified in the manual should be added after MAILER_DEFINITIONS, but BEFORE any MAILER (…, otherwise an error will occur: ERROR: FEATURE () should be before MAILER ()
can you please attach the sendmail.mc file? the post is a little confusing for the newbie.
Thanks
Noticed a small error – you request the user create:
$ sudo mkdir /etc/mail/auth
However, in step 3 you direct them to put the following in sendmail.mc:
FEATURE(`authinfo’,`hash -o /etc/mail/authINFO/smtp-auth.db’)dnl
This will cause an error. Thanks!
I will back this up. I needed to change too to work.
Hello Rahul,
I have followed the steps above and I have used Gmail as the sender. I tried the command (echo “Test Email” | mail -s “Subject Here” [email protected] ) and I get this response ‘You have new mail in /var/mail/appadmin’ but i did not receive any email in my inbox? Did I missed something?
Thank you
Have you changed the recipient address?