1

我正在从 txt 文件中的邮件列表中提取电子邮件地址。具有以下内容:

clearstatcache(); 

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

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

如您所见,我已将它们存储在 $emails 中。如果我回显 $emails,我会得到列出的电子邮件:info@example.com、test@mydomain.com、admin@domain.com 等。

现在发送密件抄送:

// 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 <info@example.com>' . "\r\n";
$headers .= 'Bcc: '.$emails . "\r\n";

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

邮件没有被发送到列表,只发送到 info@example.com - 所以这不起作用并且发生了一些意想不到的事情 - 出于某种奇怪的原因,来自 for 循环的 20 封电子邮件列在电子邮件正文的顶部当 info@example.com 收到时

当我尝试手动输入时,它工作得很好。所以下面的代码有效,但手动输入与我试图实现的目标相反。

$test = "info@example.com, test@mydomain.com, admin@domain.com,";

// 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 <info@example.com>' . "\r\n";
$headers .= 'Bcc: '.$test . "\r\n";

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

因此,问题似乎出在变量中,但我无法弄清楚为什么它不起作用,因为 $emails 可以很好地回显所有电子邮件地址。

4

2 回答 2

0

White space is automatically added to the end of line. This change in the for loop fixes the issue.

$emails .= trim($file[$i]);
于 2014-11-05T03:49:56.347 回答
0

您必须在之后关闭循环: mail($to, $subject, $message, $headers); } 和 $headers .= 'Bcc: '.$emails 。"\r\n"; 是 $headers .= 'Bcc: '.$file[$i] 。"\r\n";

所以循环将运行整个程序 20 次。不要将收件人放在 $ to 中,否则也会发送 20 次。经过测试,效果很好。

于 2015-03-21T21:31:54.573 回答