PHP表单数据发送:新增向发件人发送回执邮件的需求
实现建议:给表单提交者发送确认邮件
先整理下你的环境信息:
- PHP version: 5.3.3
- PHPMailer version: 6.0.5
- CentOS release: 6.9
你当前的表单邮件脚本运行正常,代码如下:
// Only process POST reqeusts. if ($_SERVER["REQUEST_METHOD"] == "POST") { // Get the form fields and remove whitespace. $name = strip_tags(trim($_POST["name"])); $phone = trim($_POST["phone"]); $email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL); // Check that data was sent to the mailer. if ( empty($name) OR empty($phone)) { // Set a 400 (bad request) response code and exit. http_response_code(400); echo "Oops! There was a problem with your submission. Please complete the form and try again."; exit; } // Set the recipient email address. $recipient = "email@domain.com"; // Set the email subject. $emailsubject = "New contact from $name"; // Build the email content. $email_content = "Name: $name\n"; $email_content .= "Email: $email\n\n"; $email_content .= "Phone:\n$phone\n"; // Build the email headers. $email_headers = "From: $name <$email>"; // Send the email. if (mail($recipient, $emailsubject, $email_content, $email_headers)) { // Set a 200 (okay) response code. http_response_code(200); echo 'Thank You! Your message has been sent.'; } else { // Set a 500 (internal server error) response code. http_response_code(500); echo "Oops! Something went wrong and we couldn't send your message."; } } else { // Not a POST request, set a 403 (forbidden) response code. http_response_code(403); echo "There was a problem with your submission, please try again."; }
现在要给提交者的邮箱发送确认邮件,我推荐两种实现方式,考虑到你已经安装了PHPMailer,优先用它会更可靠(比原生mail()函数更稳定,还能降低邮件被标记为垃圾邮件的概率):
方案一:使用PHPMailer发送两封邮件
PHPMailer已经帮你处理了很多邮件发送的细节,比如SMTP配置、编码问题等,修改起来也很方便。下面是完整的修改后的脚本:
// 引入PHPMailer的核心类(请根据你的实际文件路径调整) use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\Exception; require 'path/to/PHPMailer/src/Exception.php'; require 'path/to/PHPMailer/src/PHPMailer.php'; require 'path/to/PHPMailer/src/SMTP.php'; // Only process POST requests. if ($_SERVER["REQUEST_METHOD"] == "POST") { // Get the form fields and remove whitespace. $name = strip_tags(trim($_POST["name"])); $phone = trim($_POST["phone"]); $email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL); // 新增对邮箱字段的校验:要给用户发邮件,必须确保这个字段不为空且格式有效 if (empty($name) OR empty($phone) OR !filter_var($email, FILTER_VALIDATE_EMAIL)) { http_response_code(400); echo "Oops! There was a problem with your submission. Please complete the form and enter a valid email address."; exit; } try { // 初始化PHPMailer实例 $mail = new PHPMailer(true); // 推荐配置SMTP服务器(替换成你自己的邮箱SMTP信息,这能大幅提升邮件到达率) // $mail->isSMTP(); // $mail->Host = 'smtp.yourdomain.com'; // 比如smtp.gmail.com或者你域名的SMTP // $mail->SMTPAuth = true; // $mail->Username = 'your-email@domain.com'; // $mail->Password = 'your-email-password'; // $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; // $mail->Port = 587; // 如果不想用SMTP,也可以继续使用服务器的原生mail()函数 $mail->isMail(); // 设置发件人:建议用你自己域名的noreply邮箱,比如noreply@yourdomain.com,避免被拦截 $mail->setFrom('noreply@yourdomain.com', 'Your Website Name'); // -------------------------- // 1. 发送给管理员的邮件(原逻辑) // -------------------------- $adminRecipient = "email@domain.com"; $adminSubject = "New contact from $name"; $adminContent = "Name: $name\n"; $adminContent .= "Email: $email\n\n"; $adminContent .= "Phone:\n$phone\n"; $mail->addAddress($adminRecipient); $mail->Subject = $adminSubject; $mail->Body = $adminContent; $mail->send(); // -------------------------- // 2. 发送给提交者的确认邮件 // -------------------------- // 确认邮件内容,基于你提供的示例扩展 $userSubject = "Thank you for your submission!"; $userContent = "Thank you for your submission. These are the details you submitted:\n\n"; $userContent .= "Name: $name\n"; $userContent .= "Email: $email\n"; $userContent .= "Phone: $phone\n\n"; $userContent .= "We'll review your message and get back to you as soon as possible."; // 清空之前的收件人,添加提交者的邮箱 $mail->clearAddresses(); $mail->addAddress($email); $mail->Subject = $userSubject; $mail->Body = $userContent; $mail->send(); // 发送成功后的响应 http_response_code(200); echo 'Thank You! Your message has been sent, and a confirmation email has been sent to your inbox.'; } catch (Exception $e) { // 发送失败的响应,同时输出错误信息方便排查 http_response_code(500); echo "Oops! Something went wrong and we couldn't send your message. Error: {$mail->ErrorInfo}"; } } else { // Not a POST request, set a 403 (forbidden) response code. http_response_code(403); echo "There was a problem with your submission, please try again."; }
方案一的注意点:
- 务必调整PHPMailer的引入路径,确保和你的项目文件结构匹配
- 强烈建议配置SMTP信息,用自己域名的邮箱发件,这能避免邮件被当成垃圾邮件拦截
- 新增了对邮箱格式的校验,确保提交的邮箱是有效的
方案二:继续使用原生mail()函数发送确认邮件
如果你暂时不想切换到PHPMailer,也可以在原脚本的基础上直接添加一段mail()调用,在管理员邮件发送成功后触发:
修改后的核心代码片段(替换原脚本的邮件发送部分):
// Send the email. if (mail($recipient, $emailsubject, $email_content, $email_headers)) { // -------------------------- // 新增:发送确认邮件给提交者 // -------------------------- $userSubject = "Thank you for your submission!"; // 确认邮件内容,基于你提供的示例 $userContent = "Thank you for your submission. These are the details you submitted:\n\n"; $userContent .= "Name: $name\n"; $userContent .= "Email: $email\n"; $userContent .= "Phone: $phone\n\n"; $userContent .= "We appreciate your message and will contact you shortly."; // 设置确认邮件的头信息,注意不要用用户的邮箱作为发件人,否则容易被拦截 $userHeaders = "From: Your Website Name <noreply@yourdomain.com>\r\n"; $userHeaders .= "Reply-To: email@domain.com\r\n"; // 让用户回复时能发到你的工作邮箱 // 发送确认邮件 mail($email, $userSubject, $userContent, $userHeaders); // Set a 200 (okay) response code. http_response_code(200); echo 'Thank You! Your message has been sent, and a confirmation email has been sent to your inbox.'; } else { // Set a 500 (internal server error) response code. http_response_code(500); echo "Oops! Something went wrong and we couldn't send your message."; }
方案二的注意点:
- 发件人一定要用自己域名的邮箱,不要使用用户的邮箱作为发件人,很多邮件服务商都会拦截这种“伪造发件人”的邮件
- 可以添加
Reply-To头,方便用户直接回复到你的工作邮箱
最后,你提到已经知晓PHP 5.3.3的安全隐患,虽然当前脚本可以正常运行,但还是建议尽早升级PHP版本——毕竟PHP 5.3已经停止官方支持多年,存在不少未修复的安全漏洞,升级到更高版本(比如PHP 7.4或以上)会更安全稳定。
内容的提问来源于stack exchange,提问作者TheOrdinaryGeek
相关产品推荐
相关产品推荐

