0

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

ui-unicorn.co.uk
  • 151
  • 1
  • 5
  • 17
  • Possible duplicate of [PHP send mail to multiple email addresses](http://stackoverflow.com/questions/4506078/php-send-mail-to-multiple-email-addresses) – James Hunt Feb 28 '17 at 12:28
  • @JamesHunt I don't think it's a duplicate - the OP has the syntax in place but it's not quite right - your referenced issue is a question about how to create code which matches their requirements. – Kallum Tanton Feb 28 '17 at 12:30
  • I think your problem is, as @chris85 stated, that the `$email` variable doesn't exist. Try moving those three declarations to the start of the document. – Kallum Tanton Feb 28 '17 at 12:32

2 Answers2

4

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.

chris85
  • 23,846
  • 7
  • 34
  • 51
  • I agree, during development Error Reporting can be a massive time-saver. But, if in doubt, echo it out. OP would have seen from the echo that there was nothing being printed next to the first email address. – Kallum Tanton Feb 28 '17 at 12:35
  • Thanks, would it be wise to also define name and message also at the top of the PHP along with $email? – ui-unicorn.co.uk Feb 28 '17 at 12:37
  • @Neths You will get undefined notices if they aren't set but that is my preferred location for assignments. If nothing should happen if it isn't an AJAX request then I'd move the `if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') {` to the start. – chris85 Feb 28 '17 at 12:51
  • It's Ajax I have it working now for some reason it didn't work with $email = $_POST['email']; but it did with $email = filter_var($_POST["email"], FILTER_SANITIZE_EMAIL); a little odd...but yeah I've now defined $email right at the top and it now works fine, thank you for your help. – ui-unicorn.co.uk Feb 28 '17 at 12:59
0

You need to add the multiple email address in $to

$to = "address@one.com, address@two.com, address@three.com"

Needs to be a comma delimited list of email adrresses.

mail($email_to, $email_subject, $thankyou);

Thanks