简体   繁体   English

使用来自 Docker 容器的 PHP sendmail 通过主机 Postfix 发送

[英]Use PHP sendmail from Docker container to send through host Postfix

I want to send mail from my Alpine-PHP-Fpm container using my host postfix installation.我想使用我的主机 postfix 安装从我的 Alpine-PHP-Fpm 容器发送邮件。

RECAP回顾

PHP-Fpm Container -> Sendmail -> PostFix on host -> Sending via SMTP PHP-Fpm 容器 -> Sendmail -> 主机上的 PostFix -> 通过 SMTP 发送

But I get但我得到

sendmail: can't connect to remote host (127.0.0.1): Connection refused sendmail:无法连接到远程主机(127.0.0.1):连接被拒绝

Here is the Postfix config:这是后缀配置:

smtpd_banner = $myhostname ESMTP $mail_name (Ubuntu)
biff = no
append_dot_mydomain = no
readme_directory = no

smtpd_tls_cert_file=/etc/ssl/certs/ssl-cert-snakeoil.pem
smtpd_tls_key_file=/etc/ssl/private/ssl-cert-snakeoil.key
smtpd_use_tls=yes
smtpd_tls_session_cache_database = btree:${data_directory}/smtpd_scache
smtp_tls_session_cache_database = btree:${data_directory}/smtp_scache

smtpd_relay_restrictions = permit_mynetworks permit_sasl_authenticated defer_unauth_destination
myhostname = MYDOMAIN.com
alias_maps = hash:/etc/aliases
alias_database = hash:/etc/aliases
myorigin = /etc/mailname
mydestination = localhost.com, localhost

# SMTP authentication settings
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = static:no-reply@MYDOMAIN.com:SECRETPSW
smtp_sasl_security_options = noanonymous
smtp_tls_security_level = may
#header_size_limit = 4096000
relay_destination_concurrency_limit = 20

# Limit DOS Attacks
default_process_limit = 100
smtpd_client_connection_count_limit = 10
smtpd_client_connection_rate_limit = 30
queue_minfree = 20971520
header_size_limit = 51200
message_size_limit = 10485760
smtpd_recipient_limit = 100

# Default relayhost setting
relayhost = smtp.MYDOMAIN.com

mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128 172.18.0.3 # Fpm container IP
mailbox_command = procmail -a "$EXTENSION"
mailbox_size_limit = 0
recipient_delimiter = +
inet_interfaces = 172.18.0.1 # Docker0 bridge IP
inet_protocols = all

And here is the php.ini mail section:这是 php.ini 邮件部分:

[mail function]
SMTP = localhost
smtp_port = 25
sendmail_path = "/usr/sbin/sendmail -t -i"
mail.add_x_header = On

Are there any ports that I need to open in my docker-compose file or in the host firewall settings?是否需要在 docker-compose 文件或主机防火墙设置中打开任何端口?

I'm not sure about using the host's config/postfix however I can suggest an alternative solution that may work just as well.我不确定是否使用主机的配置/后缀,但是我可以建议一个可能同样有效的替代解决方案。 Add a postfix container and copy the configs from the host to the container:添加一个 postfix 容器并将配置从主机复制到容器:

services:
  application:
    image: some-company/some-application
    environment:
      - APPLICATION_ENV
    ports:
      - 80:80

  smtp:
    image: namshi/smtp
    environment:
      - MAILNAME
      - SMARTHOST_USER
      - SMARTHOST_PASSWORD
    volumes:
      - /etc/postfix/main.cf:/etc/postfix/main.cf
    ports:
      - 25:25

Then you can use smtp as your mailer host / transport and the user/pass from the environment.然后您可以使用smtp作为您的邮件主机/传输和来自环境的用户/通行证。

I am not a container expert, but if I understood correctly, you are trying to acccess 127.0.0.1 within the container (php.ini localhost config resolves to 127.0.0.1), and I assume that is not the hosts address, it points to the container internals.我不是容器专家,但如果我理解正确,您正在尝试访问容器内的 127.0.0.1(php.ini localhost 配置解析为 127.0.0.1),我认为这不是主机地址,它指向容器内部。

Figure out the host IP and change in the php.ini:找出主机IP并在php.ini中更改:

[mail function]
SMTP = host_address
smtp_port = 25

The best solution which I have found is to install msmtp and add the following to the php.ini我发现的最佳解决方案是安装 msmtp 并将以下内容添加到 php.ini

sendmail_path = /usr/bin/msmtp -t  --host host.docker.internal --from php@docker

Now your mail will be received by your mail server (smpt4dev or mailh现在您的邮件将被您的邮件服务器(smpt4dev 或 mailh)接收

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 php 和 sendmail 发送有关测试 docker 容器的电子邮件 - Send email on testing docker container with php and sendmail 如何使用LAMPP,PHP和Sendmail / Postfix将电子邮件从gmail帐户发送到其他帐户? - How to send email from gmail account to other account using LAMPP, PHP and Sendmail/Postfix? PHP通过后缀发送邮件 - PHP send mail through postfix Docker PHP / Apache容器-尝试发送邮件时,Sendmail 553错误“发件人地址的域不存在” - Docker PHP / Apache Container - Sendmail 553 Error “Domain of Sender Address does not exist” when trying to send mail 设置从主机到docker容器的PHP路径 - set PHP path from host to docker container 任何人都可以通过 AWS LAMP 服务器使用后缀发送邮件到 gmail,sendmail - Is anybody able to send mails to gmail through AWS LAMP server using postfix, sendmail PHP,postfix,sendmail,thunderbird仅适用于本地开发人员 - PHP, postfix, sendmail, thunderbird for local dev only 在Mac OS X上,php邮件真的很慢,如何直接使用postfix / sendmail? - php mail really slow on mac os x, how do I use postfix/sendmail directly? Sendmail和Postfix不从CentOS服务器发送邮件? - Sendmail & Postfix not sending mail from CentOS server? 通过运行后缀的单独电子邮件服务器从php应用程序发送电子邮件 - Send email from php application through a separate email server running postfix
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM