根据 $mail->send()
行中的计时器测量,以下脚本发送一封电子邮件大约需要 1.5 秒。如果我不使用 smtp,速度会快得多,但是,一些邮件服务器会阻止传入的电子邮件。
是什么导致了延迟?如果对此无能为力,那么避免用户等待的好的解决方案是什么?
<?php
require_once ('../../application/classes_3rd/PHPMailer/PHPMailerAutoload.php');
class myPHPMailer extends PHPMailer
{
public function __construct()
{
$this->isSMTP();
$this->SMTPDebug = 0;
$this->Host = 587;
$this->Port = "smtp.gmail.com";
$this->SMTPSecure="tls";
$this->SMTPAuth = true;
$this->Username = "example@gmail.com";
$this->Password = "my_password";
}
}
try {
$mail = new myPHPMailer(true);
$mail->AddReplyTo('me@example.com');
$mail->SetFrom('me@example.com');
$mail->AddAddress('me@example.com', 'John Doe');
$mail->Subject = "My subject";
$mail->MsgHTML("Hello! Click this link https://www.google.com/");
$time=microtime(1);
$mail->Send();
echo(microtime(1)-$time);
} catch (phpmailerException $e) {
trigger_error($e->errorMessage(), E_USER_ERROR);
}
?>
最佳答案
当您在 PHPMailer 中使用 isMail()
或 isSendmail()
传输选项时,它不会立即发送消息,而是将其提交到您的本地邮件服务器,这会释放您的网络应用程序并在闲暇时发送消息。这通常不会产生网络开销,没有加密或身份验证,如果它是一个低流量服务器,它可能不会做其他事情并且可以很快接受消息。
SMTP 从未真正打算用于交互式使用,即在提交网页期间使用,而且它确实需要很长时间。这是一个复杂的协议(protocol),有许多往返和可能延迟的点,特别是灰色列表的可能性、问候延迟、即时垃圾邮件过滤等等。
如果您想使用 SMTP 并使其更快,请使用附近的邮件服务器(甚至是本地主机)作为不需要加密或身份验证并且不对出站邮件应用垃圾邮件过滤的中继。
关于php - 使用 gmail 发送 phpmailer smtp 电子邮件需要很长时间(1.5 秒),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27552252/