Php Send-mail Form To Multiple Email Addresses
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' bu
Solution 1:
You implode an array of recipients:
$recipients = array('jack@gmail.com', 'jill@gmail.com');
mail(implode(',', $recipients), $submit, $message, $headers);
See the PHP: Mail function reference - 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:
user@example.com
user@example.com
,anotheruser@example.com
- User
<user@example.com>
- User
<user@example.com>
, Another User <anotheruser@example.com
>
Solution 2:
Just add multiple recipients comma seperated in your $mail_to
variable like so:
$mail_to= 'nobody@example.com,anotheruser@example.com,yetanotheruser@example.com';
See mail() function in PHP
Solution 3:
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_fieldsas$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 errorsif (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($errorsas$invalid_field_msg) {
echo"<p>{$invalid_field_msg}</p>";
}
}
}
Post a Comment for "Php Send-mail Form To Multiple Email Addresses"