2012-11-26 157 views
5

我對此感到困惑不解。用PHP郵件發送2封電子郵件失敗

//SMTP servers details 
$mail->IsSMTP(); 
$mail->Host = "mail.hostserver.com"; 
$mail->SMTPAuth = false;  
$mail->Username = $myEmail; // SMTP usr 
$mail->Password = "****"; // SMTP pass 
$mail->SMTPKeepAlive = true; 
$mail->From = $patrickEmail; 
$mail->FromName = "***";  
$mail->AddAddress($email, $firstName . " " . $lastName); 
$mail->WordWrap = 50;         
$mail->IsHTML(true);         
$mail->Subject = $client_subject; 
$mail->Body = $client_msg; 

if($mail->Send()) 
{ 

$mail->ClearAllRecipients(); 
$mail->ClearReplyTos(); 
$mail->ClearCustomHeaders(); 
... 
$mail->From = "[email protected]"; 
$mail->FromName = "****";  
$mail->AddAddress($ToEmail1, "***"); //To: (recipients). 
$mail->AddAddress($ToEmail2, "***"); //To: (recipients). 
$mail->WordWrap = 50;    
$mail->IsHTML(true);    
$mail->Subject = $notification_subject; 
$mail->Body = $notification_msg; 
if($mail->Send()) 
{ 
... 

第一封電子郵件發送正常。第二個不是。這種行爲的原因是什麼?我是否缺少某種重置?


更新:使用不同的郵件服務器似乎工作,顯然它是一個特定的郵件服務器導致問題的設置。任何想法可能是什麼?

+0

刪除'$ mail-> ClearCustomHeaders();'並給它一個嘗試.... – Baba

+0

@Baba謝謝,但沒有骰子。沒有區別。 – Sylverdrag

+0

我不推薦使用PHPMailer。編寫你自己的簡單郵件課程將會帶來更多的實際操作經驗,並減少錯誤。我拋棄了phpmailer類,因爲它帶有大量的錯誤和非常愚蠢的錯誤... –

回答

1

關鍵可能橫亙在你省略部分。這兩封電子郵件的發件人的域名是否相同?否則,SMTP主機可能會將此視爲中繼嘗試。如果您有權訪問SMTP服務器日誌,請檢查這些日誌;他們可能會提供一個線索。

此外,還要檢查什麼$mail->ErrorInfo說......它可能會告訴你是什麼問題。

+0

是的,同域發件人。我無法訪問SMTP日誌。對於$ mail-> ErrorInfo,我需要修改我的腳本(它通過Ajax調用) – Sylverdrag

7

某些提供商對可在特定時間範圍內發送的消息數量施加限制。要確定您的問題是否取決於提供商的「費率限制」,您應該嘗試在第一次發送後添加暫停。例如:

if ($mail->Send()) { 
    sleep(10); // Seconds 
    ... 
    if ($mail->Send()) { 
     ... 
    } 
} 

然後,通過逐漸降低睡眠時間,你應該能夠確定哪一個是速率限制。

+0

感謝這個想法,值得一試。我會在本週結束。 – Sylverdrag

+0

如果您的php設置的執行時間設置是默認值(30秒),那麼當該定時器耗盡時,這將停止執行。 –

+0

@JamesMcDonnell你是對的;謝謝你明確表示。現在我編輯了我的答案,將睡眠時間從30秒縮短到10秒。 – 2012-12-05 12:17:44

3

恕我直言,你需要爲每個發送的電子郵件創建新的PHPMailer對象。如果你想分享一些常見的設置,使用這樣的:

$mail = new PHPMailer(); 
/* Configure common settings */ 

while ($row = mysql_fetch_array ($result)) { 
    $mail2 = clone $mail; 
    $mail2->MsgHTML("Dear ".$row["fname"].",<br>".$cbody); 
    $mail2->AddAddress($row["email"], $row["fname"]); 
    $mail2->send(); 
} 
2

我覺得你的問題是$mail->SMTPAuth = false;

這是很難相信,有一些不要求認證的ISP或SMTP提供商,甚至如果他們是免費的。

你可以試試這個來檢查錯誤,而不是或除了檢查send()真:

if ($mail->IsError()) { // 
    echo ERROR; 
    } 
    else { 
    echo NO ERRORS; 
    } 

//Try adding this too, for debugging: 
    $mail->SMTPDebug = 2; // enables SMTP debug information 

其他的一切在你的代碼看起來不錯。我們使用的PHPMailer了很多,從來沒有任何問題與它

+0

它原來設置爲true。我將它改爲false來嘗試,並忘記將其設置回去(兩者都適用於第一封電子郵件,而不適用於第二封電子郵件。) – Sylverdrag

+0

刪除'$ mail-> SMTPKeepAlive = true;'並重試。 –

1

我個人會盡量讓小步驟如發送相同的電子郵件..所以只是明確收件人並嘗試發送相同的電子郵件(此代碼對我的作品)。如果此代碼將可以繼續到不能

加回以前的線路和調試,也許$mail->ClearCustomHeaders();做題

//SMTP servers details 
$mail->IsSMTP(); 
$mail->Host = "mail.hostserver.com"; 
$mail->SMTPAuth = false;  
$mail->Username = $myEmail; // SMTP usr 
$mail->Password = "****"; // SMTP pass 
$mail->SMTPKeepAlive = true; 
$mail->From = $patrickEmail; 
$mail->FromName = "***";  
$mail->AddAddress($email, $firstName . " " . $lastName); 
$mail->WordWrap = 50;         
$mail->IsHTML(true);         
$mail->Subject = $client_subject; 
$mail->Body = $client_msg; 
// all above is copied 
if($mail->Send()) { 
    sleep(5); 
    $mail->ClearAllRecipients(); 
    $mail->AddAddress('[email protected]'); //some another email 

} 
... 
1

我想你已經有了組織上的問題在這裏。

我建議:

  1. 設置您的設置(SMTP,user和pass)
  2. 從一列保持郵件信息和地址
  3. 發送電子郵件
  4. 轉到步驟創建新的郵件對象2
3

試試這個: As @Felipe Alameda A提到了Remove $mail->SMTPKeepAlive = true;

// for every mail 
if(!$mail->Send()) 
{ 
    echo 'There was a problem sending this mail!'; 
} 
else 
{ 
    echo 'Mail sent!';   
} 
$mail->SmtpClose(); 
1

嘗試用下面的例子中,

<?php 

//error_reporting(E_ALL); 
error_reporting(E_STRICT); 

date_default_timezone_set('America/Toronto'); 

require_once('../class.phpmailer.php'); 
//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded 

$mail    = new PHPMailer(); 

$body    = file_get_contents('contents.html'); 
$body    = eregi_replace("[\]",'',$body); 

$mail->IsSMTP(); // telling the class to use SMTP 
$mail->Host  = "mail.yourdomain.com"; // SMTP server 
$mail->SMTPDebug = 2;      // enables SMTP debug information (for testing) 
              // 1 = errors and messages 
              // 2 = messages only 
$mail->SMTPAuth = true;     // enable SMTP authentication 
$mail->Host  = "mail.yourdomain.com"; // sets the SMTP server 
$mail->Port  = 26;     // set the SMTP port for the GMAIL server 
$mail->Username = "[email protected]"; // SMTP account username 
$mail->Password = "yourpassword";  // SMTP account password 

$mail->SetFrom('[email protected]', 'First Last'); 

$mail->AddReplyTo("[email protected]","First Last"); 

$mail->Subject = "PHPMailer Test Subject via smtp, basic with authentication"; 

$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test 

$mail->MsgHTML($body); 

$address1 = "[email protected]"; 
$address2 = "[email protected]"; 

$mail->AddAddress($address1, "John Doe"); 
$mail->AddAddress($address2, "John Peter"); 

$mail->AddAttachment("images/phpmailer.gif");  // attachment if any 
$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment if any 

if(!$mail->Send()) { 
    echo "Mailer Error: " . $mail->ErrorInfo; 
} else { 
    echo "Message sent!"; 
} 
?> 

注:你可以更好的讓多用戶電子郵件地址和名稱作爲ARRAY,像

<?php 

    $recipients = array(
     '[email protected]' => 'Person One', 
     '[email protected]' => 'Person Two', 
     // .. 
    ); 

    foreach($recipients as $email => $name) 
    { 
     $mail->AddCC($email, $name); 
    } 

    (or) 

    foreach($recipients as $email => $name) 
    { 
     $mail->AddAddress($email, $name); 
    } 
?> 

我認爲這可以幫助你解決你的問題。