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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14541839/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 07:29:10  来源:igfitidea点击:

PHP Send-Mail form to multiple email addresses

phphtmlforms

提问by Willard

I'm very new to PHP and am using a basic template 'send-mail' form on a contact page. It's been requested that I send the email out to multiple email addresses when the "Submit" button is clicked. I've searched around & haven't quite found what I needed. What code do I need to add into the form below in order to send this out to multiple email addresses?

我对 PHP 非常陌生,我在联系页面上使用了一个基本的模板“发送邮件”表单。有人要求我在单击“提交”按钮时将电子邮件发送到多个电子邮件地址。我已经四处搜索并没有完全找到我需要的东西。我需要在下面的表单中添加什么代码才能将其发送到多个电子邮件地址?

<?php     

$mail_to = '[email protected]'; // 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 [email protected]');

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

<?php

    }

?>

Your help is greatly appreciated.

非常感谢您的帮助。

回答by ajtrichards

You implode an array of recipients:

您内爆了一组收件人:

$recipients = array('[email protected]', '[email protected]');

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

See the PHP: Mail function reference - http://php.net/manual/en/function.mail.php

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

Receiver, or receivers of the mail.

收件人或邮件的收件人。

The formatting of this string must comply with ? RFC 2822. Some examples are:

此字符串的格式必须符合 ? RFC 2822。一些例子是:

回答by ahmed.hoban

Just add multiple recipients comma seperated in your $mail_tovariable like so:

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

$mail_to = '[email protected],[email protected],[email protected]';

See mail()function in PHP

参见 PHP 中的mail()函数

回答by istos

Here is a simple example:

这是一个简单的例子:

<?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 = "[email protected], [email protected]";

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