2014-12-04 133 views
0

我有一個應用程序運行在EC2實例上。 在部分應用程序中,需要通過電子郵件向用戶發送通知。我使用PHP的本地mail()函數。郵件收到後期EC2 PHP

問題是,它似乎不是很穩定。我昨天做了一些測試,發現有些電子郵件是立即收到的,但有些似乎等了很長時間。

我在一分鐘內觸發了兩個通知,幾秒鐘後我收到第二個事件的通知,但第一個事件通知出現在我的收件箱中需要半小時。

下面是該回來晚的一個電子郵件標題:

Delivered-To: [email protected] 
Received: by 10.140.107.75 with SMTP id g69csp40419qgf; Wed, 3 Dec 2014 16:12:00 -0800 (PST) 
X-Received: by 10.194.108.162 with SMTP id hl2mr11475479wjb.102.1417651919645; Wed, 03 Dec 2014 16:11:59 -0800 (PST) 
Return-Path: <[email protected]> 
Received: from ip-xxx-xxx-xxx-xxx.localdomain (ec2-xx-xx-xx-xx.eu-west-1.compute.amazonaws.com. [xx.xx.xx.xx]) 
by mx.google.com with ESMTP id g6si19323730wjy.43.2014.12.03.16.11.59 
for <[email protected]>; Wed, 03 Dec 2014 16:11:59 -0800 (PST) 
Received-SPF: none (google.com: [email protected] does not designate permitted sender hosts) client-ip=xx.xx.xx.xx; 
Authentication-Results: mx.google.com; spf=none (google.com: [email protected] does not designate permitted sender hosts) [email protected]; dmarc=fail (p=NONE dis=NONE) header.from=gmail.com 
Received: by ip-172-31-19-221.localdomain (Postfix, from userid 48 id CABF521F29; Wed, 3 Dec 2014 23:38:35 +0000 (UTC) 
To: ---- <[email protected]> 
Subject: Test 
X-PHP-Originating-Script: 500:Notifier.class.php 
From: ---- <[email protected]> 
Message-Id: <[email protected]> 
Date: Wed, 3 Dec 2014 23:38:35 +0000 (UTC) 

我無法弄清楚是什麼原因造成這一點,所以如果任何人有一個想法,其中的問題可能是,答案是非常感謝!

+0

你意識到,PHP絕對沒有什麼與實際交付? php的工作就是把你的信件放在街角,並把它扔進郵箱。如果郵政服務很慢,那不是php的問題。 – 2014-12-04 17:19:08

回答

0

從下面的線,它看起來像你的PHP腳本在23時38分三十五秒+0000(UTC)創建的消息

Date: Wed, 3 Dec 2014 23:38:35 +0000 (UTC) 

然後從下面這條線,立即同一臺主機上運行的後綴確認收到來自PHP的消息(通過郵件()函數):

Received: by ip-172-31-19-221.localdomain (Postfix, from userid 48 id CABF521F29; Wed, 3 Dec 2014 23:38:35 +0000 (UTC) 

該問題似乎是在下一跳。它看起來像Postfix被配置爲通過亞馬遜的SMTP服務中繼郵件。然而,在調整了該時區differnces,Amazon的SMTP服務器未接收到消息,直到約33分鐘後:

Received: from ip-xxx-xxx-xxx-xxx.localdomain (ec2-xx-xx-xx-xx.eu-west-1.compute.amazonaws.com. [xx.xx.xx.xx])by mx.google.com with ESMTP id g6si19323730wjy.43.2014.12.03.16.11.59 

爲< ---- @ gmail.com>;星期三,2014年12月03日16:11:59 -0800(太平洋標準時間)

因此,延遲是在您的本地主機上的postfix將消息傳遞給亞馬遜的SMTP中繼服務器。亞馬遜的服務器很可能會延遲收到你的postfix服務器的郵件幾次,所以延遲可能是由於postfix必須重新發送郵件並重新嘗試多次,直到33分鐘後終於成功爲止。如果你能夠到達你的postfix服務器上的日誌,這些可能會對導致延遲的原因有所瞭解。

問題的一部分看起來可能是您將發件人地址設置在郵件的FROM:標題中,但您未設置信封發件人。它看起來像一個默認的信封發件人正在使用([email protected]),這顯然不是一個有效的電子郵件地址,它看起來像這可能會導致與SPF檢查問題。基於這個原因,亞馬遜的SMTP服務器可能會推遲發送郵件。您可能想要設置信封發件人,並查看是否可以解決問題。這句話應該訣竅:

$to = "[email protected]"; 
$from = "[email protected]"; 
$subject = "subject"; 
$message = "this is the message body"; 

$headers = "From: $from"; 
$ok = @mail($to, $subject, $message, $headers, "-f " . $from);