Sending Emails from Docker Containers using Gmail SMTP

Sending emails from within Docker containers can be tricky. You might not want to set up a full-blown mail server like Postfix inside a container. A lightweight and reliable alternative is to use msmtp as a sendmail replacement and relay emails through Gmail’s SMTP servers.
Here is a step-by-step guide to setting this up.
1. Get a Gmail App Password
If you use your normal Gmail password, authentication will likely fail, especially if you have 2-Step Verification enabled (which you should!). Google requires an “App Password” for this kind of access.
- Go to your Google Account settings: https://myaccount.google.com/apppasswords
- Generate a new app password. Name it something like “Docker SMTP”.
- Copy the 16-character password. You will need it for the configuration file.
2. Configure msmtprc
Create a file named msmtprc in your project directory. This file tells msmtp how to connect to Gmail.
Replace [email protected] with your actual email address and your-app-password with the password you generated in step 1.
defaults
auth on
tls on
tls_trust_file /etc/ssl/certs/ca-certificates.crt
logfile /var/log/msmtp.log
account gmail
host smtp.gmail.com
port 587
from [email protected]
user [email protected]
password your-app-password
account default : gmail
3. The Dockerfile
You need to install msmtp in your Docker image and configure PHP to use it. Here is an example Dockerfile based on a standard PHP image.
|
|
4. Running with Docker Compose
If you are using docker-compose, your setup might look like this. This assumes you have the Dockerfile and msmtprc in the same directory.
|
|
When you build and run this (docker-compose up --build), your PHP application inside the container will be able to use the standard mail() function, and msmtp will handle the delivery via Gmail.
5. Running with docker run
If you prefer to build and run the image manually:
|
|
Important: The Reply-To Header
When sending emails through Gmail’s SMTP, Gmail automatically rewrites the From address to match the authenticated account (your Gmail address). This is to prevent spam and spoofing.
If your application sends emails on behalf of users (e.g., a contact form where the “From” address is the visitor’s email), this rewrite can be problematic because hitting “Reply” would reply to yourself.
To solve this, always use the Reply-To header in your PHP code. Gmail respects this header.
|
|
Limitations and Recommendations
While using Gmail SMTP is great for development, testing, or very low-volume personal projects, it has significant limitations:
- Sending Limits: Gmail has strict daily sending limits (usually around 500 emails per day for free accounts). If you exceed this, your account may be temporarily suspended.
- Professionalism: Emails will always appear to come from
@gmail.com(or your Google Workspace domain), which might not look professional for a business application. - Deliverability: Sending transactional emails from a personal Gmail account can sometimes lead to deliverability issues or being flagged as spam.
For production applications or higher volumes of transactional email, it is highly recommended to use a dedicated transactional email provider. These services offer better deliverability, analytics, and higher sending limits. Some popular options include:
Most of these providers offer a free tier that is sufficient for small to medium-sized projects.