简体   繁体   中英

PHP contact form error sending mail

I am trying to integrate this php code to my contact form, all html inputs are fine, there must be an error in the php script, here's the code:

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

Seems like you've a syntax error man. try this one:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM