向多个人发送表单邮件 PHP?

Sending form mail to more than one person PHP?

我的表格按预期工作,但出于某种原因,电子邮件只会发送到我的一个电子邮件帐户,而不是另一个我在电子邮件字段中输入了正确的电子邮件,所以这不是问题,但我可以似乎看不出我哪里出错了,我想是因为我正在使用 $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.');
    }
?>

如果有人能破解就先谢谢了

您在定义 $to 时没有分配 $email,因此您的第二个地址未设置。

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

解决方案,将 $to 赋值移动到脚本的后面。也使用错误报告,这会抛出一个未定义的变量通知。

例如

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

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

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

您需要在 $to

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

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

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

谢谢