Sendmail is an open-source mail transfer agent (MTA) that provides an efficient way to manage and transmit emails. However, for organizations that need to send a high volume of emails, relying solely on Sendmail may not be sufficient. This is where relaying emails through an external SMTP server can be helpful. This article provides a step-by-step guide to configuring Sendmail to relay emails through an external SMTP server.
Before You Begin
Before you begin the configuration process, you need to have the following information:
- The hostname or IP address of the external SMTP server
- The port number used by the external SMTP server (usually 25, 587 or 465)
- The authentication credentials required to log into the external SMTP server (e.g., username and password)
Step 1: Install Sendmail
If you don’t already have Sendmail installed on your system, you can install it by following these steps:
- On an RHEL-based system (such as Fedora or CentOS), use the following command to install Sendmail:
sudo yum install sendmail sendmail-cf
- On a Debian-based system (such as Ubuntu or Debian), use the following command to install Sendmail:
sudo apt install sendmail sendmail-cf
Step 2: Create SMTP Credentials File
To configure the login credentials required to log into the external SMTP server, you need to create a file called “smtp-auth-creds” in the /etc/mail directory.
Open the auth-info file using a text editor:
sudo nano /etc/mail/smtp-auth-creds
Add the following lines to the file, replacing “username” with your username and “password” with your password:
1 | AuthInfo:smtp.example.com "U:username" "P:password" "M:PLAIN" |
Save the file and close the text editor.
Compile the smtp-auth-creds file into the sendmail
makemap hash /etc/mail/smtp-auth-creds < /etc/mail/smtp-auth-creds
Step 3: Modify the sendmail.mc File
The next step is to modify the sendmail.mc file to configure the relaying of emails through an external SMTP server. The sendmail.mc file is located in the /etc/mail directory.
Open the sendmail.mc file using a text editor:
sudo nano /etc/mail/sendmail.mc
In the configuration file, at line number 26, configure the SMART_HOST to the relay server. For example, to send email via Gmail SMTP, set the value to "smtp.gmail.com":
1 | define(`SMART_HOST', `smtp.example.com')dnl |
After the above line, add more configuration like SMTP credentails, SMTP port "587" with the port number used by the your external SMTP server etc.
1 2 3 4 5 | define(`ESMTP_MAILER_ARGS', `TCP $h 587')dnl FEATURE(`authinfo', `hash -o /etc/mail/smtp-auth-creds.db')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 |
Save the file and close the text editor.
Step 4: Generate the sendmail.cf File
The sendmail.mc file is used to generate the sendmail.cf file, which is used by Sendmail to determine its configuration. To generate the sendmail.cf file, use the following command:
sudo make -C /etc/mail
Then restart the sendmail service to apply the changes:
systemctl restart sendmail
Step 5: Verify Setup
To verify that the configuration is working as expected, you can use the sendmail command-line utility to send a test email:
echo "This is a test email" | sendmail [email protected]
If everything is configured correctly, you should receive the test email in your inbox.
Conclusion
In conclusion, configuring Sendmail to relay emails through an external SMTP server is a relatively straightforward process that can greatly improve the reliability and speed of your email system. By following the step-by-step guide outlined in this article, you can easily configure Sendmail to work with your external SMTP server, ensuring that your emails are delivered promptly and reliably. Remember to always back up your configuration files before making any changes, and test your configuration thoroughly to ensure that it is working as expected.