Facebook X (Twitter) Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook X (Twitter) Instagram
    TecAdmin
    You are at:Home»Mail Servers»Sendmail»Configure Sendmail to Relay Through Remote SMTP Servers

    Configure Sendmail to Relay Through Remote SMTP Servers

    Understanding how to configure Sendmail to relay emails through a remote SMTP server is an essential skill for any system administrator or IT professional managing a mail server. In this article, we'll explore step-by-step how you can set up Sendmail for SMTP relay.
    By RahulJune 26, 20233 Mins Read

    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:

    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:

    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.

    Gmail sendmail smtp
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp

    Related Posts

    What is SMTP (Simple Mail Transfer Protocol)?

    How to Flush Sendmail Mail Queue

    Python Script to Send an Email via SMTP Server

    View 10 Comments

    10 Comments

    1. Nemanja on December 10, 2020 11:18 am

      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!

      Reply
    2. Filipi Souza on May 29, 2020 5:30 pm

      In CentOS 7, I needed of the packet cyrus-sasl-plain to send e-mails by relay auth.

      Reply
    3. Coreigh Utegaard on May 26, 2020 9:51 pm

      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

      Reply
    4. Carlos on August 14, 2019 3:36 pm

      Thanks Rahul, it worked beautifully!!!!

      Reply
    5. scorp13 on May 3, 2019 1:39 pm

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

      Reply
    6. Abhilash on February 21, 2019 1:02 am

      can you please attach the sendmail.mc file? the post is a little confusing for the newbie.

      Thanks

      Reply
    7. Jerry on August 27, 2018 2:33 am

      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!

      Reply
      • chris on July 8, 2020 4:11 pm

        I will back this up. I needed to change too to work.

        Reply
    8. Rodel on March 15, 2018 3:56 am

      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

      Reply
      • Rahul K. on March 15, 2018 10:57 am

        Have you changed the recipient address?

        Reply

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • Using .env File in FastAPI
    • Setting Up Email Notifications for Django Error Reporting
    • How to Enable Apache Rewrite (mod_rewrite) Module
    • What are Microservices?
    • Variable Expansion in ZSH
    Facebook X (Twitter) Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.