The AppGini Blog
A few tips and tricks to make your coding life a tiny bit better.

Sending Emails from Docker Containers using Gmail SMTP

Sending Emails from Docker 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.

  1. Go to your Google Account settings: https://myaccount.google.com/apppasswords
  2. Generate a new app password. Name it something like “Docker SMTP”.
  3. 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.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# Use your desired PHP version
FROM php:8.2-apache

# Install msmtp and ca-certificates
RUN apt-get update && apt-get install -y msmtp ca-certificates && rm -rf /var/lib/apt/lists/*

# Copy the msmtprc configuration file
COPY msmtprc /etc/msmtprc

# Fix permissions for msmtprc (must be readable only by owner)
RUN chmod 600 /etc/msmtprc && chown www-data:www-data /etc/msmtprc

# Configure PHP to use msmtp as the sendmail command
RUN echo "sendmail_path = /usr/bin/msmtp -t" > /usr/local/etc/php/conf.d/sendmail.ini

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.

1
2
3
4
5
6
7
8
9
version: '3.8'

services:
  web:
    build: .
    ports:
      - "80:80"
    volumes:
      - ./src:/var/www/html

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:

1
2
3
4
5
# Build the image
docker build -t my-php-app .

# Run the container
docker run -d -p 80:80 --name my-running-app my-php-app

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.

1
2
3
4
5
6
7
8
$to      = '[email protected]';
$subject = 'Contact Form Submission';
$message = 'Hello, this is a message from a visitor.';
$headers = 'From: [email protected]' . "\r\n" .
           'Reply-To: [email protected]' . "\r\n" .
           'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);

Limitations and Recommendations

While using Gmail SMTP is great for development, testing, or very low-volume personal projects, it has significant limitations:

  1. 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.
  2. Professionalism: Emails will always appear to come from @gmail.com (or your Google Workspace domain), which might not look professional for a business application.
  3. 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.