PHP 邮件:多个收件人?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7389035/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP mail: Multiple recipients?
提问by Akos
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 = "[email protected]";
$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?
在我的表(“电子邮件”)中,我有多个地址。(它们不是逗号分隔的。)我如何将消息发送到所有这些地址?
回答by Amber
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”参数可以采用逗号分隔的地址列表。
回答by Brad
Separate the addresses with commas.
用逗号分隔地址。
$to=array();
while($row = mysql_fetch_array($result)) {
array_push($to, $row['address']);
}
...
mail(implode(',', $to), $submit, $message, $headers);
回答by Alex Spataru
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.
我刚刚测试了您提供的代码,在使用它们之前,人们需要知道使用这种方式(“收件人”字段中的多个地址),该电子邮件中的每个人都可以看到所有目的地。
Also, if you're using Bcc, they'll also know the first person in the list.
此外,如果您使用密件抄送,他们也会知道列表中的第一个人。
Be aware! :)
意识到!:)
回答by Guillermo Díaz Gallego
You just need to use GROUP_CONCAT
to return the results separated by ','
您只需要使用GROUP_CONCAT
返回由分隔的结果','
$result = mysql_query("SELECT GROUP_CONCAT(address) FROM email");
回答by oldboy
According to Alex's answer above, recipients will know the first person in the list even if you're using BCC. A solution for this would be to send individual copies to each address one by one.
根据上面亚历克斯的回答,即使您使用密件抄送,收件人也会知道列表中的第一个人。对此的解决方案是将单独的副本逐个发送到每个地址。
$emails = ['[email protected]', '[email protected]', '[email protected]'];
foreach ($emails as $email){ // or $result as $row
mail(
$email, // or $row['address']
'Same Subject',
'Same Content',
'From: [email protected]',
'-f [email protected]'
);
}