首页 > 解决方案 > 发送电子邮件后页面未在 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();

}

这是联系我们页面 在此处输入图像描述

发送电子邮件后,此页面未完全加载,仅加载标题 在此处输入图像描述

标签: phpgmailphpmailerphpmd

解决方案


可能是因为邮件发送失败。

您可以通过在 PHP 文件顶部添加以下内容来查看它返回的错误:

ini_set('display_errors', 1); 

此外,建议使用PHPMailer,因为该mail()功能并不总是有效。


推荐阅读