Reputation: 68
I created a php email form for a contact us page to my site, and want to use the same form for multiple email addresses. However, when I copy the code to the new page and add a different email address, it goes to the same email address as the original contact form.
I change the
$email_to = '[email protected], [email protected]';
to a different email address, but it won't work, and keeps sending to the original email address.
How can I make this form usable for multiple emails for multiple email forms?
<?php
// Set email variables
$email_to = '[email protected], [email protected]';
$email_subject = 'Contact Form Submission Bancroft Website';
// Set required fields
$required_fields = array('fullname','email','comment');
// set error messages
$error_messages = array(
'fullname' => 'Please enter a Name to proceed.',
'email' => 'Please enter a valid Email Address to continue.',
'comment' => 'Please enter your Message to continue.'
);
// Set form status
$form_complete = FALSE;
// configure validation array
$validation = array();
// check form submittal
if(!empty($_POST)) {
// Sanitise POST array
foreach($_POST as $key => $value) $_POST[$key] = remove_email_injection(trim($value));
// Loop into required fields and make sure they match our needs
foreach($required_fields as $field) {
// the field has been submitted?
if(!array_key_exists($field, $_POST)) array_push($validation, $field);
// check there is information in the field?
if($_POST[$field] == '') array_push($validation, $field);
// validate the email address supplied
if($field == 'email') if(!validate_email_address($_POST[$field])) array_push($validation, $field);
}
// basic validation result
if(count($validation) == 0) {
// Prepare our content string
$email_content = 'New Website Comment: ' . "\n\n";
// simple email content
foreach($_POST as $key => $value) {
if($key != 'submit') $email_content .= $key . ': ' . $value . "\n";
}
// if validation passed ok then send the email
mail($email_to, $email_subject, $email_content);
// Update form switch
$form_complete = TRUE;
}
}
function validate_email_address($email = FALSE) {
return (preg_match('/^[^@\s]+@([-a-z0-9]+\.)+[a-z]{2,}$/i', $email))? TRUE : FALSE;
}
function remove_email_injection($field = FALSE) {
return (str_ireplace(array("\r", "\n", "%0a", "%0d", "Content-Type:", "bcc:","to:","cc:"), '', $field));
}
?>
If you need more code, let me know
Upvotes: 1
Views: 3097
Reputation: 4748
As far as I know you can't specify multiple email addresses in the first parameter of the mail() function. EDIT - you can, but I had assumed this wasn't possible as it has caused my problems in the past.
Here is an edited version of your code. I split $email_to up by commas and then loop through each email address to send an email to each.
I have also added some if statements to control who the recipients are. If you are going to re-use the code for multiple forms then pass a formID
as a hidden field and then you can specify in the PHP who should receive the emails. No need to duplicate the PHP code for each form then ...
<?php
// Set email variables
if($_POST['formID'] == 1) {
$email_to = '[email protected], [email protected]';
} elseif($_POST['formID'] == 2) {
$email_to = '[email protected], [email protected]';
} else {
$email_to = '[email protected], [email protected]';
}
$email_subject = 'Contact Form Submission Bancroft Website';
// Set required fields
$required_fields = array('fullname','email','comment');
// set error messages
$error_messages = array(
'fullname' => 'Please enter a Name to proceed.',
'email' => 'Please enter a valid Email Address to continue.',
'comment' => 'Please enter your Message to continue.'
);
// Set form status
$form_complete = FALSE;
// configure validation array
$validation = array();
// check form submittal
if(!empty($_POST)) {
// Sanitise POST array
foreach($_POST as $key => $value) $_POST[$key] = remove_email_injection(trim($value));
// Loop into required fields and make sure they match our needs
foreach($required_fields as $field) {
// the field has been submitted?
if(!array_key_exists($field, $_POST)) array_push($validation, $field);
// check there is information in the field?
if($_POST[$field] == '') array_push($validation, $field);
// validate the email address supplied
if($field == 'email') if(!validate_email_address($_POST[$field])) array_push($validation, $field);
}
// basic validation result
if(count($validation) == 0) {
// Prepare our content string
$email_content = 'New Website Comment: ' . "\n\n";
// simple email content
foreach($_POST as $key => $value) {
if($key != 'submit') $email_content .= $key . ': ' . $value . "\n";
}
// if validation passed ok then send the email
$email = explode(',', $email_to);
foreach($email as $e) {
$e = trim($e);
mail($e, $email_subject, $email_content);
}
// Update form switch
$form_complete = TRUE;
}
}
function validate_email_address($email = FALSE) {
return (preg_match('/^[^@\s]+@([-a-z0-9]+\.)+[a-z]{2,}$/i', $email))? TRUE : FALSE;
}
function remove_email_injection($field = FALSE) {
return (str_ireplace(array("\r", "\n", "%0a", "%0d", "Content-Type:", "bcc:","to:","cc:"), '', $field));
}
?>
Upvotes: 2