PHP 将邮件发送到多个电子邮件地址
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4506078/
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 send mail to multiple email addresses
提问by rakib
What code I should do change in this PHP script to send one email to more than 20 email addresses?
我应该在此 PHP 脚本中更改哪些代码以将一封电子邮件发送到 20 多个电子邮件地址?
<?php
$email_to = "[email protected]"; // your email address
$email_subject = "Contact Form Message"; // email subject line
$thankyou = "thankyou.htm"; // thank you page
?>
Please give me an example. Thank you.
请给我一个例子。谢谢你。
回答by prodigitalson
Fore readability sake in the code use an array and implode it to a comma separated string:-
为了代码中的可读性,使用数组并将其内爆为逗号分隔的字符串:-
$recipients = array(
"[email protected]",
// more emails
);
$email_to = implode(',', $recipients); // your email address
$email_subject = "Contact Form Message"; // email subject line
$thankyou = "thankyou.htm"; // thank you page
回答by Stefan H
Your
您的
$email_to = "[email protected], [email protected], [email protected]"
Needs to be a comma delimited list of email adrresses.
需要是逗号分隔的电子邮件地址列表。
mail($email_to, $email_subject, $thankyou);
回答by Phoenix
Just separate them by comma, like $email_to = "[email protected], [email protected], John Doe <[email protected]>"
.
只需用逗号分隔它们,例如$email_to = "[email protected], [email protected], John Doe <[email protected]>"
.
回答by rajmohan
Following code will do the task....
以下代码将完成任务....
<?php
$contacts = array(
"[email protected]",
"[email protected]",
//....as many email address as you need
);
foreach($contacts as $contact) {
$to = $contact;
$subject = 'the subject';
$message = 'hello';
mail($to, $subject, $message, $headers);
}
?>
回答by zsalzbank
Something like this:
像这样的东西:
mail("[email protected] , [email protected] , [email protected]", "Test e-mail", "Hi, this is a test message!");
回答by Abhijit
$recipients = "[email protected],[email protected],[email protected],[email protected]";
$email_array = explode(",",$recipients);
foreach($email_array as $email)
{
echo $to = $email;
$subject = 'the subject';
$message = 'hello';
$headers = 'From: [email protected]' . "\r\n" .
'Reply-To: [email protected]' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
}
回答by Roel B.
It is very bad practice to send all email addresses to all recipients; you should use Bcc (blind carbon copies).
将所有电子邮件地址发送给所有收件人是非常糟糕的做法;您应该使用 Bcc(密件抄送)。
$from = "[email protected]";
$recipList = "mailaddress1,mailaddress2,etc";
$headers = "MIME-Version: 1.0\nContent-type: text/html; charset=utf-8\nFrom: {$from}\nBcc: {$recipList}\nDate: ".date(DATE_RFC2822);
mail(null,$subject,$message,$headers); //send the eail
回答by tkt986
The best way could be to save all the emails in a database.
最好的方法是将所有电子邮件保存在数据库中。
You can try this code, assuming you have your email in a database
你可以试试这个代码,假设你在数据库中有你的电子邮件
/*Your connection to your database comes here*/
$query="select email from yourtable";
$result =mysql_query($query);
/the above code depends on where you saved your email addresses, so make sure you replace it with your parameters/
/以上代码取决于您保存电子邮件地址的位置,因此请确保将其替换为您的参数/
Then you can make a comma separated string from the result,
然后你可以从结果中创建一个逗号分隔的字符串,
while($row=$result->fetch_array()){
if($rows=='') //this prevents from inserting comma on before the first element
$rows.=$row['email'];
else
$rows.=','.$row['email'];
}
Now you can use
现在你可以使用
$to = explode(',',$rows); // to change to array
$string =implode(',',$cc); //to get back the string separated by comma
With above code you can send the email like this
使用上面的代码,您可以像这样发送电子邮件
mail($string, "Test", "Hi, Happy X-Mas and New Year");
回答by uriateho
Try this. It works for me.
尝试这个。这个对我有用。
$to = $email1 .','. $email2 .','. $email3;
回答by XMen
In mail function you can as many reciepient as you want in $emailto paramater seperated by comma.
在邮件功能中,您可以在 $emailto 参数中以逗号分隔任意数量的收件人。