0

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');
}
?>
hppycoder
  • 1,016
  • 1
  • 6
  • 13
Tom
  • 196
  • 1
  • 10
  • Is this post similar? https://stackoverflow.com/questions/18379238/send-email-with-php-from-html-form-on-submit-with-the-same-script?rq=1 – hppycoder Mar 26 '21 at 20:25
  • 1
    For mailing I often use [SwiftMailer](https://swiftmailer.symfony.com/), or [PHPMailer](https://github.com/PHPMailer/PHPMailer) that has more features than the `mail()` function. It also allows easier access to SMTP servers that can send on your behalf as well. – hppycoder Mar 26 '21 at 20:26
  • where are you hosting your web site? – DCR Mar 26 '21 at 20:32
  • Does this answer your question? [PHP mail function doesn't complete sending of e-mail](https://stackoverflow.com/questions/24644436/php-mail-function-doesnt-complete-sending-of-e-mail) – Tangentially Perpendicular Mar 26 '21 at 20:58
  • No that post doesn't work. @hppycoder – Tom Mar 26 '21 at 21:40
  • @DCR I am on bluehost – Tom Mar 26 '21 at 21:40
  • @TangentiallyPerpendicular no it does not – Tom Mar 26 '21 at 21:42
  • @Tom That's great then please put the output from Tangentially's post "Make sure error reporting is enabled and set to report all errors" section. We can then see why your server isn't sending the e-mail. The PHP function may be successful but the server might not be configured to send email – hppycoder Mar 26 '21 at 23:47

1 Answers1

0

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");
}