使用 WordPress 设置 Postfix 和 Google G Suite 电子邮件

使用 WordPress 设置 Postfix 和 Google G Suite 电子邮件

我终于使用 WP-CLI 设置好 WordPress 并在我的 VPS 上运行了。直到我想更改我的Administration Email Address发现Settings > General并从 ContactForm7 提交表单时,我才意识到我输入的电子邮件地址没有收到任何电子邮件。

我有一个通过 Namescheap 购买的公共域名,并且我有一个 Google G Suite(现为 Google Workspace)的电子邮件。在本文中,我将把我的公共域名称为 example.com。我的 Linux 服务器的 FQDN 为ubuntu-server.example.com,我的电子邮件的[email protected]别名为 email [email protected];。

我安装了 Apache 2.4,并选择使用 FPM 版本的 PHP,mod_php因为它速度更快,但占用更多内存。PHP 和 Apache 在 Linux 用户和组下运行www-data

我想安装 Postfix,因此按照各种指南后,我在 Google G Suite 中的 Gmail 上设置了“安全性较低的应用程序”,因为我启用了 2FA。

我未修改的PHP.ini文件,其中的 sendmail_path 进行了注释

/etc/php/7.4/fpm/php.ini

[mail function]
; For Win32 only.
; http://php.net/smtp
SMTP = localhost
; http://php.net/smtp-port
smtp_port = 25

; For Win32 only.
; http://php.net/sendmail-from
;sendmail_from = [email protected]

; For Unix only.  You may supply arguments as well (default: "sendmail -t -i").
; http://php.net/sendmail-path
;sendmail_path =

; Force the addition of the specified parameters to be passed as extra parameters
; to the sendmail binary. These parameters will always replace the value of
; the 5th parameter to mail().
;mail.force_extra_parameters =

; Add X-PHP-Originating-Script: that will include uid of the script followed by the filename
mail.add_x_header = Off

; The path to a log file that will log all mail() calls. Log entries include
; the full path of the script, line number, To address and headers.
;mail.log =
; Log mail to syslog (Event Log on Windows).
;mail.log = syslog

我的配置有问题,我一直在使用日志来/var/log/mail.log诊断问题。我发现了一篇非常有用的文章这里展示如何记录电子邮件标题。

/etc/postfix/header_checks

/^subject:/      WARN
/^to:/           WARN
/^from:/         WARN
/^Subject:/      WARN
/^To:/           WARN
/^From:/         WARN

/etc/postfix/main.cf

# 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 (Ubuntu)
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

# See http://www.postfix.org/COMPATIBILITY_README.html -- default to 2 on
# fresh installs.
compatibility_level = 2

# TLS parameters
smtpd_tls_cert_file=/etc/ssl/certs/ssl-cert-snakeoil.pem
smtpd_tls_key_file=/etc/ssl/private/ssl-cert-snakeoil.key
smtpd_tls_security_level=may

smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl/sasl_passwd
smtp_sasl_security_options = noanonymous
smtp_tls_security_level = encrypt
smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt
smtp_tls_CApath=/etc/ssl/certs
smtp_tls_session_cache_database = btree:${data_directory}/smtp_scache

smtpd_relay_restrictions = permit_mynetworks permit_sasl_authenticated defer_unauth_destination
myhostname = ubuntu-server.example.com
alias_maps = hash:/etc/aliases
alias_database = hash:/etc/aliases
myorigin = /etc/mailname
mydestination = $myhostname, example.com, ubuntu-server.example.com, localhost.example.com, localhost
relayhost = [smtp-relay.gmail.com]:587
mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128
mailbox_size_limit = 0
recipient_delimiter = +
inet_interfaces = all
inet_protocols = all

header_checks = regexp:/etc/postfix/header_checks

在/etc/postfix/sasl/sasl_passwd文件中

[smtp-relay.gmail.com]:587 [email protected]:password

只需在 WordPress 仪表板中更改Administration Email Address即可产生以下日志:

/var/log/mail.log(更改管理电子邮件地址)

