PHP Send-Mail表单到多个电子邮件地址

时间:2013-01-26 21:38:23

标签: php html forms

我是PHP的新手,并且在联系页面上使用了基本模板“发送邮件”表单。 当点击“提交”按钮时,我被要求将电子邮件发送到多个电子邮件地址。我一直在寻找和还没找到我需要的东西。我需要在下面的表单中添加哪些代码才能将其发送到多个电子邮件地址?

<?php     

$mail_to = 'daniel30293@gmail.com'; // specify your email here


// Assigning data from the $_POST array to variables

$name = $_POST['sender_name'];

$mail_from = $_POST['sender_email'];

$phone = $_POST['sender_phone'];

$web = $_POST['sender_web'];

$company = $_POST['sender_company'];

$addy = $_POST['sender_addy'];

$message = $_POST['sender_message'];


// Construct email subject

$subject = 'Web Prayer Request from ' . $name;


// Construct email body

$body_message = 'From: ' . $name . "\r\n";

$body_message .= 'E-mail: ' . $mail_from . "\r\n";

$body_message .= 'Phone: ' . $phone . "\r\n";

$body_message .= 'Prayer Request: ' . $message;



// Construct email headers

$headers = 'From: ' . $name . "\r\n";

$headers .= 'Reply-To: ' . $mail_from . "\r\n";

$mail_sent = mail($mail_to, $subject, $body_message, $headers);


if ($mail_sent == true){ ?>

<script language="javascript" type="text/javascript">
alert('Your prayer request has been submitted - thank you.');

window.location = 'prayer-request.php';

</script>

<?php } else { ?>

<script language="javascript" type="text/javascript">
alert('Message not sent. Please, notify the site administrator admin@bondofperfection.com');

window.location = 'prayer-request.php';
</script>

<?php

    }

?>

非常感谢您的帮助。

3 个答案:

答案 0 :(得分:12)

你内爆了一系列收件人:

$recipients = array('jack@gmail.com', 'jill@gmail.com');

mail(implode(',', $recipients), $submit, $message, $headers);

请参阅PHP:Mail函数参考 - http://php.net/manual/en/function.mail.php

邮件的接收者或接收者。

此字符串的格式必须符合»RFC 2822.一些示例是:

  • user@example.com
  • user@example.comanotheruser@example.com
  • 用户<user@example.com>
  • 用户<user@example.com>,另一位用户&lt; anotheruser@example.com&gt;

答案 1 :(得分:4)

只需在$mail_to变量中添加多个以逗号分隔的收件人,如下所示:

$mail_to = 'nobody@example.com,anotheruser@example.com,yetanotheruser@example.com';

请参阅 PHP中的 mail() 函数

答案 2 :(得分:0)

这是一个简单的例子:

<?php

// Has the form been submitted?
// formSubmit: <input type="submit" name="formSubmit">
if (isset($_POST['formSubmit'])) {
    // Set some variables
    $required_fields = array('name', 'email');
    $errors = array();

    $success_message = "Congrats! Your message has been sent successfully!";
    $sendmail_error_message = "Oops! Something has gone wrong, please try later.";

    // Cool the form has been submitted! Let's loop through the required fields and check
    // if they meet our condition(s)
    foreach ($required_fields as $fieldName) {
        // If the current field in the loop is NOT part of the form submission -OR-
        // if the current field in the loop is empty, then...
        if (!isset($_POST[$fieldName]) || empty($_POST[$fieldName])) {

            // add a reference to the errors array, indicating that these conditions have failed
            $errors[$fieldName] = "The {$fieldName} is required!";
        }
    }

    // Proceed if there aren't any errors
    if (empty($errors)) {
        $name = htmlspecialchars(trim($_POST['name']), ENT_QUOTES, 'UTF-8' );
        $email = htmlspecialchars(trim($_POST['email']), ENT_QUOTES, 'UTF-8' );

        // Email Sender Settings
        $to_emails = "anonymous1@example.com, anonymous2@example.com";

        $subject = 'Web Prayer Request from ' . $name;
        $message = "From: {$name}";
        $message .= "Email: {$email}";

        $headers = "From: {$name}\r\n";
        $headers .= "Reply-To: {$email}\r\n";
        $headers .= 'X-Mailer: PHP/' . phpversion();

        if (mail($to_emails, $subject, $message, $headers)) {
            echo $success_message;
        } else {
            echo $sendmail_error_message;
        }
    } else {

        foreach($errors as $invalid_field_msg) {
            echo "<p>{$invalid_field_msg}</p>";
        }
    }
}