Skip to main content

Adding SMTP Email Functionality to the Docker WordPress Image using msmtp

I’ve always had trouble sending email through my dockerized apps (especially WordPress).

The Problem

I need to add and configure an SMTP Email plugin every time. Rather than using an extension at the code level, I wanted to do the job at the lower level.

The Solution

The general idea was to install a Mail Transfer Agent (MTA) which will be responsible for delivering mail from the container to a mail hub (SMTP server).

So I wrote this Dockerfile :

FROM wordpress:5.7.2-php8.0-apache

# Install the MTA package
RUN apt-get update && \
   apt-get install -y msmtp msmtp-mta ca-certificates && \
   apt-get clean

# MSMTP settings
COPY msmtprc /etc/msmtprc

Install the msmtp package. Additionally, install msmtp-mta, which creates a sendmail alias to msmtp.

The configuration file should look like this :

# Set defaults.

# Enable or disable TLS/SSL encryption.
auth           on
tls            on
tls_starttls   on
tls_certcheck  off
tls_trust_file /etc/ssl/certs/ca-certificates.crt
logfile        -

# Set up a default account's settings.
account        default
port           587
auth           login
host           ""
from           ""
user           ""
password       ""

My final docker-compose.yml looks like this :

version: '3.3'

    container_name: wp_db
    image: mysql:5.7
    restart: always
      MYSQL_ROOT_PASSWORD: secUrep@ssword
      MYSQL_DATABASE: wordpress
      MYSQL_USER: wordpress
      MYSQL_PASSWORD: secUrep@ssword

    container_name: wp
      context: .
      dockerfile: ./.docker/wordpress/Dockerfile
    restart: always
    working_dir: /var/www/html
      - db
      - ./wp-content:/var/www/html/wp-content
      WORDPRESS_DB_HOST: wp_db:3306
      WORDPRESS_DB_USER: wordpress
      WORDPRESS_DB_PASSWORD: secUrep@ssword
      WORDPRESS_DB_NAME: wordpress

I’ve omitted the details for the sake of simplicity. I hope the article has been useful to you though.

Ken's avatar

Mystro Ken

Ken is a Software Engineer passionate about user experience, software architecture, and open-source. He likes delivering human-centric digital experiences using top-notch JavaScript and PHP technologies.

Read more