向脚本添加第二个电子邮件地址

时间:2011-11-07 22:36:39

标签: php email

我有一个脚本,我已经修改,以满足我的要求,但我现在需要将电子邮件发送给多个人,有人可以指出我正确的方向,我如何修改脚本发送到超过一个人。

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

    // EDIT THE 2 LINES BELOW AS REQUIRED
    $email_to = "emailremoved@sample.com";
    $email_subject = "Kro Catering Website Enquiry";


    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['your_name']) ||
        !isset($_POST['type']) ||
        !isset($_POST['guests']) ||
        !isset($_POST['date']) ||
        !isset($_POST['phone']) ||
        !isset($_POST['email'])) {
        died('We are sorry, but there appears to be a problem with the form you submitted.');      
    }

    $your_name = $_POST['your_name']; // required
    $type = $_POST['type']; // required
    $guests = $_POST['guests']; // required
    $date = $_POST['date']; // not required
    $phone = $_POST['phone']; // required
    $email_from = $_POST['email']; // required

    $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 .= "Your Name: ".clean_string($your_name)."\n";
    $email_message .= "Type: ".clean_string($type)."\n";
    $email_message .= "Guests: ".clean_string($guests)."\n";
    $email_message .= "Date: ".clean_string($date)."\n";
    $email_message .= "Phone: ".clean_string($phone)."\n";
    $email_message .= "Email: ".clean_string($email_from)."\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); 
?>

<!-- include your own success html here -->

<?php

   header( 'Location: /thanks.aspx' ) ;

?>

<?php
}
?>

3 个答案:

答案 0 :(得分:2)

搜索专栏:

$email_to = "emailremoved@sample.com";

并继续添加逗号分隔电子邮件:

$email_to = "emailremoved@sample.com,emailremoved@sample.com,emailremoved@sample.com";

答案 1 :(得分:2)

PHP的mail()函数在“to”字段方面非常通用。 See the documentation here。列出的任何一个例子都没问题:

user@example.com
user@example.com, anotheruser@example.com
User <user@example.com>
User <user@example.com>, Another User <anotheruser@example.com>

因此,在第5行设置后,您的$email_to变量未被清除或以其他方式修改,您应该只能用逗号分隔2(如上面的示例中我从中复制的那样)我链接到的文档。)

答案 2 :(得分:0)

试试这个! 这是唯一对我有用的代码。

$header .= 'Bcc: someaddress@email.com';