我正在服务器上的一个 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 或防火墙问题 - 查看 the troubleshooting docs .
关于PHPMailer 在我使用 nginx 的 Digital Ocean 服务器上导致 504 超时错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27945676/