如何防止PHPMailer缓慢排队?

时间:2016-11-22 10:31:52

标签: php smtp phpmailer queuing

我注意到PHPMailer在排队发送电子邮件时会有一些减速。

例如,这里是发送几乎空邮件时的SMTPDebug 2级输出:

2016-11-22 09:53:47 SERVER -> CLIENT: 220 xxx.smtp.hotmail.com Microsoft ESMTP MAIL Service, Version: 8.0.9200.16384 ready at Tue, 22 Nov 2016 01:53:46 -0800 2016-11-22 09:53:47   CLIENT -> SERVER: EHLO localhost
2016-11-22 09:53:47 SERVER -> CLIENT: 250-BLU436-SMTP253.smtp.hotmail.com Hello [82.176.119.145] 250-TURN 250-SIZE 41943040 250-ETRN 250-PIPELINING 250-DSN 250-ENHANCEDSTATUSCODES 250-8bitmime 250-BINARYMIME 250-CHUNKING 250-VRFY 250-TLS 250-STARTTLS 250 OK
2016-11-22 09:53:47 CLIENT -> SERVER: STARTTLS
2016-11-22 09:53:47 SERVER -> CLIENT: 220 2.0.0 SMTP server ready
2016-11-22 09:53:48 CLIENT -> SERVER: EHLO localhost
2016-11-22 09:53:48 SERVER -> CLIENT: xxx.smtp.hotmail.com Hello [82.176.119.145] 250-TURN 250-SIZE 41943040 250-ETRN 250-PIPELINING 250-DSN 250-ENHANCEDSTATUSCODES 250-8bitmime 250-BINARYMIME 250-CHUNKING 250-VRFY 250-AUTH LOGIN PLAIN XOAUTH2 250 OK
2016-11-22 09:53:48 CLIENT -> SERVER: AUTH LOGIN
2016-11-22 09:53:48 SERVER -> CLIENT: 334 xxx
2016-11-22 09:53:48 CLIENT -> SERVER: xxx
2016-11-22 09:53:48 SERVER -> CLIENT: 334 xxx
2016-11-22 09:53:48 CLIENT -> SERVER: xxx
2016-11-22 09:53:48 SERVER -> CLIENT: 235 2.7.0 Authentication succeeded
2016-11-22 09:53:48 CLIENT -> SERVER: MAIL FROM:
2016-11-22 09:53:48 SERVER -> CLIENT: 250 2.1.0 xxx@outlook.com....Sender OK
2016-11-22 09:53:48 CLIENT -> SERVER: RCPT TO:
2016-11-22 09:53:49 SERVER -> CLIENT: 250 2.1.5 xxx@outlook.com
2016-11-22 09:53:49 CLIENT -> SERVER: DATA
2016-11-22 09:53:49 SERVER -> CLIENT: 354 Start mail input; end with .
2016-11-22 09:53:49 CLIENT -> SERVER: Date: Tue, 22 Nov 2016 10:53:47 +0100
2016-11-22 09:53:49 CLIENT -> SERVER: To: my name
2016-11-22 09:53:49 CLIENT -> SERVER: From: xxx@outlook.com
2016-11-22 09:53:49 CLIENT -> SERVER: Subject:
2016-11-22 09:53:49 CLIENT -> SERVER: Message-ID: <xxx@localhost>
2016-11-22 09:53:49 CLIENT -> SERVER: X-Mailer: PHPMailer 5.2.16 (https://github.com/PHPMailer/PHPMailer)//
2016-11-22 09:53:49 CLIENT -> SERVER: MIME-Version: 1.0
2016-11-22 09:53:49 CLIENT -> SERVER: Content-Type: text/plain; charset=UTF-8
2016-11-22 09:53:49 CLIENT -> SERVER:
2016-11-22 09:53:49 CLIENT -> SERVER: Name:
2016-11-22 09:53:49 CLIENT -> SERVER: Email:
2016-11-22 09:53:49 CLIENT -> SERVER: Subject:
2016-11-22 09:53:49 CLIENT -> SERVER: Message:
2016-11-22 09:53:49 CLIENT -> SERVER:
2016-11-22 09:53:49 CLIENT -> SERVER: .
2016-11-22 09:53:53 SERVER -> CLIENT: 250 2.6.0 <xxx@localhost> Queued mail for delivery
2016-11-22 09:53:53 CLIENT -> SERVER: QUIT
2016-11-22 09:53:53 SERVER -> CLIENT: 221 2.0.0 xxx.smtp.hotmail.com Service closing transmission channel

请注意以下两行:

2016-11-22 09:53:49 CLIENT -> SERVER: .
2016-11-22 09:53:53 SERVER -> CLIENT: 250 2.6.0 <xxx@localhost> Queued mail for delivery

正如您在此处所看到的,排队一个帖子之间有4秒的延迟。

这是PHP中的部分,它处理电子邮件的发送:

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $path = $_SERVER['DOCUMENT_ROOT'];
    $path .= "/joomla/libraries/vendor/phpmailer/phpmailer";
    require($path . "/PHPMailerAutoload.php");
    $mail = new PHPMailer();
    $mail->IsSMTP();
    $mail->Mailer = "smtp";
    $mail->CharSet = 'UTF-8';
    $mail->Host = gethostbyname('tls://smtp-mail.outlook.com');
    $mail->Port = "587";
    $mail->SMTPAuth = true;
    $mail->SMTPSecure = 'tls';
    $mail->Username = "xxx@outlook.com";
    $mail->Password = "xxx";
    $mail->From = trim_input("xxx@outlook.com"); 
    $mail->FromName = trim_input($_POST['Name']);
    $mail->AddAddress("xxx@outlook.com", "my name");
    $mail->AddReplyTo(trim_input($_POST['Email']), trim_input($_POST['Name']));
    $mail->SMTPDebug = 0;
    $mail->Subject = trim_input($_POST['Subject']);
    $mail->Body = trim_input($_POST['totalMessage']);
    $mail->WordWrap = 50;
    if (!$mail->Send()) {
    ?>
        //show error message
    <?php
    } else {
    ?>
        //show success message
        <?php
      }
     }
    }

我的问题是如何减少这4秒延迟? 如果不能减少,我将如何在服务器上运行而无需用户等待发送。

请注意,这适用于单个电子邮件,而不是批量电子邮件。

0 个答案:

没有答案