Smtp

將 GCP Compute Engine 與 Google Suite SMTP 中繼服務關聯起來

  • August 31, 2018

最近我們想將 GCP Compute Engine 與 Google 套件電子郵件服務連接起來。在這個文件中告訴

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 推薦,例如SendGrid, MailGunMailJet因為我們已經使用了 Google Suite。

從另一篇文章來看,連接計算引擎和Google套件很難。我們需要您的幫助來實現這一目標。

我們已經將 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 system@qreatiq.com

/var/log/maillog此處狀態的cek 日誌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.

並在 /var/log/messages 僅記錄為 ssh 啟動的會話。

此外,這是我們 Google 套件中的 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 應用程序,電子郵件永遠不會發送給發件人。好難過。有誰知道是什麼問題?

在此處使用 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 - 該包允許您配置主機/埠等。

引用自:https://serverfault.com/questions/928657