在我运行的服务器上两个都docker 镜像和 postfix smtp 服务器。smtp 服务器可通过 localhost 访问,并且不是安装在任何类型的容器中。
问题是我可以使用 sendemail 通过终端发送电子邮件外部但我无法通过在任何容器内运行 sendemail 的终端发送电子邮件。
Postfix配置如下:
# See /usr/share/postfix/main.cf.dist for a commented, more complete version
# Debian specific: Specifying a file name will cause the first
# line of that file to be used as the name. The Debian default
# is /etc/mailname.
#myorigin = /etc/mailname
smtpd_banner = $myhostname ESMTP $mail_name (Debian/GNU)
biff = no
# appending .domain is the MUA's job.
append_dot_mydomain = no
# Uncomment the next line to generate "delayed mail" warnings
#delay_warning_time = 4h
readme_directory = no
# TLS parameters
smtpd_tls_cert_file=/etc/letsencrypt/live/ellak.org/fullchain.pem
smtpd_tls_key_file= /etc/letsencrypt/live/ellak.org/privkey.pem
smtpd_use_tls=yes
smtpd_tls_session_cache_database = btree:${data_directory}/smtpd_scache
smtp_tls_session_cache_database = btree:${data_directory}/smtp_scache
# See /usr/share/doc/postfix/TLS_README.gz in the postfix-doc package for
# information on enabling SSL in the smtp client.
smtpd_relay_restrictions = permit_mynetworks permit_sasl_authenticated defer_unauth_destination
myhostname = guest8.ellak.gr
alias_maps = hash:/etc/aliases
alias_database = hash:/etc/aliases
myorigin = /etc/mailname
mydestination = mail.ellak.org, guest8.ellak.gr, localhost.ellak.gr, localhost
#Virtual alias domains
virtual_alias_domains = ellak.org
# RELAY Options
relayhost = [mail1.ellak.gr]:587
smtp_sasl_auth_enable = yes
smtp_sasl_security_options = noanonymous
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_use_tls = yes
smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt
local_recipient_maps = proxy:unix:passwd.byname $alias_maps
smtp_tls_note_starttls_offer = yes
#Network Access Options
mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128 172.17.0.0/16
mailbox_size_limit = 0
recipient_delimiter = +
inet_interfaces = all
relay_domains = *
在容器内我运行:
并输出以下错误:
Nov 27 19:51:14 7e56b4e22e09 sendemail[1988]: WARNING => The recipient <[email protected]> was rejected by the mail server, error follows:
Nov 27 19:51:14 7e56b4e22e09 sendemail[1988]: WARNING => Received: 454 4.7.1 <[email protected]>: Relay access denied
Nov 27 19:51:14 7e56b4e22e09 sendemail[1988]: ERROR => Exiting. No recipients were accepted for delivery by the mail server.
在 Postfix 日志中我得到以下信息:
Nov 27 21:42:54 guest8 postfix/smtpd[14979]: NOQUEUE: reject: RCPT from unknown[172.18.0.5]: 454 4.7.1 <[email protected]>: Relay access denied; from=<[email protected]> to=<[email protected]> proto=ESMTP helo=<7e56b4e22e09>
Nov 27 21:42:54 guest8 postfix/smtpd[14979]: lost connection after RCPT from unknown[172.18.0.5]
Nov 27 21:42:54 guest8 postfix/smtpd[14979]: disconnect from unknown[172.18.0.5]
Nov 27 21:46:14 guest8 postfix/anvil[14982]: statistics: max connection rate 2/60s for (smtp:172.18.0.5) at Nov 27 21:42:54
Nov 27 21:46:14 guest8 postfix/anvil[14982]: statistics: max connection count 1 for (smtp:172.18.0.5) at Nov 27 21:42:41
Nov 27 21:46:14 guest8 postfix/anvil[14982]: statistics: max cache size 1 at Nov 27 21:42:41
您是否知道如何在 Docker 容器内发送电子邮件?
答案1
172.18.0.5 不包含在
mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128 172.17.0.0/16
mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128 172.16.0.0/12
这包括所有私有 172.16 子网。
答案2
ssmtp
仅适用于 Debian Bullseye 及更高版本,在 11 之前你必须配置msmtp
。
在主机上,将 docker 子网(
172.0.0.0/8
默认)添加到 postfix 的配置中/etc/postfix/main.cf
。mynetworks
键值已经有值了,将新的子网范围添加到末尾:mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128 172.0.0.0/8
使用 重新启动 postfix
systemctl restart postfix
。更新您的
dockerfile
以添加所需的软件包:RUN apt-get install -y mailutils msmtp msmtp-mta
该
msmtp-mta
包是必需的,因为它为sendmail
创建
msmtprc
配置文件:account default host host.docker.internal
将其添加到你的dockerfile中:
COPY ./msmtprc /etc/msmtprc
你必须添加
docker-compose.yaml
文件的额外主机因为它们在 Linux 上默认没有添加:extra_hosts: - host.docker.internal:host-gateway
使用以下命令测试邮件命令
docker compose exec [service] echo "Hello, world!" | mail -s 'test' [email]