dpdfh60088 2018-06-16 18:27
浏览 66

PHPMailer通过ajax发送邮件

$.ajax({
    type: 'POST',
    url: 'sendmail.php',
    dataType: "json",
    data: formData,
    success: function(response) {
        console.log(response.success);
        if (response.success) {
            $('.form-sent').css({display: 'none'});
            $('.sent-sucess').css({display: 'block'});
        } else {
            alert(response.message);
        }
    },
    error: function(xhr, status, error){
        console.log(xhr);
        console.log(error);
    }
});

PHP Code

$mail = new PHPMailer(true);
try {
    $mail->SMTPDebug = 2;
    $mail->isSMTP();                            // Set mailer to use SMTP
    $mail->Host = 'smtp.gmail.com';             // Specify main and backup SMTP servers
    $mail->SMTPAuth = true;                     // Enable SMTP authentication
    $mail->Username = 'xxxx@gmail.com';          // SMTP username
    $mail->Password = 'xxx'; // SMTP password
    $mail->SMTPSecure = 'tls';                  // Enable TLS encryption, `ssl` also accepted
    $mail->Port = 587;                          // TCP port to connect to

    $mail->setFrom('xxx@gmail.com', 'Support System');
    $mail->addReplyTo($_POST['ema‌​il‌​']);
    $mail->addAddress('xxx@yahoo.com');   // Add a recipient

    $mail->isHTML(true);  // Set email format to HTML

    $bodyContent = '<h4>Customer: ' . $_POST['ema‌​il‌​'] . '</h4><p>' . $_POST['message'] . '</p>';

    $mail->Subject = $_POST['subject'];
    $mail->Body    = $bodyContent;

    $mail->send();
        // echo json_encode(['success'=>true]);
} catch (Exception $e) {
    echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}
  • When I send mail, mail is send success but ajax return error not success.
  • Response is:

2018-06-16 18:12:44 SERVER -> CLIENT: 220 smtp.gmail.com ESMTP e5-v6sm12554738pgs.59 - gsmtp
2018-06-16 18:12:44 CLIENT -> SERVER: EHLO xxx.test
2018-06-16 18:12:44 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [2405:4800:548f:2d02:19f9:f439:c390:803e]250-SIZE 35882577250-8BITMIME250-STARTTLS250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8
2018-06-16 18:12:44 CLIENT -> SERVER: STARTTLS
2018-06-16 18:12:45 SERVER -> CLIENT: 220 2.0.0 Ready to start TLS
2018-06-16 18:12:45 CLIENT -> SERVER: EHLO xxx.test
2018-06-16 18:12:45 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [2405:4800:548f:2d02:19f9:f439:c390:803e]250-SIZE 35882577250-8BITMIME250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8
2018-06-16 18:12:45 CLIENT -> SERVER: AUTH LOGIN
2018-06-16 18:12:45 SERVER -> CLIENT: 334 VXNlcm5hbWU6
2018-06-16 18:12:45 CLIENT -> SERVER: <credentials hidden>
2018-06-16 18:12:45 SERVER -> CLIENT: 334 UGFzc3dvcmQ6
2018-06-16 18:12:45 CLIENT -> SERVER: <credentials hidden>
2018-06-16 18:12:46 SERVER -> CLIENT: 235 2.7.0 Accepted
2018-06-16 18:12:46 CLIENT -> SERVER: MAIL FROM:<xxx@gmail.com>
2018-06-16 18:12:46 SERVER -> CLIENT: 250 2.1.0 OK e5-v6sm12554738pgs.59 - gsmtp
2018-06-16 18:12:46 CLIENT -> SERVER: RCPT TO:<k.abel1986@yahoo.com>
2018-06-16 18:12:46 SERVER -> CLIENT: 250 2.1.5 OK e5-v6sm12554738pgs.59 - gsmtp
2018-06-16 18:12:46 CLIENT -> SERVER: DATA
2018-06-16 18:12:47 SERVER -> CLIENT: 354 Go ahead e5-v6sm12554738pgs.59 - gsmtp
2018-06-16 18:12:47 CLIENT -> SERVER: Date: Sat, 16 Jun 2018 18:12:43 +0000
2018-06-16 18:12:47 CLIENT -> SERVER: To:xxx@yahoo.com
2018-06-16 18:12:47 CLIENT -> SERVER: From: Support System <xxx@gmail.com>
2018-06-16 18:12:47 CLIENT -> SERVER: Reply-To: emailtest1@gmail.com
2018-06-16 18:12:47 CLIENT -> SERVER: Subject: test send mail
2018-06-16 18:12:47 CLIENT -> SERVER: Message-ID: <Z8XqVgxJEztW9kdU3hz42VsB79duQMQjsq7bLQhx8eQ@xxx.test>
2018-06-16 18:12:47 CLIENT -> SERVER: X-Mailer: PHPMailer 6.0.5 (https://github.com/PHPMailer/PHPMailer)
2018-06-16 18:12:47 CLIENT -> SERVER: MIME-Version: 1.0
2018-06-16 18:12:47 CLIENT -> SERVER: Content-Type: text/html; charset=iso-8859-1
2018-06-16 18:12:47 CLIENT -> SERVER:
2018-06-16 18:12:47 CLIENT -> SERVER: <h4>Customer: emailtest1@gmail.com</h4><p>test send mail</p>
2018-06-16 18:12:47 CLIENT -> SERVER:
2018-06-16 18:12:47 CLIENT -> SERVER: .
2018-06-16 18:12:48 SERVER -> CLIENT: 250 2.0.0 OK 1529172767 e5-v6sm12554738pgs.59 - gsmtp
2018-06-16 18:12:48 CLIENT -> SERVER: QUIT
2018-06-16 18:12:48 SERVER -> CLIENT: 221 2.0.0 closing connection e5-v6sm12554738pgs.59 - gsmtp

It's something wrong, please help me.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
    • ¥20 怎么用dlib库的算法识别小麦病虫害
    • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
    • ¥15 java写代码遇到问题,求帮助
    • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
    • ¥15 有了解d3和topogram.js库的吗?有偿请教
    • ¥100 任意维数的K均值聚类
    • ¥15 stamps做sbas-insar,时序沉降图怎么画
    • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
    • ¥15 关于#Java#的问题,如何解决?