PHP send email form to multiple email addresses

I am very new to PHP and use the basic "send mail" form on the contact page. It was suggested to send an email address to multiple email addresses when clicking the Send button. I searched around and didn't quite understand what I needed. What code do I need to add to the form below to send it to multiple email addresses?

<?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

    }

?>

      

Your help is greatly appreciated.

+3


source to share


3 answers


You've blown up an array of recipients:

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

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

      

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



The recipient or recipients of the mail.

The formatting of this string must conform to "RFC 2822. Some examples:

  • user@example.com

  • user@example.com

    , anotheruser@example.com

  • User <user@example.com>

  • User <user@example.com>

    , Another user < anotheruser@example.com

    >
+11


source


Just add multiple comma separated recipients in your variable $mail_to

like:

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

      



See the mail () function in PHP

+3


source


Here's 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 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>";
        }
    }
}

      

0


source







All Articles