简体   繁体   中英

I have a HTML form that uses a POST request and PHP file to send me an email automatically, but it isn't working…any advice?

I have a form that uses an HTML form, http post request, and PHP backend to automatically send me the data that a user inputs into the form. The submission works as expected, but I am not getting an email. My email is also run by outlook. Not sure if it's an encryption thing.

HTML Code

<form action="mail.php" method="POST">
    <div class="email-box">
        <input class="tbox" id="email_box" name="email" type="email" style="cursor: pointer;" placeholder="Enter your email">
        <button class="btn" id="email_btn" type="submit" name="button">Subscribe</button>
    </div>
</form>
<!-- Snipped JavaScript validation -->

PHP

<?php
$errors = '';
$myemail = 'me@website.com';
if (empty($_POST['email'])) {
    $errors .= "\n Error: all fields are required";
}
$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 = "New Vrify subscription: $email_address";
    $email_body = "You have a new subscriber. " .
        "Here are the details:\n Email: $email_address \n " .
        $headers = "From: subscriber@website.com";
    mail($to, $email_subject, $email_body, $headers);
    //redirect to the 'thank you' page
    header('Location: thank-you.html');
}
?>

This may not be the full solution, but as far as I'm aware
"New Vrify subscription: $email_address"
is not valid. Instead, concatenate them using the . operator
"New Vrify subscription:".$email_address

Do the same to the other variables you have, I had the same issue when working on the following php:

if (($_SERVER["REQUEST_METHOD"] ?? 'GET') == "POST") {

    $msg ="New message from mysite!\n".$_POST["message"]."\nReply Email:".$_POST["email"];

    mail("me@gmail.com", time(), $msg,"From: contact@mysite");
}

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