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.

Advertisement

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:

  1. Access to a Unix-based system with Sendmail installed.
  2. Sudo or root privileges to modify the Sendmail configuration.
  3. 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:

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:

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 

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 your-email@example.com 

Replace your-email@example.com 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.

Share.

10 Comments

  1. 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!

  2. 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 ()

  3. 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!

  4. 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” recipient@example.com ) 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

Leave A Reply

Exit mobile version