php - 联系表单无法使用 PHP,我只想使用 PHP 表单发送邮件
问题描述
我有 PHP 电子邮件表单,我想使用 PHP 表单发送电子邮件,但这不起作用。
HTML:-
<form action="mail_handler.php" method="post" name="form" class="form-box">
<label for="name">Name</label><br>
<input type="text" name="name" class="inp" placeholder="Enter Your Name" required><br>
<label for="email">Email ID</label><br>
<input type="email" name="email" class="inp" placeholder="Enter Your Email" required><br>
<label for="phone">Phone</label><br>
<input type="tel" name="phone" class="inp" placeholder="Enter Your Phone" required><br>
<label for="message">Message</label><br>
<textarea name="msg" class="msg-box" placeholder="Enter Your Message Here..." required></textarea><br>
<input type="submit" name="submit" value="Send" class="sub-btn">
</form>
PHP:-
<?php
if(isset($_POST['submit'])){
$name=$_POST['name'];
$email=$_POST['email'];
$phone=$_POST['phone'];
$msg=$_POST['msg'];
$to='123@gmail.com'; // Receiver Email ID, Replace with your email ID
$subject='Form Submission';
$message="Name :".$name."Phone :".$phone."Wrote the following :".$msg;
$headers="From: ".$email;
if(mail($to, $subject, $message, $headers)){
echo "<h1>Sent Successfully! Thank you, We will contact you shortly!</h1>";
}
else{
echo "Something went wrong!";
}
}
?>
答案将不胜感激!
解决方案
来自 PHP 手册https://www.php.net/manual/en/function.mail.php。
您在这里没有使用标题。使用下面的代码
// To send HTML mail, the Content-type header must be set. I think you are missing this
$headers[] = 'MIME-Version: 1.0';
$headers[] = 'Content-type: text/html; charset=iso-8859-1';
// Additional headers .
$headers[] = 'From: <'.$email.'>';
// Mail it
if(mail($to, $subject, $message, implode("\r\n", $headers))) //Multiple extra headers should be separated with a CRLF (\r\n).
{
echo "<h1>Sent Successfully! Thank you, We will contact you shortly!</h1>";
}else{
echo "Something went wrong!";
}
推荐阅读
- android - 为什么我的 mapbox 组件(在 Ionic 应用程序中)可以在浏览器/本地主机上运行,但当我将它带到我的 Android 模拟器时却不行?
- mysql - hibernate 和 mysql 和 Operand 应该包含 2 列
- java - 基本 httpurlconnection POST 请求失败,出现 401
- python - TypeError: '>=' 在 'str' 和 'int' 的实例之间不支持 - 尝试添加 int 以输入没有运气
- r - 在 R 的 for 循环中自动绘制嵌套数据帧
- c - Hot Patching 填充是否总是 16 的倍数?
- python - 为什么 Flask 应用程序在退出其 while 循环时仍在运行其线程?
- ruby - 红宝石一号有问题吗?并包括?方法
- node.js - 将节点 js 应用程序部署到 MS azure 服务后出错
- mysql - 从不同的表中查询数据,Union