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.
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'

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

  wp:
    container_name: wp
    build:
      context: .
      dockerfile: ./.docker/wordpress/Dockerfile
    restart: always
    working_dir: /var/www/html
    depends_on:
      - db
    volumes:
      - ./wp-content:/var/www/html/wp-content
    environment:
      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.


Mystro Ken Avatar

Mystro Ken

Cras mattis consectetur purus sit amet fermentum. Nullam id dolor id nibh ultricies vehicula ut id elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Follow me on Twitter