Send email on testing docker container with php and sendmail Send email on testing docker container with php and sendmail docker docker

Send email on testing docker container with php and sendmail


It works.

In Dockerfile :

# sendmail config############################################RUN apt-get install -q -y ssmtp mailutils# root is the person who gets all mail for userids < 1000RUN echo "root=yourAdmin@email.com" >> /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.confRUN echo "AuthUser=your@gmail.com" >> /etc/ssmtp/ssmtp.confRUN echo "AuthPass=yourGmailPass" >> /etc/ssmtp/ssmtp.confRUN echo "UseTLS=YES" >> /etc/ssmtp/ssmtp.confRUN echo "UseSTARTTLS=YES" >> /etc/ssmtp/ssmtp.conf# Set up php sendmail configRUN 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" mailSendingAdresse@email.com

I succeed with the help of this two documents :


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# installRUN apt-get install -q -y msmtp mailutils# configCOPY msmtprc /etc/msmtprcRUN chmod 600 /etc/msmtprcRUN chown www-data:www-data /etc/msmtprcARG SMTP_PASSWORD=not_providedRUN sed -i "s|YourAwesomeStr0ngP4zzw0rd|$SMTP_PASSWORD|g" /etc/msmtprc# Set up php sendmail configRUN 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 defaulthost mail.yoursmtpserver.comport 587tls ontls_starttls ontls_trust_file /etc/ssl/certs/ca-certificates.crttls_certcheck onauth onuser my@mail.compassword "YourAwesomeStr0ngP4zzw0rd"from "my@mail.com"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: