1

我正在尝试寻找一种更快的方式来使用 MailJet 发送电子邮件。下面的函数在循环中被调用,因此每次循环迭代都是一封单独的电子邮件。在我的测试用例中,发送 10 封电子邮件需要 19 秒。他们的支持建议关闭 ssl 并使用端口 80 而不是 465,这将其加速到 13-14 秒,但这仍然太慢了。我尝试使用 PEAR 和 PHPMailer 进行 SMTP 发送,两者之间没有区别,两者的速度相同。有什么想法为什么这么慢?

function sendViaMailJet($mailTo, $mailSubject, $mailBody, $mailFrom="noreply@mysite.com") {
    global $error;
    require_once('class.phpmailer.php');
    $toArray = explode(',', $mailTo);
    $mailBody = makeMailJetHtmlBody($mailBody);

    $mail = new PHPMailer();
    $mail->IsSMTP();
    $mail->SMTPDebug = 1;  // debugging: 1 = errors and messages, 2 = messages only
    $mail->SMTPAuth = true;  // authentication enabled
    //$mail->SMTPSecure = 'ssl';
    $mail->Host = 'in.mailjet.com';
    $mail->Port = 80;
    $mail->Username = 'XXXXXXXXX';
    $mail->Password = 'XXXXXXXXX';
    $mail->SetFrom($mailFrom, 'My Site');
    $mail->AddReplyTo($mailFrom, 'My Site');
    $mail->isHTML(true);
    $mail->Subject = $mailSubject;
    $mail->Body = $mailBody;
    foreach($toArray as $toAddress) $mail->AddAddress($toAddress, $toAddress);
    //$mail->AddBCC('me@mysite.com', 'Gordon');

    if(!$mail->Send()) {
        $error = 'Mail error: '.$mail->ErrorInfo;
        return false;
    }
    else {
        //$error = 'Message sent!';
        return true;
    }
}

更新:我关闭了 PHPMailer 并尝试了 MailJets API,https://github.com/mailjet/mailjet-apiv3-php-simple。它更快一点,对于 http 和 https,10 封电子邮件测试现在需要大约 8 秒。

更新我切换回使用 PHPMailer 并根据以下建议进行了一些调整。发送 10 封电子邮件仍然需要约 8 秒。这是我的测试文件...

<?php 
//@@@@@@@@@@@@@@@@@@@@@@
//START Init PHPMailer: This block of code only runs once per page
//@@@@@@@@@@@@@@@@@@@@@@
$mailx = new PHPMailer();
$mailx->IsSMTP();
//$mailx->SMTPDebug = 1;  // debugging: 1 = errors and messages, 2 = messages only
$mailx->SMTPAuth = true;
//$mailx->SMTPSecure = 'ssl';
$mailx->Host = 'in.mailjet.com';
$mailx->Port = 80;
$mailx->Username = 'xxxxxxxxxxxxx';
$mailx->Password = 'xxxxxxxxxxxxx';
$mailx->isHTML(true);
$mailx->SMTPKeepAlive = true;
//@@@@@@@@@@@@@@@@@@@@@@
//END Init PHPMailer: This block of code only runs once per page
//@@@@@@@@@@@@@@@@@@@@@@

function doMailJet($mailx, $mailTo, $mailSubject, $mailBody, $monitor=true, $mailFrom="noreply@mysite.com")
{
    $toArray = explode(',', $mailTo);

    $mailx->AltBody  = $mailBody; //This is the body in plain text for non-HTML mail clients
    $mailBody        = makeMailJetHtmlBody($mailBody);
    $mailx->Subject  = $mailSubject;
    $mailx->Body     = $mailBody;
    $mailx->From     = $mailFrom;
    $mailx->FromName = 'Message from MySite';
    $mailx->addReplyTo($mailFrom, 'Reply to MySite');
    foreach($toArray as $toAddress) $mailx->addAddress($toAddress);
    //if($monitor) $mail->AddBCC('gordon@mysite.com', 'Gordon');

    if(!$mailx->send()) {
        //echo 'Message could not be sent.';
        echo 'Mailer Error: ' . $mailx->ErrorInfo;
        return false;
    } else {
        //echo 'Message has been sent';
        return true;
    }
}

echo 'Testing doMailJet...<br>';
$time_start = microtime(true); //Lets see how long this is going to take.
for ($x=0; $x<=10; $x++) {
  echo "Sending: $x <br>";
  doMailJet($mailx, 'gordon1977@mySite.com','Testing 123','Test Message',false);
}
echo 'getTimeElapsed: '.getTimeElapsed($time_start);
?>
4

0 回答 0