简体   繁体   English

PHP邮件:多个收件人?

[英]PHP mail: Multiple recipients?

I have this code: 我有这个代码:

<?php
include("db.php");

$result = mysql_query("SELECT * FROM email");

while($row = mysql_fetch_array($result))
{
$to = $row['address'];
}
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "example@example.com";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
?>

In my table ("email") I have multiple addresses. 在我的表(“电子邮件”)中,我有多个地址。 (They are not comma sepparated.) How could I send my message to all of those addresses? (它们不是逗号分隔。)如何将我的消息发送到所有这些地址?

while($row = mysql_fetch_array($result))
{
    $addresses[] = $row['address'];
}
$to = implode(", ", $addresses);

As specified on the mail() manual page , the "to" parameter of the function can take a comma-separated list of addresses. 正如mail()手册页上指定的那样,函数的“to”参数可以采用逗号分隔的地址列表。

Separate the addresses with commas. 用逗号分隔地址。

$to=array();
while($row = mysql_fetch_array($result)) {
    array_push($to, $row['address']);
}

...

mail(implode(',', $to), $submit, $message, $headers);

I just tested the codes you presented and before using them, people need to know that using this way (multiple addresses in 'to' field), every single person in that email can see all the destinatars. 我刚刚测试了您呈现的代码,在使用它们之前,人们需要知道使用这种方式(“to”字段中的多个地址),该电子邮件中的每个人都可以看到所有的目的地。

Also, if you're using Bcc, they'll also know the first person in the list. 此外,如果你正在使用密件抄送,他们也会知道列表中的第一个人。

Be aware! 意识到! :) :)

您只需要使用GROUP_CONCAT返回以','分隔的结果

$result = mysql_query("SELECT GROUP_CONCAT(address) FROM email");

According to Alex's answer above, recipients will know the first person in the list even if you're using BCC. 根据Alex的上述答案,即使您使用BCC,收件人也会知道列表中的第一个人。 A solution for this would be to send individual copies to each address one by one. 解决方案是将各个副本逐个发送到每个地址。

$emails = ['a@example.com', 'b@example.com', 'c@example.com'];
foreach ($emails as $email){ // or $result as $row
  mail(
    $email, // or $row['address']
    'Same Subject',
    'Same Content',
    'From: same_sender@example.com',
    '-f same_sender@example.com'
  );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM