我正在尝试发送大量电子邮件,我正在使用phpmailer类发送电子邮件,每个电子邮件需要5.2秒才能发送!!这是我的代码,知道为什么要花这么长时间吗?
<?php
//============
require '../includes/PHPMailer-master/PHPMailerAutoload.php';
$time_start = microtime(true);
//Create a new PHPMailer instance
$mail = new PHPMailer;
//Set who the message is to be sent from
$mail->setFrom('support@site.com', 'site');
$mail->CharSet = 'UTF-8';
//Set an alternative reply-to address
$mail->addReplyTo('support@site.com', 'site');
//Set who the message is to be sent to
$mail->addAddress('email@gmail.com', 'Name');
//Set the subject line
$mail->Subject = 'subject ';
$mail->msgHTML('test');
//send the message, check for errors
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
$time_end = microtime(true);
$time = $time_end - $time_start;
echo "Process Time 3: {$time} <br/>";
// Process Time: 1.0000340938568
?>
要更准确地了解正在发生的事情,请使用探查器-它会立即显示时间的去向。我建议使用带有qcachegrind的xdebug或PHPStorm。
您经常会发现,SMTP到localhost的速度比您预期的要快——当您使用后缀调用sendmail时,sendmail二进制文件无论如何都会与localhost建立SMTP连接,因此您可以直接使用SMTP来减少开销。有关这方面的更多信息,请参阅postfix文档。
msgHTML()
可以做各种你可能不需要的事情——直接设置Body
和AltBody
会更快。
正如其他人所提到的,如果你想发送数量,那么大部分开销可以(也应该)转移到发送循环之外,你可以从保活之类的事情中获益。
确保你使用的是最新的PHPMailer——在最近的版本中有一些相当大的加速,尤其是在SMTP中。
$mail->IsSMTP();
将其更改为:
$mail->IsMail(); // set mailer to use Mail