问题
I have this PHP code for sending emails from a website's contact form:
<?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.");
}
}
?>
I need to change it in order to send emails to two recipients:
- "email_recipient_1@mailserver.com"
- "email_recipient_2@mailserver.com"
... don't know how, though. Where do I put the other e-mail? I tried adding "email_recipient_2@mailserver.com" in the mail() but it didn't work...
Thanx.
Pedro
回答1:
You can put multiple email addresses into the to
field by simply adding a comma between them inside the parameter string like this:
mail("email1@mailserver.com, email2@mailserver.com", // rest of your code
Edit: Per comments below.
you can hide the multiple email addresses by using the additional headers param in the mail()
function as per the docs on it:
// 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";
This is the fourth param in the mail()
arguments passed:
mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )
回答2:
From
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>
回答3:
Just put your emails into an array like this example:
$recepients = array('recepient1@example.com','recepient2@example.com');
foreach($recepients as $recepient){
mail($recepient, "Mensage sent from website", $body, $header);
}
来源:https://stackoverflow.com/questions/18786206/sending-mail-to-multiple-recipients