3

我有一个 PHP 脚本,它在函数调用中向多个收件人发送电子贺卡(采用逗号分隔的电子邮件地址数组,并分别发送mail()给每个收件人)。但是,在查看收到的电子邮件时,每个客户都可以看到电子邮件发送到的其他地址,这让我相信它们都是在一封电子邮件中发送的,尽管是单独的mail()电话。这是我当前的代码:

<?php
$headers  = "From: ".$_POST['email']."\r\n"; 
$headers .= "Content-type: text/html\r\n";
$array=explode(",", $_POST['sendto']);
for ($i = 0; $i < count($array); ++$i) {
    mail(trim($array[$i]), "Happy Holidays!", $body, $headers);
}
?>

如何解决此问题,以便收件人只能在“收件人”字段中看到他们的电子邮件地址?谢谢!

4

1 回答 1

6

您要使用的是密件抄送字段。

代码:

<?php

$_POST['email'] = str_replace(array("\n", "\r"), '', $_POST['email']);
$_POST['sendto'] = str_replace(array("\n", "\r"), '', $_POST['sendto']);

$headers = "From: " . $_POST['email'] . "\r\n"
         . "Content-Type: text/html\r\n"
         . "BCC: " . $_POST['sendto'] . "\r\n";
mail($_POST['email'], 'Happy Holidays!', $body, $headers);

?>

Send the email to the sender, but BCC the recipients. Also I removed \r and \n chars from the BCC and FROM field otherwise will allow mail header injection attack. Make sure to do the same to $body.

于 2012-12-21T23:12:09.157 回答