Send email on testing docker container with php and sendmail
Solution 1
It works.
In Dockerfile :
# sendmail config
############################################
RUN apt-get install -q -y ssmtp mailutils
# root is the person who gets all mail for userids < 1000
RUN echo "[email protected]" >> /etc/ssmtp/ssmtp.conf
# Here is the gmail configuration (or change it to your private smtp server)
RUN echo "mailhub=smtp.gmail.com:587" >> /etc/ssmtp/ssmtp.conf
RUN echo "[email protected]" >> /etc/ssmtp/ssmtp.conf
RUN echo "AuthPass=yourGmailPass" >> /etc/ssmtp/ssmtp.conf
RUN echo "UseTLS=YES" >> /etc/ssmtp/ssmtp.conf
RUN echo "UseSTARTTLS=YES" >> /etc/ssmtp/ssmtp.conf
# Set up php sendmail config
RUN echo "sendmail_path=sendmail -i -t" >> /usr/local/etc/php/conf.d/php-sendmail.ini
For testing inside php sendmail container :
echo "Un message de test" | mail -s "sujet de test" [email protected]
I succeed with the help of this two documents :
- https://unix.stackexchange.com/questions/36982/can-i-set-up-system-mail-to-use-an-external-smtp-server
- https://github.com/cmaessen/docker-php-sendmail/blob/master/Dockerfile
Solution 2
If it says:
"Package 'ssmtp' has no installation candidate"
You can use msmtp instead.
Add the following to your dockerfile
# sendmail config
#################
ARG SMTP_PASSWORD=not_provided
# install
RUN apt-get install -q -y msmtp mailutils
# config
COPY msmtprc /etc/msmtprc
RUN chmod 600 /etc/msmtprc
RUN chown www-data:www-data /etc/msmtprc
ARG SMTP_PASSWORD=not_provided
RUN sed -i "s|YourAwesomeStr0ngP4zzw0rd|$SMTP_PASSWORD|g" /etc/msmtprc
# Set up php sendmail config
RUN echo "sendmail_path=/usr/bin/msmtp -t" >> /etc/php/7.3/apache2/conf.d/php-sendmail.ini
Add a msmtprc
file to your docker build context:
account default
host mail.yoursmtpserver.com
port 587
tls on
tls_starttls on
tls_trust_file /etc/ssl/certs/ca-certificates.crt
tls_certcheck on
auth on
user [email protected]
password "YourAwesomeStr0ngP4zzw0rd"
from "[email protected]"
logfile /var/log/msmtp.log
note: Some changes were made in order to make it work with my particular setup (branching FROM eboraas/apache-php
). This applies particularily to the lines:
- ARG SMTP_PASSWORD=not_provided
- RUN chown www-data:www-data /etc/msmtprc
- RUN sed -i "s|YourAwesomeStr0ngP4zzw0rd|$SMTP_PASSWORD |g" /etc/msmtprc
- RUN echo "sendmail_path=/usr/bin/msmtp -t" >> /etc/php/7.3/apache2/conf.d/php-sendmail.ini
You may need to adapt paths, passwords and so on to fit your needs. Keep in mind to set the SMTP_PASSWORD
build argument from environment (e.g. SMTP_PASSWORD=<secret> docker-compose build
) if you want to use this solution straight away.
Useful resources:
- https://wiki.debian.org/msmtp
- https://owendavies.net/articles/setting-up-msmtp/
- https://wiki.archlinux.org/index.php/msmtp#Send_mail_with_PHP_using_msmtp
- linux msmtp configuration sends from shell but fails from PHP/apache
ratm
Updated on July 11, 2022Comments
-
ratm almost 2 years
I'm on ubuntu 16.04. I have a (testing) docker (docker-compose) container running php 5.6 and apache 2.4.
On the production platform (without docker) the mail is sent with sendmail.
How to send test email on docker container (with sendmail)?
Thanks in advance for responses.
-
laimison about 5 yearsThat worked well. A note that in majority of cases people want to do
RUN echo "FromLineOverride=YES" >> /etc/ssmtp/ssmtp.conf
as well. So this allows you to set your own "From: " name and address in mail clients/scripts instead of getting default one, such as "From: www-data...". -
pableiros almost 4 yearsI had to add
RUN echo "localhost localhost.localdomain" >> /etc/hosts
at the end to make it work. Also I had to give Non secure apps permissions to my gmail account myaccount.google.com/lesssecureapps -
PHZ.fi-Pharazon almost 3 yearsI had to put ssmtp without -t -flag to get rid off error:
sendmail: recipients with -t option not supported
i.e. to php.inisendmail_path=sendmail -i
-
moonbas3 almost 3 yearsOr enable the Universe repo so you can install SSMTP.