MailJet を使用して電子メールを送信するより高速な方法を見つけようとしています。以下の関数はループ内で呼び出されるため、各ループの反復は個別の電子メールになります。私のテスト ケースでは、10 通のメールを送信するのに 19 秒かかりました。彼らのサポートでは、ssl をオフにして 465 の代わりにポート 80 を使用することを推奨しています。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を試しました。10 個の電子メールのテストは、http と https で約 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);
?>