Sending mail to multiple recipients

天大地大妈咪最大 提交于 2020-01-16 19:34:12

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!