2011-03-21 145 views
3

使用PHPMailer發送單個電子郵件給收件人我沒有收到任何內容收件人:字段時,我將$mail->SingleTo = TRUE;添加到我的代碼。個人電子郵件的PHPMailer設置發送空To:字段

當我刪除$mail->SingleTo = TRUE;我收到的電子郵件地址爲收件人:是正確的。

這就是我得到:

reply-to  xxxxxx <[email protected]>, No Reply <[email protected]> 
to  
date   Mon, Mar 21, 2011 at 5:07 PM 
subject  Testing  
mailed-by gmail.com 
signed-by gmail.com 

(其中XXXXXXX代表我的電子郵件地址。)

這裏是我的代碼:

if(isset($_POST['submit'])) 
{ 
    require_once('PHPMailer_v5.1/class.phpmailer.php'); 


$mail   = new PHPMailer(); 

$subject = $_POST['subject']; 
$body = $_POST['emailbody']; 
$to   = $_POST['to']; 



$mail->IsSMTP(); // telling the class to use SMTP 
//$mail->Host  = "localhost"; // 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->SMTPSecure = "SSL";     // sets the prefix to the servier 
$mail->Host  = "smtp.gmail.com";  // sets GMAIL as the SMTP server 
$mail->Port  = 587;     // set the SMTP port for the GMAIL server 
$mail->Username = "[email protected]"; // GMAIL username 
$mail->Password = "*********";   // GMAIL password 

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

$mail->AddReplyTo("[email protected]","No Reply"); 

$mail->Subject = $subject; 

// After adding this line I'm getting an empty To: field 
$mail->SingleTo = TRUE; 

$mail->AddAddress("[email protected]", 'xyz abc'); 
$mail->AddAddress("[email protected]", 'abc xyz'); 
//$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test 

$mail->MsgHTML($body); 


if(!$mail->Send()) { 
    $message= "Mailer Error: " . $mail->ErrorInfo; 
} 
else 
{ 
    $message= "Message sent!"; 
}  
} 
+0

問題被接受,但賞金是開放的?我們可以得到一個爲什麼我們可以回答賞金? – 2011-04-25 19:31:51

回答

3

您正在使用SMTP發送電子郵件。使用Smtp發送時,phpmailer類不使用SingleTo參數。

更多的,如果您在SingleTo == true時看到CreateHeader函數,那麼配方只會被添加到$ this-> SingleToArray,而不會被添加到頭本身,所以基本上它就是PHPmailer的bug。

貌似唯一的選擇,除非你想修補PHPMailer的,是發送郵件一個接一個,而無需使用SingleTo財產

的解決方案將是

function & prepare_mailer($subject, $body) { 

    $mail   = new PHPMailer(); 
    $mail->IsSMTP(); // telling the class to use SMTP 
    //$mail->Host  = "localhost"; // 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->SMTPSecure = "SSL";     // sets the prefix to the servier 
    $mail->Host  = "smtp.gmail.com";  // sets GMAIL as the SMTP server 
    $mail->Port  = 587;     // set the SMTP port for the GMAIL server 
    $mail->Username = "[email protected]"; // GMAIL username 
    $mail->Password = "*********";   // GMAIL password 
    $mail->SetFrom('[email protected]', 'XXXXXX'); 
    $mail->AddReplyTo("[email protected]","No Reply"); 
    $mail->Subject = $subject; 
    $mail->MsgHTML($body); 
    return $mail; 
} 

foreach($_POST['to'] as $to){ 
    $mail = null; 
    $mail = & prepare_mailer($_POST['subject'],$_POST['body']); 
    $mail->AddAddress($to['address'], $to['name']); 
    $mail->Send(); 

} 
+0

嗯,謝謝..如果問題是smtp的原因,那麼如果我使用這個類的郵件功能,我可以發送電子郵件,因爲我想要? – mysterious 2011-04-21 17:42:18

+0

如果你不使用smtp比是的,你將不得不使用IsMail()並且在服務器上配置郵件(來自PHP)。例如:[mail](http://phpmailer.worxware.com/index.php?pg=examplebmail)或[sendmail](http://phpmailer.worxware.com/index.php?pg=examplebsendmail) – 2011-04-21 18:33:46

+1

謝謝嘗試當我將再次在該項目上工作。 – mysterious 2011-04-22 10:24:53

0

的問題是在SmtpSend功能(開始在class.phpmailer.php的第701行)。正如你在那裏可以看到的那樣,他們不會考慮singleTo設置,只需添加所有收件人併發送一次消息正文。

所以,你應該添加一些邏輯那裏以測試是否singleTo爲真,並且如果它是修改代碼,以便它分別發送這些郵件,前綴到:報頭添加到消息的主體。如果singleTo爲false,則可以按原樣調用代碼(第713-758行)。

或者,如果你不想打補丁的東西,那麼你可以使用一個運輸方法(即sendmail的),它似乎支持singleTo參數。

+0

我可以發送,因爲我想使用郵件函數選項的PHP郵件而不是smtp? – mysterious 2011-04-21 17:45:01

3

設置所有其它參數,然後通過循環將收件人,發送電子郵件,然後使用ClearAllRecipients()函數。像這樣:

{ // loop start 
     $mail->AddAddress($person_email, $person_name); 
     $mail->Send(); 
     $mail->ClearAllRecipients(); 
} // loop end 
0

SingleTo不是一個好主意。它僅適用於「sendmail」或「郵件」傳輸,而不適用於SMTP。如果您將SingleTo與SMTP一起使用,則只會忽略此參數而不會出現任何錯誤或警告,並且您可能會得到重複項。

既然你同時使用SingleTo的SMTP,如在你的代碼,你的情況SingleTo被忽略。

SMTP協議的設計方式是不能將一條消息發送給幾個不同的收件人,每個收件人在TO:字段中只有自己的地址。要讓每個收件人在TO中只有其名稱,則必須再次發送整個郵件。這解釋了爲什麼SingleTo與SMTP不兼容。

根據PHPMailer庫的作者,SingleTo計劃在PHPMailer 6.0的發行版中棄用,並在7.0中刪除。作者解釋說,最好控制發送給更高級別的多個收件人,因爲PHPMailer不是郵件列表發件人。他們說,使用PHP mail()函數需要不鼓勵,因爲安全使用非常困難; SMTP更快,更安全,並提供更多的控制和反饋。由於SMTP與SingleTo不兼容,PHPMailer的作者將刪除SingleTo而不是SMTP。

相關問題