我有这个PHP代码,用于从网站的联系表单发送电子邮件:
<?php
if(count($_POST) > 0){
$userName = $_POST['userName'];
$userEmail = $_POST['userEmail'];
$userSubject = $_POST['userSubject'];
$userMessage = $_POST['userMessage'];
$header = "Content-Type: text/html\r\nReply-To: $userEmail\r\nFrom: $userEmail <$userEmail>";
$body =
@"Contact sent from website ".$_SERVER['REMOTE_ADDR']." | Day and time ".date("d/m/Y H:i",time())."<br />
<hr />
<p><b>Name:</b></p>
$userName
<p>———————————</p>
<p><b>Subject:</b></p>
$userSubject
<p>———————————</p>
<p><b>Mensagem:</b></p>
$userMessage
<hr />
End of message";
if(mail("email_recipient_1@mailserver.com", "Mensage sent from website", $body, $header)){
die("true");
} else {
die("Error sending.");
}
}
?>
我需要更改它才能向两位收件人发送电子邮件:
......不知道怎么样。我在哪里放另一封电子邮件?我尝试在 mail()中添加“email_recipient_2@mailserver.com”,但它无效...
感谢名单。
佩德罗
答案 0 :(得分:2)
这
http://php.net/manual/en/function.mail.php
The formatting of this string must comply with » RFC 2822. Some examples are:
user@example.com
user@example.com, anotheruser@example.com
User <user@example.com>
User <user@example.com>, Another User <anotheruser@example.com>
答案 1 :(得分:1)
只需将您的电子邮件放入数组,如下例所示:
$recepients = array('recepient1@example.com','recepient2@example.com');
foreach($recepients as $recepient){
mail($recepient, "Mensage sent from website", $body, $header);
}
答案 2 :(得分:0)
您可以将多个电子邮件地址放入to
字段,只需在参数字符串中添加逗号,如下所示:
mail("email1@mailserver.com, email2@mailserver.com", // rest of your code
编辑:以下评论。
您可以使用mail()
函数中的附加标题参数隐藏多个电子邮件地址,其中包含per the docs:
// Additional headers
$headers .= 'To: Mary <mary@example.com>, Kelly <kelly@example.com>' . "\r\n";
$headers .= 'From: Birthday Reminder <birthday@example.com>' . "\r\n";
$headers .= 'Cc: birthdayarchive@example.com' . "\r\n";
$headers .= 'Bcc: birthdaycheck@example.com' . "\r\n";
这是传递的mail()
参数中的第四个参数:
mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )