Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Owen Davies
Owen Davies

Posted on • Originally published atowendavies.net on

     

Replacing SSMTP with MSMTP in Docker

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, oris only available from another sourceE: Package 'ssmtp' has no installation candidate
Enter fullscreen modeExit fullscreen mode

Looks likesSMTP 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 thephp:7.30-apache docker image

FROM php:7.3-apache
Enter fullscreen modeExit fullscreen mode

Lets install msmtp with apt-get, and tidy up afterwards:

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

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 defaulthost smtp.office365.comport 587tls ontls_starttls ontls_trust_file /etc/ssl/certs/ca-certificates.crttls_certcheck onauth onuser user@domain.compassword "YourAwesomeStr0ngP4zzw0rd"from "user@domain.com"logfile /var/log/msmtp.log
Enter fullscreen modeExit fullscreen mode

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
Enter fullscreen modeExit fullscreen mode

Make the msmtprc config file readable/writable by its owner

RUN chmod 600 /etc/msmtprc
Enter fullscreen modeExit fullscreen mode

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 .
Enter fullscreen modeExit fullscreen mode

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

sendmail_path = /usr/bin/msmtp -t
Enter fullscreen modeExit fullscreen mode

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

COPY php.ini $PHP_INI_DIR/php.ini
Enter fullscreen modeExit fullscreen mode

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
Enter fullscreen modeExit fullscreen mode

2. Run the container

docker run -d -p 80:80/tcp php-msmtp-setup:latest
Enter fullscreen modeExit fullscreen mode

3. Log into the container

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

docker container ls
Enter fullscreen modeExit fullscreen mode

Run the following command to login to the container

docker exec -ti <CONTAINER_ID> /bin/bash
Enter fullscreen modeExit fullscreen mode

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
Enter fullscreen modeExit fullscreen mode

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');"
Enter fullscreen modeExit fullscreen mode

5. Final source

OurDocker file

FROM php:7.3-apacheRUN apt-get update && apt-get install msmtp -y && \    rm -rf /var/lib/apt/lists/*COPY msmtprc /etc/msmtprcRUN chmod 600 /etc/msmtprcCOPY php.ini $PHP_INI_DIR/php.iniEXPOSE 80
Enter fullscreen modeExit fullscreen mode

The changes to ourphp.ini file

sendmail_path = /usr/bin/msmtp -t
Enter fullscreen modeExit fullscreen mode

Ourmsmtprc file

account defaulthost smtp.office365.comport 587tls ontls_starttls ontls_trust_file /etc/ssl/certs/ca-certificates.crttls_certcheck onauth onuser user@domain.compassword "YourAwesomeStr0ngP4zzw0rd"from "user@domain.com"logfile /var/log/msmtp.log
Enter fullscreen modeExit fullscreen mode

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

C# Dev, DevOps, Azure, kubernetes, docker, terraform
  • Location
    London, UK
  • Joined

More fromOwen Davies

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp