Sending System E-mails with Gmail

From Jeremy Bryan Smith
Jump to navigation Jump to search

Simple (SSMTP)

Here's how to send system mails from your computer using a gmail account. You will be limited to sending emails from your gmail address (the one with which you authenticate). These instructions work whether you have a free @gmail.com address or you are using GSuite to provide email for your domain, (e.g. mail.mydomain.example.com).

Assuming apt-based package manager, remove sendmail (overkill and bloated for our use) and install ssmtp:

apt-get remove sendmail*
apt-get install ssmtp

Log in to gmail and create an application password for authentication: https://myaccount.google.com/apppasswords In this example, the application password is:

sdfsdfgadfgdfsdf

In your /etc/ssmtp/ssmtp.conf:

#
# Config file for sSMTP sendmail
#
# The person who gets all mail for userids < 1000
# Make this empty to disable rewriting.
root=myusername@gmail.com

# The place where the mail goes. The actual machine name is required no 
# MX records are consulted. Commonly mailhosts are named mail.domain.com
mailhub=smtp.gmail.com:587
 
AuthUser=myusername@gmail.com
AuthPass=sdfsdfgadfgdfsdf

UseSTARTTLS=yes
UseTLS=yes

# Where will the mail seem to come from?
rewriteDomain=mydomain.com

# The full hostname
hostname=mydomain.com

# Are users allowed to set their own From: address?
# YES - Allow the user to specify their own From: address
# NO - Use the system generated From: address
FromLineOverride=YES


Note: The rewriteDomain and hostname options don't really work... gmail always replaces the From address with your gmail address :-(


For the examples below, let's assume the following:

  • Your gmail address is: myusername@gmail.com
  • Your local system's hostname is: mycomputer.mydomain.com
  • Your local system username is: jeremy
  • You will send a test email to Bob Ross <bobross@happytrees.com>

Adjust each of those and anything in red as needed.

Edit your /etc/ssmtp/revaliases, add a line for any local user for which you would like to have all mail redirected elsewhere.

jeremy:myusername@gmail.com:smtp.gmail.com:587

You should have the following in /etc/hosts:

mycomputer.mydomain.com

You should have the following in /etc/hosts

127.0.1.1 mycomputer.mydomain.com

Test sending an email to Bob Ross via bobross@happytrees.com with:

echo -e "To: Bob Ross <bobross@happytrees.com>\r\nSubject: Email test at $(date) from $(hostname)\r\n\r\ntesting..."  | sendmail jeremy

This results in the following email:

Bcc: jeremy@mydomain.com
Return-Path: <myusername@gmail.com>
Received: from mycomputer.mydomain.com ([2603:3020:2ff0:c000::1874])
        by smtp.gmail.com with ESMTPSA id f17sm2049437uaq.19.2020.01.24.21.38.52
        for <jeremy@mydomain.com>
        (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256);
        Fri, 24 Jan 2020 21:38:54 -0800 (PST)
Message-ID: <5e2bd46e.1c69fb81.b7332.d795@mx.google.com>
Received: by mycomputer.mydomain.com (sSMTP sendmail emulation); Sat, 25 Jan 2020 00:38:52 -0500
From: root <myusername@gmail.com>
Date: Sat, 25 Jan 2020 00:38:52 -0500
To: Bob Ross <bobross@happytrees.com>
Subject: Email test at Sat Jan 25 00:38:52 EST 2020 from mycomputer.mydomain.com

testing...

Which appears like so in your inbox:

from:      root <myusername@gmail.com>
to:        Bob Ross <bobross@happytrees.com>
bcc:       jeremy@mydomain.com
date:      Jan 25, 2020, 12:38 AM
subject:   Email test at Sat Jan 25 00:38:52 EST 2020 from mycomputer.mydomain.com
mailed-by: gmail.com

You can then create an email filter for organizing those emails. Create a filter with the To: field. You can use either the Bcc: address above (jeremy@mydomain.com) or the To: address (Bob Ross <bobross@happytrees.com>).

I use the following filter convention so that I can filter by domain name, computer name, and user name:

  • Email format
    • To: field: jeremy@mycomputer.mydomain.com
  • Gmail Filter
    • To: jeremy@mycomputer.mydomain.com
    • Label Name: Systems/mycomputer.mydomain.com/jeremy

For example, you may have the following addresses:

  • jeremy@desktop.home.mydomain.com
  • apache@desktop.home.mydomain.com
  • jeremy@notebook.office.mydomain.com
  • root@firewall.home.mydomain.com
  • root@freenas.home.mydomain.com


Even though gmail delivers them to your gmail address (myusername@gmail.com), you can still filter based on the original To: or system (Bcc:) address.

Advanced (sendmail, GSuite only)

If you are paying for hosted gmail via GSuite, you can set up a mail relay to act as your local network's mail server for other hosts. In addition, this mail relay will be able to send emails from any address under your domain (e.g. anybody@mydomain.example.com) In this example, I assume:

  • We'll use sendmail for this
  • Your top level domain is example.com
  • Your username is (jeremy)
  • Your network's domain is mydomain.example.com
  • The hostname of the machine running the mail relay is: mail (mail.mydomain.example.com)

On your mail relay server:

Install sendmail:

apt-get install sendmail

In /etc/mail/sendmail.mc, place the following:

define(`confDOMAIN_NAME', `mail.example.com')dnl
MASQUERADE_AS(`example.com')dnl

You may need to add the following to /etc/hosts to make sendmail happy:

127.0.0.1 mydomain.example.com

Rebuild the sendmail config, restart:

cd /etc/mail && make && service sendmail restart

You will now be able to send emails from any address at your domain. For example:

echo -e "From: anybody@example.com\r\nSubject: Email test at $(date) from $(hostname)\r\n\r\ntesting..."  | sendmail someone@overhere.com

You can then also configure your various systems within that network with SSMTP as follows (without needing an application password):

#
# Config file for sSMTP sendmail
#
# The person who gets all mail for userids < 1000
# Make this empty to disable rewriting.
root=jeremy@example.com

# The place where the mail goes. The actual machine name is required no 
# MX records are consulted. Commonly mailhosts are named mail.domain.com
mailhub=mail.mydomain.example.com

# Where will the mail seem to come from?
rewriteDomain=example.com

# The full hostname
hostname=desktop.mydomain.example.com

# Are users allowed to set their own From: address?
# YES - Allow the user to specify their own From: address
# NO - Use the system generated From: address
FromLineOverride=Yes