【问题标题】:Send email on testing docker container with php and sendmail使用 php 和 sendmail 发送关于测试 docker 容器的电子邮件
【发布时间】:2018-04-25 04:02:43
【问题描述】:

我在 ubuntu 16.04 上。我有一个运行 php 5.6 和 apache 2.4 的(测试)docker(docker-compose)容器。

在生产平台(没有 docker)上,邮件使用 sendmail 发送。

如何在 docker 容器上发送测试邮件(使用 sendmail)?

提前感谢您的回复。

【问题讨论】:

  • 试试nullmailer 包。真的很容易配置,而且它的工作。

标签: php docker docker-compose sendmail php-5.6


【解决方案1】:

有效。

在 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

在这两个文件的帮助下我成功了:

【讨论】:

  • 效果很好。请注意,在大多数情况下,人们也想做RUN echo "FromLineOverride=YES" &gt;&gt; /etc/ssmtp/ssmtp.conf。因此,这允许您在邮件客户端/脚本中设置自己的“发件人:”名称和地址,而不是获取默认值,例如“发件人:www-data...”。
  • 我必须在末尾添加RUN echo "localhost localhost.localdomain" &gt;&gt; /etc/hosts 才能使其正常工作。我还必须为我的 gmail 帐户 myaccount.google.com/lesssecureapps 授予非安全应用程序权限
  • 我不得不把 ssmtp 不带 -t -flag 来摆脱错误:sendmail: recipients with -t option not supported 即到 php.ini sendmail_path=sendmail -i
【解决方案2】:

如果它说:

“包'ssmtp'没有安装候选”

您可以改用 msmtp

将以下内容添加到您的 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

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)。这尤其适用于以下行:

  • ARG SMTP_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 构建参数(例如 SMTP_PASSWORD=&lt;secret&gt; docker-compose build)。

有用的资源:

【讨论】:

  • 或者启用 Universe 存储库,以便您可以安装 SSMTP。
猜你喜欢
  • 2017-01-20
  • 2011-03-03
  • 1970-01-01
  • 2018-07-22
  • 2013-11-22
  • 2013-12-22
  • 1970-01-01
  • 2010-12-10
  • 1970-01-01
相关资源
最近更新 更多