将表格邮件发送给多人PHP?

时间:2017-02-28 12:25:32

标签: php email contact-form

我的表单按预期正常工作但由于某种原因,电子邮件只会发送到我的某个电子邮件帐户,而不是另一个我在电子邮件字段中输入正确的电子邮件,因此不是问题,但我可以&似乎看到我出错的地方我认为这是因为我使用$ email抓住第二封电子邮件的电子邮件地址...这里是我的php所在我错了吗?

<?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.');
    }
?>

如果有人能破解它,请提前感谢

2 个答案:

答案 0 :(得分:4)

在您定义$email时未指定$to,因此未设置第二个地址。

演示:https://3v4l.org/QIIJu

解决方案,将$to作业移至后面的脚本中。还使用错误报告,这会抛出一个未定义的变量通知。

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

因为此时定义了$email。也不要使用错误抑制,这只是隐藏有用的信息。如果您不希望它显示,则隐藏错误显示但仍记录它们。

答案 1 :(得分:0)

您需要在$ to

中添加多个电子邮件地址
$to = "address@one.com, address@two.com, address@three.com"

需要是逗号分隔的电子邮件地址列表。

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

由于