Php 电子邮件未发送

Php email not sending

我的代码片段附在下面。

我已经设法让它在另一个页面上运行,所以我只是复制并粘贴了该过程和前端代码,并将变量更改为新页面,但它不起作用。我的 PHP 日志没有显示任何未定义的内容,也没有抛出任何错误。我正在使用 phpmailer 并且电子邮件身份验证在另一个文件中处理,该文件已确认可以正常工作,因此问题不存在。

我认为问题出在 process.php 文件中。

前端:

            <form class="contact" name="contact">
                <input type="hidden" name="to_email"  value="<?= $this->user->user_email; ?>" />

                <p>Enter your enquiry on the form below.</p> 
                <div class="input-group">
                <span class="input-group-addon" id="basic-addon1">Message</span>
                <textarea class="form-control" name="message" rows="5"></textarea>
                </div>
                <br><br>
                <div class="input-group">
                <span class="input-group-addon" id="basic-addon1">Phone Number</span>
                <input type="text" style="color: #333333" name="phone" class="form-control" placeholder="Optional" aria-describedby="basic-addon1"></input>
                <span class="input-group-addon" id="basic-addon1">Contact Name</span>
                <input type="text" style="color: #333333" name="from_name" class="form-control" aria-describedby="basic-addon1" value="<?= $this->user->user_contact_name; ?>"></input>
                </div>
                </form>

前端脚本:

    $(document).ready(function () {
        $("input#submit").click(function(){
            $.ajax({
                type: "POST",
                url: "profile/process", // 
                data: $('form.contact').serialize(),
                success: function(msg){
                    alert("Your message has been sent, thanks!");   
                },
                error: function(){
                    alert("failure");
                }
            });
        });
    });
</script>

Process.php

<?php
$to = strip_tags($_POST['to_email']);
if (isset($_POST['name'])) {
$name = strip_tags($_POST['from_name']);
$email = 'example@example.com';
$message = strip_tags($_POST['message']);
$email_body = "You have received a new message. ".
" Here are the details:\n Name: $name \n ".
"Email: $email\n Message \n $message";
    $mail = new Mail;
    $mail_sent = $mail->sendMail(
        $to,
        $email,
        $name,
        Config::get('EMAIL_CONTACT_SUBJECT'),
        $email_body
    );
}?>

事情看起来不对劲:

<?php
$to = strip_tags($_POST['to_email']);
$name = isset($_POST['name'])?strip_tags($_POST['from_name']):"";
$email = 'example@example.com';
$message = strip_tags($_POST['message']);
$email_body = "You have received a new message. ".
        " Here are the details:\n Name: $name \n ".
        "Email: $email\n Message \n $message";
$mail = new Mail;
$mail_sent = $mail->sendMail(
        $to,
        $email,
        $name,
        Config::get('EMAIL_CONTACT_SUBJECT'),
        $email_body
);
?>

如果您使用的是外部邮件服务器:

  • Gmail:您的服务器可能被标记为 "unusual activity",因此发送邮件的过程一直停留在 SMTP 错误:无法验证。例如,当您在马来西亚注册了 Gmail 帐户,但您试图从美国发送邮件时,就会发生这种情况。通过调试检查身份验证是否有错误

     $mail->SMTPDebug = 3; 
    
  • 其他邮件服务提供商:如果您使用其他邮件服务提供商,例如:cPanel 虚拟主机,您可能会被阻止使用服务器发送 unsolicited/mass 邮件,您可以检查调试消息(见上文)以查看发生了什么

如果您使用的是本地邮件服务器:

  • 大多数 ISP 大部分时间都阻止外发邮件以与垃圾邮件作斗争。检查调试消息以查看发生了什么。