javascript - PHPMailer SMTP 问题 - 收到信件但网站上有错误

标签 javascript php smtp phpmailer

我遇到以下问题:我的 Bootstrap 4 .html 网站上有一个联系表单。该表单是一个基于 PHPMailer 和 Bootstrap 验证器的简单表单。之前我在没有 SMTP 的情况下从联系表单收到了电子邮件,一切都很好。现在我决定使用 Gmail SMTP 并更改了我的代码。我的 Gmail 收件箱收到了邮件,但信件一发出,我就在网站上看到了错误。

请您告诉我我的问题出在哪里?为了解决这个问题我伤透了脑筋。

ajax.js 文件:

$(document).ready(function() {
function submitForm(){
            var name =$("input[name=name]").val();
            var email =$("input[name=email]").val();
            var comment =$("textarea[name=comment]").val();
            var captcha=grecaptcha.getResponse();
            var form = $('form')[0];
            var formData = new FormData(form);
            formData.append('name', name );
            formData.append('email', email );
            formData.append('comment', comment );
            formData.append('captcha', captcha );
            $.ajax({
                 url: "include/ajax/send.php",
                 type: "POST",
                 contentType: false,
                 processData: false,
                 data: formData,
                 cache: false,
                 success : function(text){
             if (text == "success"){
                 formSuccess();
             } else {
                 formError();
                 submitMSG(false,text);
             }
         }
     });
 }

php 文件:

// send email
$mail = new PHPMailer;
// Email Template

$mail->SMTPOptions = array(
    'ssl' => array(
        'verify_peer' => false,
        'verify_peer_name' => false,
        'allow_self_signed' => true
    )
);

//Tell PHPMailer to use SMTP
$mail->isSMTP();

//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 0;


//Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';

//Set the hostname of the mail server
$mail->Host = 'smtp.gmail.com';
// use
// $mail->Host = gethostbyname('smtp.gmail.com');
// if your network does not support SMTP over IPv6

//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
$mail->Port = 465;

//Set the encryption system to use - ssl (deprecated) or tls
$mail->SMTPSecure = 'ssl';

//Whether to use SMTP authentication
$mail->SMTPAuth = true;

//Username to use for SMTP authentication - use full email address for gmail
$mail->Username = "xxx@gmail.com";

//Password to use for SMTP authentication
$mail->Password = "xxx";

/* Replace the email address and the Email Subject */
$EmailTo = "xxx@gmail.com"; // The Send To Email
$Subject= "New Message";         // Email Subject
// reCAPTCHA SEcret Key
$reCaptchaSecretKey = "xxx";


//retrive form variables from ajax
$name =@$_REQUEST["name"];
$email =@$_POST["email"];
$comment =@$_POST["comment"];
$captcha=@$_REQUEST["captcha"];
$formGoogleCaptcha = $captcha;

//check Catptcha
$response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$reCaptchaSecretKey."&response=".$formGoogleCaptcha."&remoteip=".$_SERVER['REMOTE_ADDR']);
$obj = json_decode($response);

if ($obj->success == 1 || $obj->success == true) {


$email_body="";
$email_body = $email_body . "<strong>First Name: </strong>" . $name . "\r\n\r\n";
$email_body = $email_body . "<strong>Email: </strong>" . $email . "\r\n\r\n";
$email_body = $email_body . "<strong>Message: </strong>" . $comment . "\r\n\r\n";

$email_body = nl2br($email_body);
$mail->SMTPDebug = 1;

我还附上了错误示例

enter image description here

先谢谢你们了, friend 们。

最佳答案

您看到的是调试输出,出现该输出是因为您启用了调试输出。更改此:

$mail->SMTPDebug = 1;

$mail->SMTPDebug = 0;

请注意,您在脚本的早期将其关闭,但稍后再次打开。

另一个问题是您使用的是非常旧版本的 PHPMailer。 Get the latest .

关于javascript - PHPMailer SMTP 问题 - 收到信件但网站上有错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60683378/

相关文章:

C# 电子邮件到 gmail 标记为垃圾邮件,但来自雷鸟客户端的邮件正常发送(非垃圾邮件)

javascript - Laravel Mix 和 Webpack : Cannot Load JSON File (Webpack Adding . js 到文件名)

javascript - jQuery:根据窗口宽度更改 jQuery

javascript - 图像 slider 未正确加载

javascript - JQuery map : What exactly is this? 有什么作用?

php - 如何禁止所有使用 markdown 的 HTML(PHP 和 JS)?

linux - sendmail 452 4.5.3 收件人太多

PHP、SPL预定义常量

php - Magento 从 1.9.4.5 升级到 Openmage 20.x

ruby-on-rails - 参数错误 : SMTP-AUTH requested but missing user name + ActionMailer