1

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

  1. 向多个地址发送电子邮件(现在它仅在向一个电子邮件地址发送电子邮件时才有效。
  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)
         {
         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 "";
4

3 回答 3

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";从您的联系人中隐藏其他用户

于 2012-04-26T19:36:46.407 回答
0
//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 "";
于 2012-04-26T19:56:24.383 回答
-1

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

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

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

进而:

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

应该这样做。

于 2012-04-26T19:45:37.300 回答