简体   繁体   中英

Contact form handler suddenly doesn’t send emails to Gmail

I did a “raw” PHP contact form sender a year ago, tested many times, it worked. It should just send an email to a Gmail account with the submitted email address or contact form parameters included in email body.

Now on redesign I got back to the contact form, tested it randomly and it doesn't send anything at all. No error messages, the submitted form never arrives in the main Gmail inbox, unlike it used to.

<?php
$errors = '';
$myemail = 'x@gmail.com';
$noreply = 'x@y.com';

if (empty($_POST['email']))
{
    $errors .= "\n Error: Please provide an e-mail address where we can send the newsletter and special deals";
}

$email_address = $_POST['email'];

if (!preg_match(
    "/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i",
    $email_address))
{
    $errors .= "\n Error: Invalid email address";
}

if (empty($errors))
{
    $to = $myemail;
    $email_subject = "Contact form submission: $email_address";
    $email_body = "You have received a newsletter signup. ".
        "Email: $email_address";

    $headers = "From: $noreply\n";
    $headers .= "Reply-To: $email_address";

    mail($to,$email_subject,$email_body,$headers);
    //redirect to the 'thank you' page
    header('Location: newsletter_submit.html');
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <title>Contact form handler</title>
    </head>
    <body>
    <!-- This page is displayed only if there is some error -->
    <?php
    echo nl2br($errors);
    ?>
    </body>
</html>

It did happen with another similar form on this website, I wonder if something's wrong with the code (althought it worked once) or Gmail changed something. I tried whitelisting the form sender email, but nothing changed.

It turned out that our mail server rejected same-domain emails. I changed the "myemail" to a gmail account, and the "noreply" to the actual "email server" mail address, then the forms suddenly appeared again in the gmail inbox. -.-

Anyway, thanks for the ideas, it helped me narrowing down the possible bugs list.

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