Oct 14 20:34:50 ubuntu-server postfix/pickup[122612]: 615314047B: uid=33 from=<www-data>
Oct 14 20:34:50 ubuntu-server postfix/cleanup[122616]: 615314047B: warning: header To: [email protected] from local; from=<[email protected]>
Oct 14 20:34:50 ubuntu-server postfix/cleanup[122616]: 615314047B: warning: header Subject: [The Surge Network] New Admin Email Address from local; from=<[email protected]>
Oct 14 20:34:50 ubuntu-server postfix/cleanup[122616]: 615314047B: warning: header From: WordPress <[email protected]> from local; from=<[email protected]>
Oct 14 20:34:50 ubuntu-server postfix/cleanup[122616]: 615314047B: message-id=<[email protected]>
Oct 14 20:34:50 ubuntu-server postfix/qmgr[122614]: 615314047B: from=<[email protected]>, size=1000, nrcpt=1 (queue active)
Oct 14 20:34:50 ubuntu-server postfix/smtp[122618]: 615314047B: SASL authentication failed; server smtp-relay.gmail.com[74.125.133.28] said: 535-5.7.8 Username and Password not accepted. Learn more at?535 5.7.8  https://support.google.com/mail/?p=BadCredentials i127sm10296wmi.19 - gsmtp
Oct 14 20:34:50 ubuntu-server postfix/smtp[122618]: connect to smtp-relay.gmail.com[2a00:1450:400c:c08::1c]:587: Network is unreachable
Oct 14 20:34:50 ubuntu-server postfix/smtp[122618]: 615314047B: to=<[email protected]>, relay=none, delay=0.09, delays=0.01/0/0.08/0, dsn=4.4.1, status=deferred (connect to smtp-relay.gmail.com[2a00:1450:400c:c08::1c]:587: Network is unreachable)

正如我之前所说,Apache 和 PHP 在www-dataLinux 用户和组(其默认设置)下运行,这些在日志中出现得相当明显。有趣的是,如果我通过 ContactForm7 提交表单,它www-data似乎再次被否决。

ContactForm7 邮件设置

/var/log/mail.log(提交 CF7 表单)

Oct 14 21:09:04 ubuntu-server postfix/pickup[122612]: C31844047F: uid=33 from=<www-data>
Oct 14 21:09:04 ubuntu-server postfix/cleanup[123010]: C31844047F: warning: header To: [email protected] from local; from=<[email protected]>
Oct 14 21:09:04 ubuntu-server postfix/cleanup[123010]: C31844047F: warning: header Subject: =?us-ascii?Q?John_Smith_has_sumbitted_a_form_at_WordPress_Si?=  =?us-ascii?Q?te?= from local; from=<[email protected]>
Oct 14 21:09:04 ubuntu-server postfix/cleanup[123010]: C31844047F: warning: header From: Webmaster <[email protected]> from local; from=<[email protected]>
Oct 14 21:09:04 ubuntu-server postfix/cleanup[123010]: C31844047F: message-id=<[email protected]>
Oct 14 21:09:04 ubuntu-server postfix/qmgr[122614]: C31844047F: from=<[email protected]>, size=693, nrcpt=1 (queue active)
Oct 14 21:09:04 ubuntu-server postfix/smtp[123012]: C31844047F: SASL authentication failed; server smtp-relay.gmail.com[74.125.140.28] said: 535-5.7.8 Username and Password not accepted. Learn more at?535 5.7.8  https://support.google.com/mail/?p=BadCredentials b14sm13955wrm.42 - gsmtp
Oct 14 21:09:04 ubuntu-server postfix/smtp[123012]: connect to smtp-relay.gmail.com[2a00:1450:400c:c08::1c]:587: Network is unreachable
Oct 14 21:09:04 ubuntu-server postfix/smtp[123012]: C31844047F: to=<[email protected]>, relay=none, delay=0.14, delays=0.03/0.03/0.08/0, dsn=4.4.1, status=deferred (connect to smtp-relay.gmail.com[2a00:1450:400c:c08::1c]:587: Network is unreachable)

答案1

不要将系统主机名设置为裸域名。不要将 Postfix 的邮件名设置为裸域名。不要将裸域名添加到 Postfix 的 mydestination 或任何其他 Postfix 配置选项。执行任何这些操作都会导致 Postfix 尝试在本地传递邮件,而不是将其发送到互联网。

使用您的域名的子域来命名系统。将完全限定域名(包括子域)作为 Postfix 邮件名。例如,您可以调用您的系统,capacitorFQDN 将是capacitor.example.com。然后,example.com 的邮件将被发送到它应该到达的地方。并且只有发给某人 @capacitor.example.com 的邮件才会在本地发送(当然,除非您将其转发到某个地方)。

相关内容