2014-11-06 47 views
0

我想發送大量電子郵件,我使用phpmailer類發送電子郵件,他們每個5.2秒發送!這是我的代碼,爲什麼需要這麼長時間?phpmailer採取5秒..但PHP郵件()採取少於1

<?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('[email protected]', 'site'); 
$mail->CharSet = 'UTF-8'; 
//Set an alternative reply-to address 
$mail->addReplyTo('[email protected]', 'site'); 
//Set who the message is to be sent to 
$mail->addAddress('[email protected]', '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 



?> 
+0

目前你的時間還包括了'$ mail'的初始化時間等。嘗試在「發送」之前先花時間。 'phpmailer'生成所有頭文件並進行檢查。你有沒有檢查你的服務器的負載? – Cheery 2014-11-06 03:15:30

+0

我已經嘗試把$ time_start = microtime(true);在if語句之前,它仍然說5秒。你是什​​麼意思檢查服務器的負載? – Jake 2014-11-06 03:17:02

+0

我正在使用谷歌應用程序的業務..我只是使用函數連接到我的Gmail帳戶來傳遞郵件,現在它需要2秒...我認爲谷歌應用程序的業務可能是干擾或什麼? :S – Jake 2014-11-06 03:18:37

回答

0

得到什麼回事更準確的圖片,使用一個分析器 - 它將使顯而易見,其中時間是怎麼回事。我推薦使用qcachegrind或PHPStorm的xdebug。

您經常會發現SMTP到localhost的速度比您期望的要快 - 當您使用postfix調用sendmail時,sendmail二進制文件將與本地主機建立SMTP連接,因此您可以通過直接使用SMTP來降低開銷。有關更多信息,請參閱後綴文檔。

msgHTML()做的各種事情,你可能不需要 - 設置BodyAltBody直接會更快。

正如其他人所提到的,如果你想發送數量,大部分的開銷可以(也應該)移到發送循環之外,你可以從keepalive等東西中獲益。

確保您使用最新的PHPMailer - 在最近的版本中有一些相當大的加速,尤其是在SMTP中。

0
$mail->IsSMTP(); 

將其更改爲:

$mail->IsMail(); // set mailer to use Mail