PHP发送邮件的形式将多个电子邮件地址(PHP Send-Mail form to multiple

2019-07-18 18:26发布

我很新的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

    }

?>

你的帮助是极大的赞赏。

Answer 1:

你爆收件人的数组:

$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.comanotheruser@example.com
  • 用户<user@example.com>
  • 用户<user@example.com>另一用户< anotheruser@example.com >


Answer 2:

只需添加多个收件人逗号分隔在$mail_to变量,像这样:

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

邮件()在PHP函数



Answer 3:

下面是一个简单的例子:

<?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>";
        }
    }
}


文章来源: PHP Send-Mail form to multiple email addresses
标签: php html forms