将 GCP Compute Engine 与 Google Suite SMTP 中继服务链接

将 GCP Compute Engine 与 Google Suite SMTP 中继服务链接

最近我们想将 GCP Compute Engine 与 Google Suite 电子邮件服务连接起来。此时文档告诉

Google Compute Engine 不允许在端口 25 上进行出站连接。默认情况下,出站 SMTP 会被阻止,因为服务器到服务器的 SMTP 容易受到大量滥用。此外,拥有可信赖的第三方提供商(例如 SendGrid、Mailgun 或 Mailjet)可减轻 Compute Engine 和您维护接收方 IP 声誉的负担。

我们的想法是,我们有 php 脚本并在 httpd/apache 上运行,它具有mail向我们的销售团队发送电子邮件的功能,如下所示

$mail = mail($siteOwnersEmail, $subject, $message, $headers);

并使用 sendmail 中继到 smtp google 服务。我们的计算引擎在 Centos 7 上运行。我们不考虑其他值得信赖的第三方作为 Google 推荐,例如SendGridMailGun和,MailJet因为我们已经使用了 Google Suite。

来自另一个邮政,连接 Compute Engine 和 Google Suite 非常困难。我们需要您的帮助来实现这一点。

我们已经配置了 sendmail,如下所示

divert(-1)dnl
include(`/usr/share/sendmail-cf/m4/cf.m4')dnl
VERSIONID(`setup for linux')dnl
OSTYPE(`linux')dnl
dnl define(`confSMTP_LOGIN_MSG', `$j Sendmail; $b')dnl
dnl define(`confLOG_LEVEL', `9')dnl
dnl define(`SMART_HOST', `relay-smtp.gmail.com')dnl
define(`confDEF_USER_ID', ``8:12'')dnl
dnl define(`confAUTO_REBUILD')dnl
define(`confTO_CONNECT', `1m')dnl
define(`confTRY_NULL_MX_LIST', `True')dnl
define(`confDONT_PROBE_INTERFACES', `True')dnl
define(`PROCMAIL_MAILER_PATH', `/usr/bin/procmail')dnl
define(`ALIAS_FILE', `/etc/aliases')dnl
define(`STATUS_FILE', `/var/log/mail/statistics')dnl
define(`UUCP_MAILER_MAX', `2000000')dnl
define(`confUSERDB_SPEC', `/etc/mail/userdb.db')dnl
define(`confPRIVACY_FLAGS', `authwarnings,novrfy,noexpn,restrictqrun')dnl
define(`confAUTH_OPTIONS', `A')dnl
dnl define(`RELAY_MAILER_ARGS', `TCP $h 587′)dnl
dnl define(`ESMTP_MAILER_ARGS', `TCP $h 587′)dnl
dnl define(`confAUTH_OPTIONS', `A p')dnl
dnl TRUST_AUTH_MECH(`EXTERNAL DIGEST-MD5 CRAM-MD5 LOGIN PLAIN')dnl
dnl define(`confAUTH_MECHANISMS', `EXTERNAL GSSAPI DIGEST-MD5 CRAM-MD5 LOGIN PLAIN')dnl
dnl define(`confCACERT_PATH', `/etc/pki/tls/certs')dnl
dnl define(`confCACERT', `/etc/pki/tls/certs/ca-bundle.crt')dnl
dnl define(`confSERVER_CERT', `/etc/pki/tls/certs/sendmail.pem')dnl
dnl define(`confSERVER_KEY', `/etc/pki/tls/certs/sendmail.pem')dnl
dnl define(`confDONT_BLAME_SENDMAIL', `groupreadablekeyfile')dnl
dnl define(`confTO_QUEUEWARN', `4h')dnl
dnl define(`confTO_QUEUERETURN', `5d')dnl
dnl define(`confQUEUE_LA', `12')dnl
dnl define(`confREFUSE_LA', `18')dnl
define(`confTO_IDENT', `0')dnl
dnl define(`confINET_QOS', `AF11')dnl
dnl FEATURE(delay_checks)dnl
FEATURE(`no_default_msa', `dnl')dnl
FEATURE(`smrsh', `/usr/sbin/smrsh')dnl
FEATURE(`mailertable', `hash -o /etc/mail/mailertable.db')dnl
FEATURE(`virtusertable', `hash -o /etc/mail/virtusertable.db')dnl
FEATURE(redirect)dnl
FEATURE(always_add_domain)dnl
FEATURE(use_cw_file)dnl
FEATURE(use_ct_file)dnl
dnl define(`confMAX_DAEMON_CHILDREN', `20')dnl
dnl define(`confCONNECTION_RATE_THROTTLE', `3')dnl
FEATURE(local_procmail, `', `procmail -t -Y -a $h -d $u')dnl
FEATURE(`access_db', `hash -T<TMPF> -o /etc/mail/access.db')dnl
FEATURE(`blacklist_recipients')dnl
EXPOSED_USER(`root')dnl
dnl define(`confLOCAL_MAILER', `cyrusv2')dnl
dnl define(`CYRUSV2_MAILER_ARGS', `FILE /var/lib/imap/socket/lmtp')dnl
DAEMON_OPTIONS(`Port=smtp,Addr=127.0.0.1, Name=MTA')dnl
dnl DAEMON_OPTIONS(`Port=submission, Name=MSA, M=Ea')dnl
dnl DAEMON_OPTIONS(`Port=smtps, Name=TLSMTA, M=s')dnl
dnl DAEMON_OPTIONS(`port=smtp,Addr=::1, Name=MTA-v6, Family=inet6')dnl
dnl DAEMON_OPTIONS(`Name=MTA-v4, Family=inet, Name=MTA-v6, Family=inet6')
FEATURE(`accept_unresolvable_domains')dnl
dnl FEATURE(`relay_based_on_MX')dnl
LOCAL_DOMAIN(`localhost.localdomain')dnl
dnl MASQUERADE_AS(`qreatiq.com')dnl
dnl FEATURE(masquerade_envelope)dnl
dnl FEATURE(masquerade_entire_domain)dnl
dnl MASQUERADE_DOMAIN(localhost)dnl
dnl MASQUERADE_DOMAIN(localhost.localdomain)dnl
dnl MASQUERADE_DOMAIN(mydomainalias.com)dnl
dnl MASQUERADE_DOMAIN(mydomain.lan)dnl
MAILER(smtp)dnl
MAILER(procmail)dnl
dnl MAILER(cyrusv2)dnl

并已将 SELinux 设置为如下形式

setsebool httpd_can_sendmail on 

生成证书

cd /etc/pki/tls/certs
make sendmail.pem

创建用于身份验证的身份验证文件smtp.gmail.com

mkdir /etc/mail/auth
chmod 700 /etc/mail/auth 

创建客户信息文件

AuthInfo:smtp.gmail.com “U:smmsp” “I:gmail_address” “P:password” “M:PLAIN”
AuthInfo:smtp.gmail.com:587 “U:smmsp” “I:gmail_address” “P:password” “M:PLAIN”

和做

makemap -r hash client-info.db < client-info
chmod 600 client-info client-info.db

编辑别名

root:email_address

apache在受信任用户处添加用户并运行newaliases

然后我尝试使用此代码发送电子邮件

echo ‘this is a test’| mail -s test_email [email protected]

并且这里的 cek 日志/var/log/maillog状态deferred because connection timeout with aspmx2.googlemail.com是完整的堆栈跟踪

Aug 30 06:54:03 gcp***-***a sendmail[14979]: w7U6s31m014979: to=system@*****.com, ctladdr=a*****g_imannuel (1001/1002), delay=00:00:00, xdelay=00:00:00, mailer=relay, pri=30234, relay=[127.0.0.1] [127.0.0.1], dsn=2.0.0, stat=Sent (w7U6s3QN014980 Message accepted for delivery)
Aug 30 06:59:03 gcp***-***a sendmail[14982]: w7U6s3QN014980: to=<system@*****.com>, ctladdr=<a*****g_imannuel@gcp***-***a.c.calm-ca***-XXX.internal> (1001/1002), delay=00:05:00, xdelay=00:05:00, mailer=esmtp, pri=120537, relay=aspmx2.googlemail.com. [173.194.66.26], dsn=4.0.0, stat=Deferred: Connection timed out with aspmx2.googlemail.com.

并且仅在 ssh 启动会话时记录在 /var/log/messages 中。

此外,我们的 Google Suite 还提供 SMTP 中继服务

Allowed senders: Any addresses (not recommended)
Only accept mail from the specified IP addresses: Yes
Allowed IP addresses: www.****.com
Require SMTP Authentication: Yes
Require TLS encryption: No

检查了 Google Suite - Gmail 应用,电子邮件始终未发送给发件人。太遗憾了。有人知道问题出在哪里吗?

答案1

在这里使用 sendmail 会使事情变得过于复杂(而且可能仍然需要使用端口 25)。只需按照以下说明操作即可:https://support.google.com/a/answer/2956491?hl=en设置 GSuite 来中继电子邮件、使用身份验证并使用端口 587 发送。

您也不能使用内置的 PHP mail() 函数(可能没人会用它)。请改用 PEAR::Mail:http://pear.php.net/manual/en/package.mail.mail.php- 该包允许您配置主机/端口等。

相关内容