PHP新手求助:如何实现提交后发送邮件的联系表单
实现PHP提交后发送邮件的联系表单
Hey there! Since you're new to PHP, let's break this down step by step to get your maintenance form sending emails after submission. I'll start by fixing up your HTML form and then walk you through the PHP logic.
第一步:完善你的HTML表单
首先,你的表单需要明确指定提交的目标PHP文件,并且给每个输入字段加上name属性(PHP需要通过这个来获取用户输入)。这里是补全后的可用版本:
<section class="mbr-section form1 cid-qL6aon4aVW" id="form1-11"> <div class="container"> <div class="row justify-content-center"> <div class="title col-12 col-lg-8"> <h2 class="mbr-section-title align-center pb-3 mbr-fonts-style display-2">MAINTENANCE FORM</h2> <h3 class="mbr-section-subtitle align-center mbr-fonts-style display-5">Send us your maintenance request</h3> </div> </div> <div class="row justify-content-center"> <div class="col-lg-8 mx-auto mbr-form" data-form-type="formoid"> <!-- 指定提交到PHP处理脚本,用POST方法更安全 --> <form action="send-maintenance.php" method="POST" class="mbr-form form-with-styler"> <!-- 姓名输入 --> <div class="row"> <div class="col-md-12 mb-3"> <label for="name" class="form-label">Your Name</label> <input type="text" class="form-control" id="name" name="name" required> </div> </div> <!-- 邮箱输入 --> <div class="row"> <div class="col-md-12 mb-3"> <label for="email" class="form-label">Your Email</label> <input type="email" class="form-control" id="email" name="email" required> </div> </div> <!-- 维护需求内容 --> <div class="row"> <div class="col-md-12 mb-3"> <label for="request" class="form-label">Maintenance Request Details</label> <textarea class="form-control" id="request" name="request" rows="5" required></textarea> </div> </div> <!-- 提交按钮 --> <div class="row"> <div class="col-md-12 text-center"> <button type="submit" class="btn btn-primary" name="submit">Send Request</button> </div> </div> </form> </div> </div> </div> </section>
关键细节:
action="send-maintenance.php":表单提交后会跳转到这个PHP文件处理逻辑method="POST":用POST传递数据,避免敏感信息暴露在URL中- 每个输入都加了
name属性(比如name="name"),PHP要靠这个获取用户输入值 - 添加
required属性,让浏览器先做基础的字段非空验证
第二步:编写PHP邮件处理脚本(send-maintenance.php)
接下来创建send-maintenance.php文件,用来处理表单提交并发送邮件。这里提供两种方案,推荐第二种更可靠的:
方案1:使用PHP内置mail()函数(简单但易被拦截)
这是最基础的方法,但很多服务器会默认拦截mail()发送的邮件(容易被标记为垃圾邮件)。
<?php // 检查是否为POST提交,防止直接访问该脚本 if ($_SERVER["REQUEST_METHOD"] == "POST") { // 获取并过滤表单数据 $name = trim($_POST["name"]); $email = trim($_POST["email"]); $request = trim($_POST["request"]); // 验证输入合法性 if (!empty($name) && !empty($email) && filter_var($email, FILTER_VALIDATE_EMAIL) && !empty($request)) { // 邮件配置 $to = "your-email@example.com"; // 替换成你的接收邮箱 $subject = "New Maintenance Request from $name"; // 纯文本邮件内容 $message = "Name: $name\n"; $message .= "Email: $email\n\n"; $message .= "Maintenance Request:\n$request"; // 邮件头信息 $headers = "From: $name <$email>\r\n"; $headers .= "Reply-To: $email\r\n"; // 发送邮件 if (mail($to, $subject, $message, $headers)) { echo "<h3>Thank you! Your maintenance request has been sent.</h3>"; } else { echo "<h3>Oops! Something went wrong. Please try again later.</h3>"; } } else { echo "<h3>Please fill out all fields with valid information.</h3>"; } } else { // 如果直接访问该脚本,跳转回表单页面 header("Location: index.html"); exit; } ?>
方案2:使用PHPMailer(更可靠,推荐)
PHPMailer是一个专业的PHP邮件库,支持SMTP发送,不容易被拦截,还支持HTML格式邮件。
- 先获取PHPMailer:可以手动下载源码,或者用Composer安装(
composer require phpmailer/phpmailer) - 编写处理脚本:
<?php use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\Exception; // 引入PHPMailer文件(路径根据你的实际部署调整) require 'PHPMailer/src/Exception.php'; require 'PHPMailer/src/PHPMailer.php'; require 'PHPMailer/src/SMTP.php'; if ($_SERVER["REQUEST_METHOD"] == "POST") { $name = trim($_POST["name"]); $email = trim($_POST["email"]); $request = trim($_POST["request"]); // 验证输入 if (!empty($name) && !empty($email) && filter_var($email, FILTER_VALIDATE_EMAIL) && !empty($request)) { $mail = new PHPMailer(true); try { // SMTP服务器配置(以Gmail为例,可替换为你的邮件服务商) $mail->isSMTP(); $mail->Host = 'smtp.gmail.com'; $mail->SMTPAuth = true; $mail->Username = 'your-gmail-email@gmail.com'; // 你的Gmail账号 $mail->Password = 'your-app-password'; // Gmail需要用应用密码(需开启两步验证) $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; $mail->Port = 465; // 设置收件人和发件人 $mail->setFrom($email, $name); $mail->addAddress('your-email@example.com'); // 接收邮件的邮箱 // 邮件内容 $mail->isHTML(true); $mail->Subject = "New Maintenance Request from $name"; $mail->Body = " <h3>New Maintenance Request</h3> <p><strong>Name:</strong> $name</p> <p><strong>Email:</strong> $email</p> <p><strong>Request Details:</strong></p> <p>$request</p> "; // 纯文本版本,供不支持HTML的邮箱客户端 $mail->AltBody = "Name: $name\nEmail: $email\n\nMaintenance Request:\n$request"; $mail->send(); echo "<h3>Thank you! Your maintenance request has been sent successfully.</h3>"; } catch (Exception $e) { echo "<h3>Oops! Could not send the email. Error: {$mail->ErrorInfo}</h3>"; } } else { echo "<h3>Please fill out all fields with valid information.</h3>"; } } else { header("Location: index.html"); exit; } ?>
第三步:重要注意事项
- 服务器配置:如果用
mail()函数,要确保你的服务器已配置邮件服务(如Sendmail、Postfix);如果用SMTP,需确认邮件服务商允许SMTP访问(比如Gmail要开启两步验证并生成应用密码)。 - 安全防护:
- 一定要验证用户输入,防止垃圾邮件和注入攻击(比如用
trim()过滤空格、filter_var()验证邮箱格式) - 可以添加验证码(如Google reCAPTCHA)阻止机器人提交表单
- 一定要验证用户输入,防止垃圾邮件和注入攻击(比如用
- 测试建议:先在本地环境(如XAMPP、WAMP)测试,上线后记得检查垃圾邮件文件夹,避免邮件被误判。
内容的提问来源于stack exchange,提问作者Jacob Langreck
相关产品推荐
相关产品推荐

