2014-11-05 58 views
1

我拉從我的郵件列表中的電子郵件地址在txt文件。與以下內容:只發送密碼抄送郵件在php

clearstatcache(); 

$file = file("test.txt"); 

for ($i = 0; $i < 20; $i++) { 
$emails .= $file[$i]; 
} 

正如你所看到我已經存儲他們在$電子郵件。如果我回聲$電子郵件,我得到的電子郵件中列出: [email protected][email protected][email protected]

現在發送BCC:

// recipient 
$to = ''; 

// subject 
$subject = 'The subject is here'; 

// message 
$message = 'The body of the email is here'; 

// To send HTML mail, the Content-type header must be set 
$headers = 'MIME-Version: 1.0' . "\r\n"; 
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; 

// Additional headers 
$headers .= 'From: John Doe <[email protected]>' . "\r\n"; 
$headers .= 'Bcc: '.$emails . "\r\n"; 

// Mail it 
mail($to, $subject, $message, $headers); 

郵件沒有被髮送到列表,只發送到[email protected] - 所以這不起作用,併發生意外的情況 - 出於某種奇怪的原因,來自for循環的20封電子郵件列在電子郵件的頂部身體何時收到[email protected]

當我嘗試手動輸入它完美的作品。所以下面的代碼可以正常工作,但是手動輸入對於我想要實現的功能是反效果的。

$test = "[email protected], [email protected], [email protected],"; 

// recipient 
$to = ''; 

// subject 
$subject = 'The subject is here'; 

// message 
$message = 'The body of the email is here'; 

// To send HTML mail, the Content-type header must be set 
$headers = 'MIME-Version: 1.0' . "\r\n"; 
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; 

// Additional headers 
$headers .= 'From: John Doe <[email protected]>' . "\r\n"; 
$headers .= 'Bcc: '.$test . "\r\n"; 

// Mail it 
mail($to, $subject, $message, $headers); 

所以出現這個問題是在變,但我想不通爲什麼它不工作,因爲$郵件呼應了所有的電子郵件地址的罰款。

+2

你的文本文件的確切內容是什麼?我猜測你的閱讀方法會導致字符串中出現一些空行,這會錯誤地結束標題塊並啓動主體。一個更簡單的方法是隻做'implode(','file('test.txt'))''。函數'file()'按行讀入一個數組,'implode()'用逗號將它們粘在一起。 – 2014-11-05 02:28:15

+0

使用'$ test =「info @ example.com,test @ mydomain.com,admin @ domain.com」;'逗號之間沒有空格 – 2014-11-05 02:32:08

+0

在@MichaelBerkowski所說的內容之上,您可能需要閱讀以下內容:http:/ /stackoverflow.com/a/9525476/2518525 – Darren 2014-11-05 02:53:51

回答

0

白色空間自動添加到行尾。 for循環中的這一更改修復了這個問題。

$emails .= trim($file[$i]); 
0

您必須關閉循環後: 郵件($到,$主題,$消息,$頭); } and $ headers。='Bcc:'。$ emails。爲 「\ r \ n」 個; 是 $ headers。='Bcc:'。$ file [$ i]。爲 「\ r \ n」 個;

所以循環會運行整個程序20次。不要將收件人放在$ to中,否則會發送20次。 經過測試,效果很好。