Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / containers / docker

Replacing SSMTP with MSMTP in Docker

5.00/5 (1 vote)
4 Feb 2020CPOL2 min read 12.8K  
Looks like sSMTP is no longer maintained, MSMTP is the suggested replacement. This post discusses how to do the replacement.

I was trying to build a new image of a Docker file that I’ve been running for months, and I’m greeted with this error:

Package ssmtp is not available, but is referred to by another package.
This may mean that the package is missing, has been obsoleted, or
is only available from another source

E: Package 'ssmtp' has no installation candidate

Looks like sSMTP is no longer maintained, and has been orphaned since 19th March 2019. MSMTP is the suggested replacement. So let’s get replacing.

The Plan

  1. Install mSMTP in our Docker image
  2. Configure mSMTP to our smtp server (in this case Office 365)
  3. Tell PHP to use mSMTP for email instead of sSMTP
  4. Send a test email
  5. Final source

1. Install mSMTP in our Docker Image

We’re going to start with the php:7.30-apache docker image:

FROM php:7.3-apache

Let's install msmtp with apt-get, and tidy up afterwards:

apt-get update && apt-get install ssmtp -y && \
rm -rf /var/lib/apt/lists/*

2. Configure mSMTP to our smtp Server (in this case, Office 365)

We need to setup a configuration file for msmtp to have the smtp details. The msmtp config location is in /etc/.

Let’s create a new file called msmtprc, and add the following configuration:

account default
host smtp.office365.com
port 587
tls on
tls_starttls on
tls_trust_file /etc/ssl/certs/ca-certificates.crt
tls_certcheck on
auth on
user user@domain.com
password "YourAwesomeStr0ngP4zzw0rd"
from "user@domain.com"
logfile /var/log/msmtp.log

This is for Office 365, change the smtp details to gmail or your own smtp server.

Copy the Config into our Docker Image

COPY msmtprc /etc/msmtprc

Make the msmtprc Config File readable/writable by its Owner

RUN chmod 600 /etc/msmtprc

3. Tell PHP to Use mSMTP for Email Instead of sSMTP

Now that we have mSMTP setup, we need to tell PHP to use it.

You can get a copy of the default php.ini file from the docker image we have just created. It’s located in /usr/local/etc/php/php.ini-production.

docker cp <CONTAINER_ID>:/usr/local/etc/php/php.ini.production .

Open up your php.ini file, and make the following change to the sendmail_path variable:

sendmail_path = /usr/bin/msmtp -t

Copy your php.ini file to the Docker image php config location:

COPY php.ini $PHP_INI_DIR/php.ini

4. Send a Test Email

With all that setup, we should now be able to send an email.

1. Build the Docker Image

docker build . -t php-msmtp-setup:latest

2. Run the Container

docker run -d -p 80:80/tcp php-msmtp-setup:latest

3. Log Into the Container

Run the following command to find the CONTAINER ID of our new container:

docker container ls

Run the following command to login to the container:

docker exec -ti <CONTAINER_ID> /bin/bash

4. Send an Email Directly With msmtp

echo -e "Subject: Test Mail\r\n\r\nThis is a test mail, 
let me know if this works" |msmtp --debug --from from@yourdomain.com -t to@someone.com

5. Send an Email with php

php -r "mail('to@domain.com','Test Mail from PHP', 
'This is a test mail from PHP, let me know if this works');"

5. Final Source

Our Docker file:

FROM php:7.3-apache

RUN apt-get update && apt-get install msmtp -y && \
    rm -rf /var/lib/apt/lists/*

COPY msmtprc /etc/msmtprc
RUN chmod 600 /etc/msmtprc

COPY php.ini $PHP_INI_DIR/php.ini

EXPOSE 80

The changes to our php.ini file:

sendmail_path = /usr/bin/msmtp -t

Our msmtprc file:

account default
host smtp.office365.com
port 587
tls on
tls_starttls on
tls_trust_file /etc/ssl/certs/ca-certificates.crt
tls_certcheck on
auth on
user user@domain.com
password "YourAwesomeStr0ngP4zzw0rd"
from "user@domain.com"
logfile /var/log/msmtp.log

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)