Sendmail configuration to relay emails via remote SMTP

In this page we will only do the sendmail configuration to use third party SMTP service.
For this configuration as a example GMAIL SMTP service going to be use.

Before starting to configure any application it is a best practice to configure a hostname of the machine according to their service or any to identify as a resource in your environment.
Follow this link to change hostname.

  1. I am using CentOS 8 machine will use below commands to install sendmail and its require configuration packages

    $ sudo dnf install sendmail procmail cyrus-sasl sendmail-cf -y

    If you are already logging as root user then use the above command without sudo.

  2. To edit the sendmail files we will require root user's privilege.

  3. Create directory to store authentication details in a file, it will require to authenticate remote SMTP.

    # mkdir /etc/mail/authinfo # chmod 700 /etc/mail/authinfo

  4. Due to mandatory multi-factor authentication (MFA) in GMAIL service it is require to create "App Password" for the instruction follow this link

  5. Create an authentication file by any name like in this example we have smtp-gmail file.

    # vi /etc/mail/authinfo/smtp-gmail

    And add the following content in it.

    AuthInfo: "U:root" "" "P:AppPassword"

    In the above content AppPassword is created as instructed in step 3 link.

  6. Create a hash map file for same authentication file which will be in .DB extention.

    # makemap hash /etc/mail/authinfo/smtp-gmail < /etc/mail/authinfo/smtp-gmail

    This hash file will use in configuration to authenticate remote SMTP service. Once the hash file is created or update the authentication details file can be remove but if there is any change like change in password then again that authentication file is need to create and update the hasp file with same command. If the hash file is updated in same filename then there is no require to re-build sendmail configuration or restart sendmail service.

  7. Edit aliases file for the users who can use the relay mail service so that whenever that user tries to send mail then he can use remote SMTP service.

    # vi /etc/aliases

    Add below line for root user in aliases file below the line of postmaster: alias. Replace the sender with your GMAIL account Id.


    After edit To update aliases.db file

    # newaliases

  8. Add the below lines in source file at the line above which contain "MAILER(smtp)dnl".
    It is always better to keep the require configuration together. Some of the below options are available but those are commented.

    define(`SMART_HOST',`[]')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

  9. Re-build configuration of sendmail. In this rebuild process, source file with .MC extension is convert in to sendmail configuration file which ends with .CF extension it can read by sendmail process. To make sendmail configuration file command m4 can be use.

    m4 /etc/mail/ > /etc/mail/

    Or the below shell script which is shipped with sendmail package


  10. To apply the changes done in restart the sendmail service.

    # systemctl restart sendmail

  11. Test configured sendmail configuration.

    echo "The only source of knowledge is the experience" | mail -s "Test sendmail relay"
Design by SanjuD (@ngineerbabu)