简体   繁体   中英

Contact form php script sending spam to Gmail

I created a php script (for a contact form) to send emails to my Gmail account.
If I use the sender email in the header ( $headers = "From: " . $email; ), Gmail reports the received message as spam.
If I don't use the email in the header (eg the sender name $headers = "From: " . $name; ) the message is not reported as spam.
Do you have any suggestion to let me use the email in the header?
Thanks!

<?php

/* Check if the url field is empty (antispam) */
if ($_POST['leaveblank'] != '' or $_POST['dontchange'] != 'http://') {
    $name = $_POST['name'];
    $faillink = "xxx.php";
    header("Location: $faillink");
} else {

    $name = $_POST['name'];
    $email = $_POST['email'];
    $subject_prefix = "[ContactForm]: ";
    $subject = $subject_prefix . $_POST['subject'];
    $message = $_POST['message'];
    $to = "myemail@gmail.com";

    $body = "From: " . $name . "\n";
    $body .= "Email: " . $email . "\n";
    $body .= "Message: " . $message . "\n";

    $headers = "From: " . $email;

    $oklink = "yyy.php";
    $faillink = "xxx.php";

    if ( preg_match( "/[\r\n]/", $name ) || preg_match( "/[\r\n]/", $email ) ) {
        header("Location: $faillink");
    }

    $retmail = mail($to, $subject, $body, $headers);
    if ($retmail) {
        header("Location: $oklink");
    } else {
        header("Location: $faillink");
    }
}

?>

I solved the issue as Iain suggested so I replaced the mail headers as follows:

$headers = "From: " . "noreplay@mydomain.com" . "\r\n";
$headres .= "Reply-To: " . $email . "\r\n";

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