Sendmail is one of the oldest and most popular mail transfer agents (MTAs) available on Unix-based systems. It’s highly customizable, which makes it a preferred choice for many professionals, but this flexibility can also make it seem daunting. A key feature of Sendmail is its ability to relay emails through a remote SMTP server, which we’ll delve into here.
Relaying emails is a process where the email delivery is routed through different servers rather than going directly from the sender to the recipient. This practice can help optimize the email delivery process and overcome deliverability issues.
Prerequisites
Before we get started, ensure that you have the following:
- Access to a Unix-based system with Sendmail installed.
- Sudo or root privileges to modify the Sendmail configuration.
- Access to the remote SMTP server credentials, including the hostname, port, and authentication details.
Step-by-Step Guide
Step 1: Backup Your Existing Sendmail Configuration
Before making any changes to the Sendmail configuration, it’s a good practice to make a backup. This ensures you can restore your settings if anything goes wrong. To do this, use the following command:
sudo cp /etc/mail/sendmail.mc /etc/mail/sendmail.mc.bak
Step 2: Modify the Sendmail Configuration File
Open the Sendmail configuration file sendmail.mc with a text editor of your choice. Here, we’re using nano:
sudo nano /etc/mail/sendmail.mc
Scroll down and look for a line starting with dnl #. Immediately after this line, insert the following configuration, replacing smtp.yourprovider.com, user, and password with your SMTP server’s hostname, your username, and password respectively:
1 2 3 | define(`SMART_HOST',`[smtp.yourprovider.com]')dnl define(`confAUTH_MECHANISMS', `EXTERNAL GSSAPI DIGEST-MD5 CRAM-MD5 LOGIN PLAIN')dnl FEATURE(`authinfo',`hash -o /etc/mail/authinfo.db')dnl |
Save and exit the editor.
Step 3: Create the Authinfo File
Next, create the authinfo file that contains your SMTP server’s authentication information:
sudo nano /etc/mail/authinfo
Add the following line to the authinfo file, replacing the placeholders with your SMTP server’s hostname, username, and password:
1 | AuthInfo:smtp.yourprovider.com "U:user" "P:password" |
Save and exit the file.
Step 4: Update the Authinfo Database
After saving your authinfo file, update the authinfo.db file by running the following command:
sudo makemap hash /etc/mail/authinfo < /etc/mail/authinfo
Step 5: Update the Sendmail Configuration
Now, update the Sendmail configuration using the m4 macro processor:
sudo m4 /etc/mail/sendmail.mc > /etc/mail/sendmail.cf
Step 6: Restart Sendmail
Finally, apply the changes by restarting the Sendmail service:
sudo service sendmail restart
Testing Your Configuration
To confirm that Sendmail is correctly configured to relay emails, you can send a test email. Here's how you do it:
echo "Subject: Test Mail" | sendmail -v [email protected]
Replace [email protected] with your email address. You should receive the email shortly. Check the email headers to verify it was relayed through your SMTP server.
Conclusion
That's it! You've successfully configured Sendmail to relay emails through a remote SMTP server. Remember that this process might vary slightly depending on your system and the specific SMTP server you're using. If you encounter any issues, check your SMTP server's documentation and Sendmail logs for more information.
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?