Neths Neths -4 years ago 144
PHP Question

Sending form mail to more than one person PHP?

my form is working as intended but for some reason the email will only send to one of my email accounts and not the other I am putting the right email in the email field so that isn't the issue however I can't seem to see where I am going wrong I assume it's because I'm using $email to grab the email address to where the 2nd email is suppose to go...here is my php where am I going wrong?

<?php
$from = 'Pixel Wars - Press Inquiry';
$to = "my-email@gmail.com, $email";
$subject = 'Press Inquiry from Pixelwars.com';

function errorHandler ($message) {
die(json_encode(array(
'type' => 'error',
'response' => $message
)));
}

function successHandler ($message) {
die(json_encode(array(
'type' => 'success',
'response' => $message
)));
}


if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$body = "Name: $name\r\n Email: $email\r\n\r\n Message:\r\n $message";

$pattern = '/[\r\n]|Content-Type:|Bcc:|Cc:/i';
if (preg_match($pattern, $name) || preg_match($pattern, $email) || preg_match($pattern, $message)) {
errorHandler('Header injection detected.');
}

// Check if name has been entered
if (!$_POST['name']) {
errorHandler('Please enter your name.');
}

// Check if email has been entered and is valid
if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
errorHandler('Please enter a valid email address.');
}

// Check if message has been entered
if (!$_POST['message']) {
errorHandler('Please enter your message.');
}

// prepare headers
$headers = 'MIME-Version: 1.1' . PHP_EOL;
$headers .= 'Content-type: text/plain; charset=utf-8' . PHP_EOL;
$headers .= "From: $name <$email>" . PHP_EOL;
$headers .= "Return-Path: $to" . PHP_EOL;
$headers .= "Reply-To: $email" . PHP_EOL;
$headers .= "X-Mailer: PHP/". phpversion() . PHP_EOL;

// send the email
$result = @mail($to, $subject, $body . "\r\n\n" .'------------------ '. "\r\n\n" .'Hello '.$name.' we will contact you as soon as possible about your query.' ."\n". 'Dont forget to keep visiting www.pixelwars.com for more updates and awesome content.' ."\n". 'We will email you back on the provided email below, thank you and have a nice day.' . "\r\n\n" .'-- '.$email, $headers);
if ($result) {
successHandler('Thank You! we will be in touch');
} else {
errorHandler('Sorry there was an error sending your message.');
}
} else {
errorHandler('Allowed only XMLHttpRequest.');
}
?>


Thank you in advance if anyone can crack it

Answer Source

You don't have $email assigned when you are defining $to so your second address is not set.

Demo: https://3v4l.org/QIIJu

Solution, move the $to assignment to later in the script. Also use error reporting, this would have thrown an undefined variable notice.

e.g.

<?php
$from = 'Pixel Wars - Press Inquiry';
$subject = 'Press Inquiry from Pixelwars.com';
....

$to = "my-email@gmail.com, $email";
$result = @mail($to, $subject, $body ....

because at this point the $email is defined. Also don't use error suppression, that is just hiding useful information. If you don't want it displayed hide the error displaying but still log them.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download