Getting mail to work with GMail in Docker

I am moving my web site to docker, and I need to be able to send emails using a GMail account. GMail is limited to 500 emails a day, but that’s more than enough for my modest operations at this stage.

No separate container for email

In a “docker spirit” I tried to create a dedicated container for the email client, but it was a bad idea. To access the email container from the web server container, you need local email client anyway, and if you have one, that local client may go directly to GMail, removing the need for the dedicated email container.

Enable less secure apps in GMail

VERY IMPORTANT: Regardless of the mail client you are using, make sure to enable “less secure apps” in the settings of the Gmail account you use for sending. Without it, GMail will complain that the user name and the password are incorrect, when they are in fact perfectly alright. To do so:

  1. Open gmail.com.
  2. Click on your account icon in the top left, and choose “Manage my Google account”.
  3. Select Security on the left.
  4. Scroll down to “Less secure app access”.
  5. Click on “Turn on access (not recommended)”.
  6. Click on the switch to allow less secure apps.

Alternatively, you may go directly to https://myaccount.google.com/u/2/lesssecureapps.

Note that while this all works at the time of writing, Google may change the user interface at any time without warning, so if you can’t find some of those controls, don’t blame me 🙂

msmtp

Msmtp, or Minimal SMTP server proved to be easy to install and configure.
The code is here: https://github.com/ikriv-tests/dockermail/tree/master/msmtp

The steps to get it going are quite simple:


git clone https://github.com/ikriv-tests/dockermail.git
cd dockermail/msmtp

# Edit the file and replace YOUR_MAIL and YOUR_PASSWORD with real values
vi mail/msmtprc

# Build the docker image
sudo docker build -t msmtptest .

# Start the container and test that it works
sudo docker run --rm msmtptest bash -c 'printf "Subject: Test from msmtp\nThis is a test" | \
msmtp recipient@example.com'

Postfix

I did not have much luck with Postfix. I got lost on installing TLS certificates or whatever, and gave up.

Sendmail

Sendmail is what my original, non-containerized web site is usingMy web server container is based on Ubuntu, which has lots of email clients. I started with Sendmail, which I use on the current, non-containerized web site. The result was not good. I was able to configure Sendmail to use GMail, but squeezing Sendmail into Docker container did not work well. It complained about things, hung on “Updating /etc/mail/aliases…” for a very long time, and in the end did not work.

Code: https://github.com/ikriv-tests/dockermail/tree/master/sendmail

To configure Sendmail to run with Gmail, add the following lines to etc/mail/sendmail.mc just before MAILER_DEFINITIONS:

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/gmail-auth')dnl

Then as root create directory /etc/mail/auth, and put there file gmail-auth that looks like this:

AuthInfo:smtp.gmail.com "U:whatever" "I:your-email@gmail.com" "P:your_password"

Give “auth” directory access mask of 700, and gmail-auth file access mask of 600, then run the following command to generate authentication database:

cd /etc/mail/auth && makemap hash gmail-auth <gmail-auth

Finally, restart Sendmail with new configuration:

yes 'y' | sendmailconfig

This works great in standalone mode, but produces hiccups under Docker. The build complains about things, hangs for a very long time on “Updating /etc/mail/aliases…”, and in the end of the day trying to send a test email does not work. Again, these steps work great in standalone mode, i.e. if you create an Ubuntu virtual machine on AWS and do them manually, but not with Ubuntu container in Docker. I guess some important dependencies are missing, but I did not have time to investigate. I just went with msmtp.

Leave a Reply

Your email address will not be published. Required fields are marked *