如何使用此脚本向多个人发送电子邮件而不在 to 字段中显示所有电子邮件?

Posted

技术标签:

【中文标题】如何使用此脚本向多个人发送电子邮件而不在 to 字段中显示所有电子邮件?【英文标题】:How to use this script to email multiple people without showing all emails in the to field? 【发布时间】:2012-04-26 19:26:28 【问题描述】:

我在下面有这个脚本,我想对其进行调整,以便它可以做两件事:

    向多个地址发送电子邮件(目前仅在向一个电子邮件地址发送电子邮件时才有效。 在“收件人”字段中,不应显示收到相同通知的所有其他人。因此,每封电子邮件都应该是个人的。

代码:

         //Query to get emails
         //$share_to comes from a drop down selection of users.  
         //it could be 1 person or multiple people.
         //The way its stored in the field is this way user1,user2,user3,user4 or if one person then just user1.  

         $sql = "SELECT email_address from accounts 
         WHERE person_id='$share_to'";
         $result = mysql_query($sql);

         $query = mysql_query($sql) or die 
         ("Error: ".mysql_error());

         if ($result == "")
         
         echo "";
         
         echo "";


         $rows = mysql_num_rows($result); 


         if($rows == 0)
         
         print("");

         
         elseif($rows > 0)
         
         while($row = mysql_fetch_array($query))

         

         $email = htmlspecialchars($row['email_address']);


         print("");
         

         


         $headers = "MIME-Version: 1.0\r\n";
         $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
         $headers .= "From: $usermail\r\n";
         $headers .= "BCC: $email\r\n";
         $to = "support@domain.com"; //the mail in the "TO", visible to all. 
         there has to be 1.
         $subject = "$fullname Asked you something";
         $message = "<html><body>";
         $message .= "Hi!, <br><br>$fullname asked you something.<br><br>";
         $message .= "<a href=www.domain.com/login.php>Click here to login.</a><br><br>";
         $message .= "Please reply to this email if you have any questions.<br><br>Thanks,    
         <br><br>Abe<br>";
         $message .= "<img src=www.domain.com/images/logohop.png /><br><br>";

         $message .= "</body></html>";
         mail($to, "Subject: $subject",
         $message, "$headers" );
         echo "";

【问题讨论】:

将发送的邮件放入while循环中 【参考方案1】:

使用其中任何一种,您可以向多个用户发送电子邮件

$headers .= 'To: Mary <mary@example.com>, Kelly <kelly@example.com>' . "\r\n";

$headers .= 'Cc: birthdayarchive@example.com,birthdayarchive2@example.com ' . "\r\n";

请参阅http://php.net/manual/en/function.mail.php 寻求帮助

使用$headers .= 'BCc: birthdayarchive@example.com,birthdayarchive2@example.com ' . "\r\n"; 隐藏您联系人中的其他用户

【讨论】:

是的,但这会阻止接收者看到其他接收者吗? 使用 $headers .= 'BCc:birthdayarchive@example.com,birthdayarchive2@example.com '。 "\r\n"; 是的,但是在 to 字段中,它显示了我的电子邮件从哪里发送。如果我随后选择在 to 字段中使用 $email,那么它将显示所有电子邮件地址。【参考方案2】:
//Query to get emails
         //$share_to comes from a drop down selection 
         of users.  it could be 1 person or multiple 
         people.The way its stored in the field is 
         this way user1,user2,user3,user4 
         or if one person then just user1.  

         $sql = "SELECT email_address from accounts 
         WHERE person_id='$share_to'";
         $result = mysql_query($sql);

         $query = mysql_query($sql) or die 
         ("Error: ".mysql_error());

         if ($result == "")
         
         echo "";
         
         echo "";


         $rows = mysql_num_rows($result); 


         if($rows == 0)
         
         print("");

         
         elseif($rows > 0)
         
         $email='';
         while($row = mysql_fetch_array($query))

         

         $email. = htmlspecialchars($row['email_address']).',';


         print("");
         

         


         $headers = "MIME-Version: 1.0\r\n";
         $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
         $headers .= "From: $usermail\r\n";
         $headers .= "BCC: ".rtrim($email,",")."\r\n";
         $to = "support@domain.com"; //the mail in the "TO", visible to all. 
         there has to be 1.
         $subject = "$fullname Asked you something";
         $message = "<html><body>";
         $message .= "Hi!, <br><br>$fullname asked you something.<br><br>";
         $message .= "<a href=www.domain.com/login.php>Click here to login.</a><br><br>";
         $message .= "Please reply to this email if you have any questions.<br><br>Thanks,    
         <br><br>Abe<br>";
         $message .= "<img src=www.domain.com/images/logohop.png /><br><br>";

         $message .= "</body></html>";
         mail($to, "Subject: $subject",
         $message, "$headers" );
         echo "";

【讨论】:

嘿。现在它不会发送任何电子邮件。 print_r($email) 并查看电子邮件地址。确保正确获取电子邮件地址 如果我选择一个人来发送通知,那么它可以工作并打印:Array ([0] => person@domain.com) 但如果我选择了超过 1 个人,那么它不会打印它。 好的,现在它说:PHP Parse error: syntax error, unexpected '=' 并且该行是 $email。 = htmlspecialchars($row['email_address']).','; 快点结束$email .= htmlspecialchars($row['email_address']).',';【参考方案3】:

如果您不希望收件人看到完整列表,您可以发送多封电子邮件,每个收件人一封 - 正如 Dagon 上面提到的,只需将邮件代码移动到循环内部 - 或者您可以使用密件抄送字段。

要将一封电子邮件发送给多个收件人,您需要在 - 中添加一个逗号分隔的地址列表 -

$emails = array();
while($row = mysql_fetch_array($query)) 
    $emails[] = htmlspecialchars($row['email_address']);

然后:

$headers .= "BCC: " . implode(",", $emails) . "\r\n";

应该这样做。

【讨论】:

它给了我这个错误,不知道如何解决它:PHP Parse error: syntax error, unexpected '.'它指向你的 $headers 行。 @ariel 多了一个点,少了一个括号,应该是$headers .= "BCC: " . implode("," , $emails) . "\r\n"; 当您输入代码而没有先进行测试时会发生这种情况。谢谢,@danishgoel。

以上是关于如何使用此脚本向多个人发送电子邮件而不在 to 字段中显示所有电子邮件?的主要内容,如果未能解决你的问题,请参考以下文章

python:如何使用TO,CC和BCC发送邮件?

在一个脚本中捕获多个命令

PHP 邮件不在“收件人”标题中显示“收件人”

如何向我的 gmail 发送电子邮件而不是托管

正确发送邮件列表邮件的电子邮件标题

如何使用 Codeigniter 通过 cc 发送多封电子邮件?