使用 php 和 sendmail 在测试 docker 容器上发送电子邮件
Send email on testing docker container with php and sendmail
我在 ubuntu 16.04。我有一个(测试)docker (docker-compose) 容器 运行 php 5.6 和 apache 2.4.
在生产平台上(没有docker)邮件是用sendmail发送的。
如何在 docker 容器上发送测试邮件(使用 sendmail)?
提前感谢您的回复。
有效。
在 Dockerfile 中:
# sendmail config
############################################
RUN apt-get install -q -y ssmtp mailutils
# root is the person who gets all mail for userids < 1000
RUN 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.conf
RUN echo "AuthUser=your@gmail.com" >> /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
用于在 php sendmail 容器内进行测试:
echo "Un message de test" | mail -s "sujet de test" mailSendingAdresse@email.com
我在这两份文件的帮助下成功了:
如果它说:
"Package 'ssmtp' has no installation candidate"
您可以使用 msmtp。
将以下内容添加到您的docker文件
# 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
将 msmtprc
文件添加到您的 docker 构建上下文:
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 my@mail.com
password "YourAwesomeStr0ngP4zzw0rd"
from "my@mail.com"
logfile /var/log/msmtp.log
注意:进行了一些更改以使其适用于我的特定设置(分支 FROM eboraas/apache-php
)。这尤其适用于以下行:
- ARGSMTP_PASSWORD=not_provided
- 运行 chown www-data:www-data /etc/msmtprc
- 运行 sed -i "s|YourAwesomeStr0ngP4zzw0rd|$SMTP_PASSWORD |g" /etc/msmtprc
- 运行 echo "sendmail_path=/usr/bin/msmtp -t" >> /etc/php/7.3/apache2/conf.d/php-sendmail.ini
您可能需要调整路径、密码等以满足您的需要。如果您想立即使用此解决方案,请记住从环境(例如 SMTP_PASSWORD=<secret> docker-compose build
)设置 SMTP_PASSWORD
构建参数。
有用的资源:
- 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
我在 ubuntu 16.04。我有一个(测试)docker (docker-compose) 容器 运行 php 5.6 和 apache 2.4.
在生产平台上(没有docker)邮件是用sendmail发送的。
如何在 docker 容器上发送测试邮件(使用 sendmail)?
提前感谢您的回复。
有效。
在 Dockerfile 中:
# sendmail config
############################################
RUN apt-get install -q -y ssmtp mailutils
# root is the person who gets all mail for userids < 1000
RUN 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.conf
RUN echo "AuthUser=your@gmail.com" >> /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
用于在 php sendmail 容器内进行测试:
echo "Un message de test" | mail -s "sujet de test" mailSendingAdresse@email.com
我在这两份文件的帮助下成功了:
如果它说:
"Package 'ssmtp' has no installation candidate"
您可以使用 msmtp。
将以下内容添加到您的docker文件
# 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
将 msmtprc
文件添加到您的 docker 构建上下文:
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 my@mail.com
password "YourAwesomeStr0ngP4zzw0rd"
from "my@mail.com"
logfile /var/log/msmtp.log
注意:进行了一些更改以使其适用于我的特定设置(分支 FROM eboraas/apache-php
)。这尤其适用于以下行:
- ARGSMTP_PASSWORD=not_provided
- 运行 chown www-data:www-data /etc/msmtprc
- 运行 sed -i "s|YourAwesomeStr0ngP4zzw0rd|$SMTP_PASSWORD |g" /etc/msmtprc
- 运行 echo "sendmail_path=/usr/bin/msmtp -t" >> /etc/php/7.3/apache2/conf.d/php-sendmail.ini
您可能需要调整路径、密码等以满足您的需要。如果您想立即使用此解决方案,请记住从环境(例如 SMTP_PASSWORD=<secret> docker-compose build
)设置 SMTP_PASSWORD
构建参数。
有用的资源:
- 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