php - 发送电子邮件后页面未在 PHP 中正确加载
问题描述
我正在使用 php mail() 发送电子邮件。使用此功能可以正确发送邮件。但是发送邮件后页面没有正确加载。实际上我想在发送电子邮件后登陆同一页面。以下是我写的代码
if (isset($_POST['submit'])) { // Check if form was submitted
if (empty($_POST['name']) ||
empty($_POST['email']) ||
empty($_POST['phone']) ||
empty($_POST['subject']) ||
empty($_POST['message']) ||
!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
echo "No arguments Provided!";
return false;
}
$name = strip_tags(htmlspecialchars($_POST['name']));
$email_address = strip_tags(htmlspecialchars($_POST['email']));
$phone = strip_tags(htmlspecialchars($_POST['phone']));
$subject = strip_tags(htmlspecialchars($_POST['subject']));
$message = strip_tags(htmlspecialchars($_POST['message']));
// Create the email and send the message
$to = 'myemail@gmail.com'; // Add your email address inbetween the '' replacing yourname@yourdomain.com - This is where the form will send a message to.
$email_subject = "Subjet : $subject";
$email_body = "You have received a new message from your website contact form.\n\n" . "Here are the details:\n\nName: $name\n\nEmail: $email_address\n\nPhone: $phone\n\nMessage:\n$message";
$headers = "From: myemail@gmail.com\n"; // This is the email address the generated message will be from. We recommend using something like noreply@gmail.com.
$headers .= "Reply-To: $email_address";
mail($to, $email_subject, $email_body, $headers);
header("Refresh:0");
return true;
// exit();
}
解决方案
可能是因为邮件发送失败。
您可以通过在 PHP 文件顶部添加以下内容来查看它返回的错误:
ini_set('display_errors', 1);
此外,建议使用PHPMailer,因为该mail()
功能并不总是有效。
推荐阅读
- delphi - 如何返回 VCL for web/Intraweb 中的登录页面?
- angular - Angular-7 如何导航到单页网站的某些部分
- java - How to remove specific String stored in an ArrayList
- python - 数据集的大小如何影响 ARIMA 预测的准确性?
- swift - 如何为 UITableViewCell 添加左右边距
- javascript - 在用户更改其内容后读取输入框的值,可能吗?
- android - NavigationDrawer 中奇怪的单出项目
- node.js - 使用节点 js 检查 mongodb 中的唯一电子邮件
- ssl - 由 nginx 代理的 gerrit 仅在“托管”在顶级目录上时才有效
- javascript - 为什么 chrome.runtime.lastError 不进入