PHP表单发送电子邮件给多个收件人

34

我有一些PHP代码,用于将表单发送到特定的电子邮件地址。但是,我想在发送时在PHP中包含几个其他电子邮件地址。我该如何做?

<?php
if(isset($_POST['email'])) {

    // EDIT THE 2 LINES BELOW AS REQUIRED
    $email_to = "email@email.com";
    $email_subject = "MVP Nomination";


    function died($error) {
        // your error code can go here
        echo "We are very sorry, but there were error(s) found with the form you submitted. ";
        echo "These errors appear below.<br /><br />";
        echo $error."<br /><br />";
        echo "Please go back and fix these errors.<br /><br />";
        die();
    }

    // validation expected data exists
    if(!isset($_POST['username']) ||
        !isset($_POST['body'])||
        !isset($_POST['email'])) {
        died('We are sorry, but there appears to be a problem with the form you submitted.');       
    }

    $username = $_POST['username']; // required
    $body = $_POST['body']; // required
    $email_from = $_POST['email'];         // required

  $error_message = "";
    $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
  if(!preg_match($email_exp,$email_from)) {
    $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
  }
    $string_exp = "/^[A-Za-z .'-]+$/";
  if(!preg_match($string_exp,$username)) {
    $error_message .= 'The Username you entered does not appear to be valid.<br />';
  }

  if(strlen($error_message) > 0) {
    died($error_message);
  }
    $email_message = "Form details below.\n\n";

    function clean_string($string) {
      $bad = array("content-type","bcc:","to:","cc:","href");
      return str_replace($bad,"",$string);
    }

    $email_message .= "Name: ".clean_string($username)."\n";
    $email_message .= "Comments: ".clean_string($body)."\n";


// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);  
}
header("Location: ThankYou.html");  //Redirect to Thank You HTML page after email is sent
?>

谢谢。


1
@PLB - StackOverflow的代码功能非常完美,没有必要使用外部网站。 - Buggabill
你在提问之前尝试过搜索吗?http://stackoverflow.com/search?q=php+multiple+email+addresses+ - Buggabill
@Buggabill 不,他已经在jsfiddle上发布了php代码,这就是为什么我建议使用viper-7。现在问题已经被编辑过了。 - Leri
是的...我知道它已经被编辑过了。我进行了编辑。依赖外部网站并不是一个好的做法。链接可能会失效 - 包括在jsfiddle上的链接。 - Buggabill
9个回答

94

这将有效:

$email_to = "jhewitt@amleo.com,some@other.com,yet@another.net";

我看到其他回答在逗号后面的电子邮件之间放了一个空格,只有这个没有。我认为这样更好?两种方法之间有区别吗? - Jon
1
如何隐藏邮件头中的其他电子邮件地址。它将向收件人显示所有电子邮件地址。 - smehsoud
@smehsoud 这是一个不同的问题,但你可以简单地在邮件头中添加一个密送抄送行。 - Gung Foo

12

请按照以下格式使用逗号分隔数值。

$email_to = 'Mary <mary@example.com>, Kelly <kelly@example.com>';
@mail($email_to, $email_subject, $email_message, $headers);

或者对电子邮件地址运行 foreach 循环

//list of emails in array format and each one will see their own to email address
$arrEmail = array('Mary <mary@example.com>', 'Kelly <kelly@example.com>');

foreach($arrEmail as $key => $email_to)
    @mail($email_to, $email_subject, $email_message, $headers);

10
如果您需要将电子邮件添加为抄送或密送,请在用作标题的变量中添加以下部分:
$headers .= "CC: sombodyelse@noplace.com".PHP_EOL;
$headers .= "BCC: hidden@special.com".PHP_EOL;

敬礼


1
为了实现最佳的跨平台兼容性,您可能希望使用 PHP_EOL 而不是 \r\n。 - Evan Donovan
我已经像Evan Donovan所指出的那样,将\r\n更改为PHP_EOL - MTranchant

5

您可以将收件人添加到$email_to变量中,用逗号(,)隔开。或者您可以在标题中添加新字段,即抄送(CC):密送(BCC):,并在那里放置您的收件人。BCC是最推荐的方式。


3
如果我理解正确,尝试这个方法。
$headers = "Bcc: someone@domain.com";

或者

$headers = "Cc: someone@domain.com";

2
如果所有的答案都不起作用,那么您可以简单地使用多个邮件函数来发送给多个收件人。
$email_to1 = "firstEmail@demo.com";
$email_to2 = "secondEmail@demo.com";

mail($email_to1, $email_subject, $email_message, $headers);  
mail($email_to2, $email_subject, $email_message, $headers);  

1
确保你使用逗号而不是分号来连接多个电子邮件。

0

PHP中的$email_to可以接受多个电子邮件收件人。

您可以使用implode将不同的电子邮件地址连接起来,使用逗号','分隔。

如果您正在使用phpmailer,则可以使用explode将电子邮件地址字符串分隔,然后使用循环(如for each)添加它们。

/** separate each email address */
           $str_arr = explode (",", $recipient);

/** add each email address using addAddress */
           foreach ($str_arr as $email) {
            $mail->addAddress($email); //(recipientEmail, recipientName) - name is optional
            }

发送时它会看起来像这样

enter image description here


0

如果您的电子邮件地址存储在数组中:

$emails = ['jhewitt@amleo.com', 'some@other.com', 'yet@another.net'];
$email_to = implode(',', $emails); // "glue" every array elements (emails) with a comma and return a single string "jhewitt@amleo.com,some@other.com,yet@another.net"
@mail($email_to, $email_subject, $email_message, $headers);

当您从外部数据源检索电子邮件地址时,这将非常有用。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接