簡體   English   中英

PHPMailer使用nginx在我的Digital Ocean服務器上造成504超時錯誤

[英]PHPMailer is causing 504 timeout error on my Digital Ocean server using nginx

我正在我的服務器上的一個PHP頁面上安裝電子郵件發送功能。 我希望能夠指定要發送的Gmail帳戶,因此我使用的是PHPMailer。 但是,每次加載發送電子郵件的頁面時,我都會在大約30秒后收到504網關超時錯誤。 最終,電子郵件被發送(我在大約5分鍾后收到),但這是正常的嗎? 這是一個非常基本的文本電子郵件。

這是我發送電子郵件的代碼

require '../html/lib/phpmailer/PHPMailerAutoload.php';


//Create a new PHPMailer instance
$mail = new PHPMailer;

//Tell PHPMailer to use SMTP
$mail->isSMTP();

//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 2;

//Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';

//Set the hostname of the mail server
$mail->Host = 'smtp.gmail.com';

//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
$mail->Port = 587;

//Set the encryption system to use - ssl (deprecated) or tls
$mail->SMTPSecure = 'tls';

//Whether to use SMTP authentication
$mail->SMTPAuth = true;

//Username to use for SMTP authentication - use full email address for gmail
$mail->Username = "user@gmail.com";

//Password to use for SMTP authentication
$mail->Password = "pw";

//Set who the message is to be sent from
$mail->setFrom('from@gmail.com', 'First Last');

//Set an alternative reply-to address
//$mail->addReplyTo('@example.com', 'First Last');

//Set who the message is to be sent to
$mail->addAddress('recip@gmail.com', 'recip');
$mail->Subject = 'PHPMailer GMail SMTP test 2';

//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
//$mail->msgHTML(file_get_contents('contents.html'), dirname(__FILE__));

//Replace the plain text body with one created manually
$mail->Body = 'This is another plain-text message body';

//Attach an image file
//$mail->addAttachment('images/phpmailer_mini.png');

//send the message, check for errors
if (!$mail->send()) {
    echo "Mailer Error: " . $mail->ErrorInfo;
} else {
    echo "Message sent!";
}

SMTP超時很長(至少5分鍾)。 你得到的504是因為nginx和你的PHP cgi之間的超時(我假設你正在運行FPM)更短,所以當PHP生成錯誤時,nginx已經放棄了連接,所以你是得不到反饋。

這很可能是您主機上的DNS或防火牆問題 - 請查看故障排除文檔

Digital Ocean在其支持中心指出如何啟用像smtp這樣的服務來優先考慮IPv4上的連接,同時仍然為您的Droplet維護可訪問的IPv6功能。

您可以優先使用IPv6上的IPv4地址,以便您可以繼續發送電子郵件而不禁用IPv6。 您可以通過編輯Droplet的/etc/gai.conf文件並從以下行刪除注釋(#)來實現:

默認配置:#precedence :: ffff:0:0/96 100

IPv4優先級配置:precedence :: ffff:0:0/96 100

這已被我檢查並確認為PHPMailer問題工作,其中請求將超時(504網關超時)但郵件仍最終交付(Ubuntu 16.04 LEMP)。

這是因為Ngninx放棄了與PHP-FPM的連接,而php-script仍然在后台運行,嘗試解析IPv6 SMTP地址,最后在沒有成功的情況下轉移到IPv4。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM