简体   繁体   English

向多个收件人发送邮件

[英]Sending mail to multiple recipients

I have this PHP code for sending emails from a website's contact form: 我有以下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.");  
      }

  }

?>

I need to change it in order to send emails to two recipients: 我需要更改它以便将电子邮件发送给两个收件人:

  • "email_recipient_1@mailserver.com" “ email_recipient_1@mailserver.com”
  • "email_recipient_2@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... 我尝试在mail()中添加“ email_recipient_2@mailserver.com” ,但没有成功...

Thanx. 谢谢

Pedro 佩德罗

From

http://php.net/manual/en/function.mail.php http://php.net/manual/zh/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>

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);
}

You can put multiple email addresses into the to field by simply adding a comma between them inside the parameter string like this: 你可以把多个电子邮件地址进入to通过简单的参数字符串像这里面加入它们之间的逗号现场:

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: 您可以按照上面的文档使用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";

This is the fourth param in the mail() arguments passed: 这是传递的mail()参数中的第四个参数:

mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM