1

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>———————————&lt;/p>
    <p><b>Subject:</b></p>
    $userSubject
    <p>———————————&lt;/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

4

3 回答 3

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>
于 2013-09-13T12:26:02.163 回答
1

to您可以通过在参数字符串中简单地在它们之间添加一个逗号来将多个电子邮件地址放入字段中,如下所示:

mail("email1@mailserver.com, email2@mailserver.com", // rest of your code

编辑:根据下面的评论。

您可以根据其上的文档mail()使用函数中的附加标头参数来隐藏多个电子邮件地址:

// 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 ]] )
于 2013-09-13T12:26:11.170 回答
1

只需将您的电子邮件放入一个数组中,如下例所示:

$recepients = array('recepient1@example.com','recepient2@example.com');

foreach($recepients as $recepient){
    mail($recepient, "Mensage sent from website", $body, $header);
}
于 2013-09-13T12:33:26.533 回答