首页 > 解决方案 > 我有一个 HTML 表单,它使用 POST 请求和 PHP 文件自动向我发送电子邮件,但它不起作用……有什么建议吗?

问题描述

我有一个表单,它使用 HTML 表单、http post 请求和 PHP 后端来自动向我发送用户输入表单的数据。提交按预期工作,但我没有收到电子邮件。我的电子邮件也是由 Outlook 运行的。不确定它是否是加密的东西。

HTML 代码

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

标签: javascriptphphtmlpost

解决方案


这可能不是完整的解决方案,但据我所知
"New Vrify subscription: $email_address"
是无效的。相反,使用.运算符连接它们
"New Vrify subscription:".$email_address

对您拥有的其他变量执行相同的操作,我在处理以下 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");
}

推荐阅读