PHP 联系表发送邮件时出错

PHP contact form error sending mail

我正在尝试将此 php 代码集成到我的联系表中,所有 html 输入都没有问题,php 脚本中一定有错误,这是代码:

    <?php
$to = 'test@email.com;
$subject = 'New Website Message';
$headers = 'From: (Website Form) <POST['email']>' . "\r\n" . 'Content-type: text/html; charset=utf-8';
$message = '
<html>
    <head>
        <title>You have received a new message!</title>
    </head>
    <body>
        <h3>Name: <span style="font-weight: normal;">' . $_POST['name'] . '</span></h3>

        <h3>Email: <span style="font-weight: normal;">' . $_POST['email'] . '</span></h3>

        <div>
            <h3 style="margin-bottom: 5px;">Message:</h3>
            <div>' . $_POST['message'] . '</div>
        </div>
    </body>
</html>';

if (!empty($_POST['name']) && !empty($_POST['email']) && !empty($_POST['message'])) {
    if (filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
        mail($to, $subject, $message, $headers) or die('<span style="color: red;">Error sending Mail</span>');
        echo '<span class="send-true" style="color: #00dd63;">Your email was sent!</span>';
    }
} else {
    echo '<span style="color: red;">All fields must be filled!</span>';
}
?>

看来你是个语法错误,伙计。 试试这个:

    <?php
$to = 'test@email.com';
$subject = 'New Website Message';
$headers = 'From: (Website Form) <$_POST['email']>' . "\r\n" . 'Content-type: text/html; charset=utf-8';
$message = '
<html>
    <head>
        <title>You have received a new message!</title>
    </head>
    <body>
        <h3>Name: <span style="font-weight: normal;">' . $_POST['name'] . '</span></h3>

        <h3>Email: <span style="font-weight: normal;">' . $_POST['email'] . '</span></h3>

        <div>
            <h3 style="margin-bottom: 5px;">Message:</h3>
            <div>' . $_POST['message'] . '</div>
        </div>
    </body>
</html>';

if (!empty($_POST['name']) && !empty($_POST['email']) && !empty($_POST['message'])) {
    if (filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
        mail($to, $subject, $message, $headers) or die('<span style="color: red;">Error sending Mail</span>');
        echo '<span class="send-true" style="color: #00dd63;">Your email was sent!</span>';
    }
} else {
    echo '<span style="color: red;">All fields must be filled!</span>';
}
?>