使用MailJet发送SMTP电子邮件的最快方法

时间:2014-08-31 21:19:45

标签: php email phpmailer pear mailjet

我试图找到一种使用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);
?>

0 个答案:

没有答案