php - 从 HTML 表单发送电子邮件

标签 php html forms email

<?php 
$errors = '';
$myemail = 'my@email.com';//<-----Put Your email address here.
if(empty($_POST['name'])  || 
   empty($_POST['email']) || 
   empty($_POST['message']))
{
    $errors .= "\n Error: all fields are required";
}

$name = $_POST['name']; 
$email_address = $_POST['email']; 
$message = $_POST['message']; 

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: $name";
    $email_body = "You have received a new message. ".
    " Here are the details:\n Name: $name \n Email: $email_address \n Message \n $message"; 

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

    mail($to,$email_subject,$email_body,$headers);
    //redirect to the 'thank you' page
    header('Location: contact-form-thank-you.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>

这是我用来发送电子邮件的 PHP 文件,请检查。 以下是 HTML 代码。

我在服务器上试过它也给我错误“所有字段都是必需的” 基本上,变量不会从 HTML 表单中获取任何数据。

<form action="email.php" method="post" name="contact_form" enctype="text/plain">
  <div class="form-group" >
    <label for="name">Name</label>
    <input class="form-control" type="text" name="name" id="name" placeholder="Full Name" style="border-radius: 0px; margin-bottom: 3%">

    <label for="email">Email address</label>
    <input type="email" class="form-control" name="email" id="email" aria-describedby="emailHelp" placeholder="Enter email" style="border-radius: 0px; margin-bottom: 3%">

    <label for="message">Message</label>
    <textarea class="form-control" rows="5" name="message" id="message" style="border-radius: 0px; margin-bottom: 3%"></textarea>

    <button type="submit" class="btn btn-default" style="border-radius: 0px; background-color: midnightblue; box-shadow: 2px 2px 2px #888888; color: white;">Submit</button>
  </div>
</form>

最佳答案

我已经从您的表单中删除了enctype。也在同一页写脚本。尝试使用此代码(将此脚本另存为 index.php):

<?php
if (isset($_POST['send_email'])) {
    $errors = '';
    $myemail = 'my@email.com'; //<-----Put Your email address here.
    if (empty($_POST['name']) || empty($_POST['email']) || empty($_POST['message'])) {
        $errors .= "\n Error: all fields are required";
    }
    $name = $_POST['name'];
    $email_address = $_POST['email'];
    $message = $_POST['message'];
    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: $name";
        $email_body = "You have received a new message. " .
            " Here are the details:\n Name: $name \n Email: $email_address \n Message \n $message";
        $headers = "From: $myemail\n";
        $headers .= "Reply-To: $email_address";
        if (mail($to, $email_subject, $email_body, $headers)) {
            echo 'mail sent';
        } else {
            echo 'mail not sent';
        }
    }
}
?>
<form action="index.php" method="post" name="contact_form">
<div class="form-group" >
    <label for="name">Name</label>
    <input class="form-control" type="text" name="name" id="name" placeholder="Full Name" style="border-radius: 0px; margin-bottom: 3%">
    <label for="email">Email address</label>
    <input type="email" class="form-control" name="email" id="email" aria-describedby="emailHelp" placeholder="Enter email" style="border-radius: 0px; margin-bottom: 3%">
    <label for="message">Message</label>
    <textarea class="form-control" rows="5" name="message" id="message" style="border-radius: 0px; margin-bottom: 3%"></textarea>
    <button type="submit" name="send_email" class="btn btn-default" style="border-radius: 0px; background-color: midnightblue; box-shadow: 2px 2px 2px #888888; color: white;">Submit</button>
</div>

关于php - 从 HTML 表单发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40646350/

相关文章:

Javascript 或 Jquery - 将输入字段限制为仅 8 位小数

php - 设置对 PHP 函数的调用,将 POST 变量作为参数,作为 HTML 表单操作

php - 使用 PHP (Code Igniter) 和 MongoDB 构建用户系统

php - 一个提交按钮需要一种形式的输入字段,但其他按钮不需要?

jquery - 通过隐藏 iFrame 上传文件 - 表单嵌套问题

javascript - 添加列表组项,其中包含使用 JS 将输入输入到 ul 的表单

php - ionCube 安装::缺少 zend_extension CentOs

javascript - 更改谷歌地图中的javascript不会更新它?

javascript - 使用 jQuery 更改元素文本和数据值?

javascript - 在 Form.Item 内使用 value 属性进行选择不会反射(reflect)该 value 属性的任何更改