$linuxcli.in _    [commands]    [tips & tricks]    [manpage]    [ESXi CLi]    [what is my ip?]    [pingme!]    [BIOS lock code]    [Code repo]


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. In CentOS 8 use below commands to install sendmail and its require configuration packages
  2. $ sudo dnf install sendmail procmail cyrus-sasl sendmail-cf -y

    If already logged-in as root user then use the above command without sudo.

    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.
  4. # mkdir /etc/mail/authinfo

    # chmod 700 /etc/mail/authinfo


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

  6. Create an authentication file by any name like in this example we have smtp-gmail file.
  7. # vi /etc/mail/authinfo/smtp-gmail

    And add the following content in it.

    AuthInfo: "U:root" "I:sender@gmail.com" "P:AppPassword"

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


  8. Create a hash map file for same authentication file which will be in .DB extention.
  9. # 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.


  10. Edit aliases file for the users who can use the relay mail service.
  11. # vi /etc/aliases

    Whenever that user tries to send mail then he can use remote SMTP service.

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

    root: sender@gmail.com

    After edit to apply the changes in aliases.db file run below command.

    # newaliases


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

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


  14. Re-build configuration of sendmail.
  15. m4 /etc/mail/sendmail.mc > /etc/mail/sendmail.cf

    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.

    Or run the below shell script which is shipped with sendmail package

    # /etc/mail/make


  16. To apply the changes done in sendmail.cf restart the sendmail service.
  17. # systemctl restart sendmail


  18. Test configured sendmail configuration.
  19. $ echo "The only source of knowledge is the experience" | \
    mail -s "Test sendmail relay" recipient@gmail.com

Last updated on 30 June 2023 10:43 am IST