< BACK

Email mini tutorials and how-to guides

Using Google Mail (SMTP) with exim4 on Ubuntu 16.04

Updated: 2020-01-19

This method uses authentication via Google’s App Passwords function for SMTP access to Google mail. See here for more detail on using App Passwords.

Step 1: Set up Google App Passwords for your server

Sign in to your Google account at https://myaccount.google.com/ and under the Security option, make sure 2-Step Verification is enabled and click App passwords

5a17f86ef5107f29de36dc967b9dda85.png

for which Google may require you to re-authenticate.

Once you are signed-in to Google Account, in the App passwords screen, choose Mail from Select App dropdown, and Other (custom name) from Select device, like this:

6268dc166ac3e1ba60a8b7c3203bc10c.png

e051063c29ecb261eded606e5c560156.png

and enter a designation of your choice (in this example “server_rainbow”):

8f79237caf302a70ec27a62174b70a54.png

then click the [Generate] button to obtain a password for the device:

b4eacfa5b1d911d3428e800123ef7a55.png

and make a note of this 16 character password! This will be used later.

Step 2: Install and configure exim4 on the Ubuntu server

Use apt to install necessary packages:

sudo apt update
sudo apt install exim4 eximon4 exim4-doc-html file spf-tools-perl swaks

If your are not prompted to configure exim4 during the install process, issue the following command to initiate this:

sudo dpkg-reconfigure exim4-config

This is a summary of the prompts which are presented and the appropriate response or choice:

PROMPT RESPONSE
Configuration type: mail sent by smarthost; received via SMTP or fetchmail 
System mail name: <hostname> => localhost
Other destinations for which mail is accepted:  leave empty
Machines to relay mail for:  leave empty
IP address or host name of the outgoing smarthost: smtp.gmail.com::587
Hide local mail name in outgoing mail?:  no
Keep number of DNS-queries minimal (Dial-on-Demand) ?: no
Delivery method for local mail: mbox format in /var/mail/
Split configuration into small files ? Yes
Root and postmaster mail recipient: <username>

and the screens should look very similar to these:

434f5ccd63d33cc45111f5bb2eaa89df.png

89dadfa99ab323680cbe1a225acd8962.png

27f5acce2d9eb41cc892f2521807d321.png

cfdbded63613e45d3d223a4ff01a0829.png

a01d51ed56a908cfe3fec1cc5a477314.png

6e6c940d9d89521c1ece649c8163f57c.png

0d5f2187ac35d3283a3297c48b88492c.png

2e49899ab795a3a27010956a88ff4d47.png

ca6a63d249a32a8343b7be58d7f5b28d.png

56a0c6d7c0d354abc44c06580c5457c4.png

eab5dac9209820654604c6f4e4b6350f.png

Step 3: Prepare a Google App Password password files

This process creates two files which can only be accessed by root and the exim4 process user.

You will need the 16 character password Google App Password generated in Step 1, above, and your google email address.

Step 3a: Create a password file

sudo touch /etc/exim4/passwd.client
sudo chown Debian-exim:root /etc/exim4/passwd.client
sudo chmod 640 /etc/exim4/passwd.client
sudo touch /etc/exim4/.pw
sudo chmod 400 /etc/exim4/.pw

Now save the 16 character password Google App Password in a single line in the /etc/exim4/.pw file using your standard editor, e.g.

sudo vim /etc/exim4/.pw

When you change this password, this file needs to be updated, AND the following process needs to be executed once more.

Step 3b: Generate the exim4 password file

This process generates the exim4 password file - note that you need to specify your own Google email address as a variable EMAIL:

sudo su # change to user root
GMAIL=your_gmail_address@gmail.com # specify your google email address
read -r -d '' LIST <<EOF
gmail-smtp.l.google.com:${GMAIL}:$(cat /etc/exim4/.pw)
*.google.com:${GMAIL}:$(cat /etc/exim4/.pw)
smtp.gmail.com:${GMAIL}:$(cat /etc/exim4/.pw)
EOF
echo "${LIST}" >> /etc/exim4/passwd.client

Step 4: Configure address re-writing

Add the following to /etc/email-addresses :

sudo su # become root
GMAIL=your_gmail_address@gmail.com # specify your google email address
USERNAME="YOUR-USER-NAME" # your local username
HOSTNAME="YOUR-HOSTNAME" # your hostname defined for 127.0.1.1 in /etc/hosts
echo "${USERNAME}: ${GMAIL}" >> /etc/email-addresses
echo "${USERNAME}@localhost: ${GMAIL}" >> /etc/email-addresses
echo "${USERNAME}@${HOSTNAME}: ${GMAIL}" >> /etc/email-addresses
echo "${USERNAME}@{HOSTNAME}.localdomain: ${GMAIL}" >> /etc/email-addresses

and add a line to your /etc/aliases file

echo "${USERNAME}: ${GMAIL}" >> /etc/aliases

Note: Using a valild MX record domain

On my system I also had to ensure that the entry in /etc/mailname was for a domain which an MX record was defined.

Now update exim4 configuration and restart the exim4 service:

sudo update-exim4.conf
sudo /etc/init.d/exim4 restart

[ ok ] Restarting exim4 (via systemctl): exim4.service.

Step 5: Test the functionality

To check that the setup is functioning, send a test mail while checking the mail log:

RECIPIENT="someone@somewhere.com" # enter an appropriate recipient email address
echo "Subject: test mail from "$(hostname)" at "$(date +%c) | sendmail "$RECIPIENT"; tail -f /var/log/exim4/mainlog
2020-01-08 11:20:05 exim 4.86_2 daemon started: pid=24623, -q30m, listening for SMTP on [127.0.0.1]:25
2020-01-08 11:20:05 Start queue run: pid=24624
2020-01-08 11:20:05 End queue run: pid=24624
2020-01-08 11:20:10 1ip8Re-0006PW-Ku <= root@localhost U=root P=local S=338
2020-01-08 11:20:11 1ip8Re-0006PW-Ku => XXXXXX@YYYYY.com R=smarthost T=remote_smtp_smarthost H=smtp.gmail.com [2a00:1450:400c:c06::6d] X=TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128 CV=yes DN="C=US,ST=California,L=Mountain View,O=Google LLC,CN=smtp.gmail.com" A=plain C="250 2.0.0 OK  1578478812 r6sm3687899wrq.92 - gsmtp"
2020-01-08 11:20:11 1ip8Re-0006PW-Ku Completed
2020-01-08 11:34:32 exim 4.86_2 daemon started: pid=25454, -q30m, listening for SMTP on [127.0.0.1]:25
2020-01-08 11:34:32 Start queue run: pid=25455
2020-01-08 11:34:32 End queue run: pid=25455
2020-01-08 11:35:20 1ip8gK-0006cv-4v <= ZZZZ@localhost U=root P=local S=338
2020-01-08 11:35:21 1ip8gK-0006cv-4v => XXXXXX@YYYYY.com R=smarthost T=remote_smtp_smarthost H=smtp.gmail.com [2a00:1450:400c:c06::6d] X=TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128 CV=yes DN="C=US,ST=California,L=Mountain View,O=Google LLC,CN=smtp.gmail.com" A=plain C="250 2.0.0 OK  1578479721 z83sm3428429wmg.2 - gsmtp"
2020-01-08 11:35:21 1ip8gK-0006cv-4v Completed

Finally verify that the test mail arrived.