2

我对 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

    }

?>

非常感谢您的帮助。

4

3 回答 3

11

您内爆了一组收件人:

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

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

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

收件人,或邮件的接收者。

此字符串的格式必须符合 » RFC 2822。一些示例如下:

  • user@example.com
  • user@example.com,anotheruser@example.com
  • 用户<user@example.com>
  • 用户<user@example.com>, 另一个用户 < anotheruser@example.com>
于 2013-01-26T21:40:46.700 回答
4

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

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

参见 PHP 中的mail()函数

于 2013-01-26T21:42:43.843 回答
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>";
        }
    }
}
于 2013-01-26T22:30:31.033 回答