使用 gmail 发送 phpmailer smtp 电子邮件需要很长时间(1.5 秒)

use*_*531 6 php email gmail smtp phpmailer

以下脚本大约需要 1.5 秒来发送电子邮件,这是由$mail->send()线路上的计时器测量的。如果我不使用 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);
}

?>
Run Code Online (Sandbox Code Playgroud)

小智 5

而不是使用这个:

$mail->IsSMTP();
Run Code Online (Sandbox Code Playgroud)

用这个:

$mail->IsMail();
Run Code Online (Sandbox Code Playgroud)

我也遇到了同样的问题,SMTP 确实会降低你的速度。

  • PHP Mailer将不再保留SMTP,它将成为正常的PHP Mail功能,您可以签入已发送的邮件,并且可以在电子邮件源中查看gmail登录签名。所以如果你想使用 SMTP 那么不要使用 $mail-&gt;IsMail(); (2认同)

Syn*_*hro 4

当您在 PHPMailer 中使用isMail()isSendmail()传输选项时,它不会立即发送消息,而是将其提交到本地邮件服务器,这会释放您的 Web 应用程序并在空闲时发送消息。这通常不会产生网络开销,不需要加密或身份验证,并且如果它是低流量服务器,它可能几乎不做其他事情并且可以很快接受消息。

SMTP 从来就不是真正用于交互使用的,即在提交网页期间使用,而且它确实需要很长时间。这是一个复杂的协议,有许多往返和可能延迟的点,特别是可能出现灰名单、问候延迟、立即垃圾邮件过滤等。

如果您想使用 SMTP 并使其速度更快,请使用附近的邮件服务器(甚至是本地主机)作为中继,该中继不需要加密或身份验证,并且不对出站邮件应用垃圾邮件过滤。