Docker and PHP mail

October 6, 2020

Lang: cs en de es

Are you using Docker and want to send emails from PHP using the system function mail() ? In this article I will describe how to configure Docker and PHP to make sending emails work.

On the Internet, sending emails is a common thing. And even a web application (often just a web presentation) needs to send emails. Emails are stored on mail hosting servers. But how to send emails?

Windows

Web application developers who use Windows have always had a problem with sending emails. That's why various email sending tools or tools that capture emails from the development environment have been created.

Unix

On Linux or any other Unix, sending emails is standard. At most, to keep the email from being discarded or falling into SMAP, you need a local/test machine set up to forward emails through the mailserver, which is fully configured to ensure high email deliverability and emails do not go to SPAM.

Another option is to configure the application to send emails through an external SMTP server. This is a pretty good and versatile solution, but you have to make sure that every application is set up this way and on all places. Of course, even then, some lower level emails may not leave typical application crash information, which often use system emailing (although even this emailing could probably be overridden).

Docker

Currently, Docker is widely used, which is a great way to develop applications. However, with it, you are again solving the problem of sending emails. Because in Docker you only have one process running and nothing else, hence no mailserver. And if you have Docker on a production environment then functional email sending is a must.

Generally speaking, it's best not to use other technologies unless there is an explicit reason to do so. That is, unless there is a need to, Docker is just a useless technology to know for production. Every technology you use, you should be able to manage and have the time to do so, otherwise you are setting yourself up for problems.

Docker and PHP mail

So how do I configure Docker to send email from a PHP application?

As I mentioned in the Docker and Cron article, there is typically only one process running in docker. Therefore, if you are running a web application (web-server with PHP) in Docker, you will find that, that the mailserver, the service that is needed to send email, is not running on the system. Therefore, you must send mail from the PHP script using the SMTP protocol via an external service. Or you can modify the Docker image so that you can still use the PHP mail() function.

Specifically, the solution is to have PHP use a different command to send emails through an external mail server.
We will use the system utility msmtp to send. In older versions of Linux distributions, ssmtp was used.

The configuration for msmtp is in the /etc/msmtprc file.

The contents of the configuration file may look like this:

account default
host smtp.example.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 "password"
from "user@domain.com"
logfile /var/log/msmtp.log

You still need to modify the PHP configuration. Thus, to the php.ini file add the following:

[mail function]
sendmail_path = /usr/bin/msmtp -t

You can try sending an email via msmtp by entering the following command into the console of the running container:

msmtp -d -a default -i -t <<END
From: from@example.com
To: toto@example.com
Subject: test msmtp
test
END

If you have set everything up correctly, this works and you can use mail() in PHP in the normal way.

Email Trust

Even though you send emails, they may not reach the recipients' inbox. Emails may fall into SPAM or even be discarded. Therefore, always check what mailservers you are sending mail through and if they have emails are highly trusted, only then have you done your best to ensure that the emails arrive successfully in the recipient's inbox.

The Future of PHP

Deciding if the future is programming in PHP then watch this video Does it make sense to program in PHP?:

Articles on a similar topic

GitLab CI/CD: test automation and application deployment
Migrating VPS from VMware to Proxmox
VMware licensing change
Running Microsoft SQL Server on Linux
Backup: the Proxmox Backup Server
Linux as a router and firewall
How to upload a docker image to the Docker Registry
Linux: logical volume management
Linux Software RAID
Running a web application behind a proxy
Mailbox migration
Docker multistage build
Backing up your data by turning on your computer
Podman
Importing Windows into Proxmox virtualization
Proxmox virtualization
Docker and Cron
Lenovo ThinkPad X1 Carbon: LTE modem EM7544 commissioning
Yocto Project: Build custom operating system for embedded devices
Preparing a Linux server to run a web application in Python
How to address poor file share performance in Docker
How to get started using Docker correctly
Installing Linux on a dedicated HPE ProLiant DL320e server
How to stress test a web application
Why use the JFS filesystem
How to boot from a 4TB drive with GTP using UEFI
Btrfs file system
Raspberry PI
WINE - running Windous programs under Linux
GNU/Linux operating system

Newsletter

If you are interested in receiving occasional news by email.
You can register by filling in your email news subscription.


+