Skip to content

Instantly share code, notes, and snippets.

@egatjens
Last active April 20, 2024 02:44
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save egatjens/867f2035e6b9660b174c to your computer and use it in GitHub Desktop.
Save egatjens/867f2035e6b9660b174c to your computer and use it in GitHub Desktop.
Configuring Gmail as a Sendmail email relay | Ubuntu 14
Setup sendmail

Prefix all commands with 'sudo'

  1. Install prerequisites
$ apt-get install sendmail mailutils
  1. Configure hosts file correctly:
$ vim /etc/hosts

And make sure the line looks like this:

127.0.0.1 localhost localhost.localdomain yourhostnamehere
  1. Reload /etc/hosts, so that the previous changes take effect
$ /etc/init.d/networking stop
$ /etc/init.d/networking start
  1. Run the sendmail config and answer ‘Y’ to everything:
$ sendmailconfig
Create Gmail Authentication file

Need to log as root: 'sudo -s'

$ mkdir  -m 700 /etc/mail/authinfo/
$ cd /etc/mail/authinfo/

next we need to create an auth file with a following content. File can have any name, in this example the name is gmail-auth:

AuthInfo: "U:root" "I:YOUR GMAIL EMAIL ADDRESS" "P:YOUR PASSWORD"

Replace the above email with your gmail or google apps email.

Please note that in the above password example you need to keep 'P:' as it is not a part of the actual password.

In the next step we will need to create a hash map for the above authentication file:

$ makemap hash gmail-auth < gmail-auth
Configure your sendmail

Put bellow lines into your sendmail.mc configuration file right above first "MAILER" definition 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_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/gmail-auth.db')dnl
define(`confDOMAIN_NAME', `app05.site.com')dnl

Do not put the above lines on the top of your sendmail.mc configuration file !

In the next step we will need to re-build sendmail's configuration. To do that execute:

$ make -C /etc/mail

Reload sendmail service:

/etc/init.d/sendmail reload

and you are done.

Update Gmail Sign-in & Security settings.

On Gmail Settings Put "Allow less secure apps": ON

If you try to send the email without this step, Google will stop the sign-in attempt from the server.

Configuration test

Now you can send an email from your command line using mail command:

$ echo "Just testing my sendmail gmail relay" | mail -s "Sendmail gmail Relay" my-email@my-domain.com
Taken from:

LinuxConfig

holarails

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment