Sending System E-mails with Gmail
Here's how to send system mails from your computer using a gmail account:
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.