How to send mail using php mail() function in docker way
Using ssmtp to send email to the relay container
Sometimes we want to run dockerized old php site that we do not want to work with, or a programmer is gone and nobody cares to make changes to use email relay host, such as mailgun or gmail or anything else. In the Linux VM or bare metal server it is quite easy task – you run web server and mail server two in one and mail server takes care of mail routing.
In the dockerized environment usually you want to run the least amount of services possible in the container, so sending mail using PHP's mail() function becomes a tricky.
Let's create a docker-compose.yml file, containing all required containers:
caddy – web server
php – php server for the app – the trick here to use msmtp as a sendmail to have mail sent to remote server (our mail container)
mail – smtp relay server, we will use postfix
the source code is here: docker-php-mail-example
the main thing is to use ssmtp on the php container and send data to mail container
enjoy!
Commen in the Fediverse @saint@river.group.lt