sendmail: Configuring Gmail as sendmail email relay

This guide describe the steps needed to configure your Gmail account as sendmail email relay.

  • sendmail package on your machine
  • Gmail account
  • Connectivity to smtp.gmail.com on 587/tcp port

The first step, backup the sendmail configuration, for restore the previous configuration in case something goes wrong:

# cd /etc/mail
# cp sendmail.cf sendmail.cf.orig
# cp sendmail.mc sendmail.mc.orig

In the next step, we need to create a file to store the credentials Gmail to access to the smtp server.

Create the /etc/mail/auth directory:

# mkdir -m 700 -p /etc/mail/auth

Create auth-info file to store the Gmail credentials:

# touch /etc/mail/auth/auth-info

Open the auth-info file in your text-editor and copy & paste the follow lines. Replace <USERNAME> and <PASSWORD> placeholders with your credentials:

AuthInfo:smtp.gmail.com "U:root" "I:<USERNAME>@gmail.com" "P:<PASSWORD>"

Save the file and make the database-map:

# cd /etc/mail/auth
# makemap hash auth-info < auth-info

At the end of execution will be created auth-info.db file.

For strong security, set the read-write permission only for root user:

# chmod 0600 /etc/mail/auth/*

Open /etc/mail/sendmail.mc file in your text-editor and copy the follow lines and paste before the first MAILER line:

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_MECHANISMS', `EXTERNAL GSSAPI DIGEST-MD5 CRAM-MD5 LOGIN PLAIN')dnl
FEATURE(`authinfo',`hash /etc/mail/auth/auth-info')dnl
TRUST_AUTH_MECH(`EXTERNAL DIGEST-MD5 CRAM-MD5 LOGIN PLAIN')

Save the file and make the sendmail.cf:

# cd /etc/mail
# m4 sendmail.mc > sendmail.cf

Restart sendmail service:

# sh /etc/rc.d/rc.sendmail restart

Now, you are ready for send the first email:

$ echo 'e-Mail TEST'| mail -s TEST [email protected]
  • en/how-to/sendmail-and-gmail-relay.txt
  • Last modified: 2 years ago
  • by Giuseppe Di Terlizzi