xmlhttprequest与phpmailer $ mail-> send()不兼容

时间:2017-03-17 17:30:37

标签: javascript php ajax phpmailer

我正在尝试使用javascript和phpmailer中的xmlhttprequest API设置自动电子邮件发送功能。电子邮件已成功发送。但是,php响应无法发送回xmlhttprequest的onreadystatechange事件。通过调试过程,xhr.readystate的值为1,然后是2,并且该过程完成没有任何错误或警报。请参阅以下代码: js代码与xmlhttprequest:

  var xhr = new XMLHttpRequest(); // Usual mix-and-matching for x-browser omitted for brevity
                        var senddata = 'problem=' + dom.byId("sProblem").value + '&name=' + dom.byId("sName").value + '&email=' + dom.byId("sEmail").value + '&phone=' + dom.byId("sPhone").value
                                       + '&streets=' + dom.byId("sStreets").value + '&comments=' + dom.byId("sComments").value;

                        xhr.onreadystatechange = function () {

                            if (xhr.readyState == 4 && xhr.status == 200) {                            
                                alert("Thank you for sending email ");                      
                            }
                        }

                         xhr.open('POST', 'Scripts/app/php/report.php', true);
                        xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
                        xhr.send(senddata);

我的php文件非常简单:

<?php
require __DIR__ . "/../../../../../phpmailer/PHPMailerAutoload.php";


$problem = $_POST["problem"];
$name = $_POST["name"];
$email  = $_POST["email"];
$phone = $_POST["phone"];
$streets = $_POST["streets"];
$comments = $_POST["comments"];

$mail = new PHPMailer(true);
$mail->isSMTP();
$mail->Host = 'smtp.office365.com';
$mail->Port       = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth   = true;
$mail->Username = 'xxx@xxx.net';
$mail->Password = 'xxx';
$mail->SetFrom('xxx@xxx.net', 'FromEmail');
$mail->addAddress('xxx@xxx.org', 'ToEmail');
$mail->IsHTML(true);

$mail->Subject = 'It is a test from Jing';
$mail->Body =  "Report a problem form from the KCRC website:"."<br>";
$mail->Body .= "Problem: $problem"."<br>";
$mail->Body .= "Name: $name"."<br>";
$mail->Body .= "E-mail: $email"."<br>";
$mail->Body .= "Phone: $phone"."<br>";
$mail->Body .= "Streets: $streets"."<br>";
$mail->Body .= "Comments: $comments"."<br>";


$mail->AltBody =  "Report a problem form from the KCRC website:"."<br>";
$mail->AltBody .= "Problem: $problem"."<br>";
$mail->AltBody .= "Name: $name"."<br>";
$mail->AltBody .= "E-mail: $email"."<br>";
$mail->AltBody .= "Phone: $phone"."<br>";
$mail->AltBody .= "Streets: $streets"."<br>";
$mail->AltBody .= "Comments: $comments"."<br>";

if(!$mail->send()) { 
  echo "Mail sending failed."; 
}else{
  echo "Thank you for using our mail form";
}

当我评论这部分时:

if(!$mail->send()) {
    echo "Mail sending failed."; 
  }else{
    echo "Thank you for using our mail form";
    }

我可以收到此提醒:提醒(“感谢您发送电子邮件”);
但如果我取消注释,电子邮件将被发送,但没有警报(即时状态永远不会达到4)。我需要提醒,告知用户他们的电子邮件已发送。

如果我只运行php文件,我看到没有错误:当我将调试级别设置为2时,这是php页面的结果:

debug level 2; message: SERVER -> CLIENT: 220     CY4PR16CA0044.outlook.office365.com Microsoft ESMTP MAIL Service ready at Mon, 20 Mar 2017 12:46:59 +0000 debug level 2; message: CLIENT -> SERVER: EHLO localhost debug level 2; message: SERVER -> CLIENT: 250-CY4PR16CA0044.outlook.office365.com Hello [96.61.212.2] 250-SIZE 157286400 250-PIPELINING 250-DSN 250-ENHANCEDSTATUSCODES 250-STARTTLS 250-8BITMIME 250-BINARYMIME 250-CHUNKING 250 SMTPUTF8 debug level 2; message: CLIENT -> SERVER: STARTTLS debug level 2; message: SERVER -> CLIENT: 220 2.0.0 SMTP server ready debug level 2; message: CLIENT -> SERVER: EHLO localhost debug level 2; message: SERVER -> CLIENT: 250-CY4PR16CA0044.outlook.office365.com Hello [96.61.212.2] 250-SIZE 157286400 250-PIPELINING 250-DSN 250-ENHANCEDSTATUSCODES 250-AUTH LOGIN 250-8BITMIME 250-BINARYMIME 250-CHUNKING 250 SMTPUTF8 debug level 2; message: CLIENT -> SERVER: AUTH LOGIN debug level 2; message: SERVER -> CLIENT: 334 VXNlcm5hbWU6 debug level 2; message: CLIENT -> SERVER: d2ViZm9ybXN1Ym1pdEBrZW50Y291bnR5cm9hZHMubmV0 debug level 2; message: SERVER -> CLIENT: 334 UGFzc3dvcmQ6 debug level 2; message: CLIENT -> SERVER: eDd1Vyd5LmA/Xw== debug level 2; message: SERVER -> CLIENT: 235 2.7.0 Authentication successful target host BL2PR14MB0929.namprd14.prod.outlook.com debug level 2; message: CLIENT -> SERVER: MAIL FROM: debug level 2; message: SERVER -> CLIENT: 250 2.1.0 Sender OK debug level 2; message: CLIENT -> SERVER: RCPT TO: debug level 2; message: SERVER -> CLIENT: 250 2.1.5 Recipient OK debug level 2; message: CLIENT -> SERVER: DATA debug level 2; message: SERVER -> CLIENT: 354 Start mail input; end with . debug level 2; message: CLIENT -> SERVER: Date: Mon, 20 Mar 2017 08:46:59 -0400 debug level 2; message: CLIENT -> SERVER: To: ToEmail  debug level 2; message: CLIENT -> SERVER: From: FromEmail  debug level 2; message: CLIENT -> SERVER: Subject: It is a test from Jing debug level 2; message: CLIENT -> SERVER: Message-ID: <2e72266fdeacea641e142a9ffab0a19c@localhost> debug level 2; message: CLIENT -> SERVER: X-Mailer: PHPMailer 5.2.15 (https://github.com/PHPMailer/PHPMailer) debug level 2; message: CLIENT -> SERVER: MIME-Version: 1.0 debug level 2; message: CLIENT -> SERVER: Content-Type: multipart/alternative; debug level 2; message: CLIENT -> SERVER: boundary="b1_2e72266fdeacea641e142a9ffab0a19c" debug level 2; message: CLIENT -> SERVER: Content-Transfer-Encoding: 8bit debug level 2; message: CLIENT -> SERVER: debug level 2; message: CLIENT -> SERVER: This is a multi-part message in MIME format. debug level 2; message: CLIENT -> SERVER: debug level 2; message: CLIENT -> SERVER: --b1_2e72266fdeacea641e142a9ffab0a19c debug level 2; message: CLIENT -> SERVER: Content-Type: text/plain; charset=us-ascii debug level 2; message: CLIENT -> SERVER: debug level 2; message: CLIENT -> SERVER: Report a problem form from the KCRC website:
Problem: 
Name: 
E-mail: 
Phone: 
Streets: 
Comments: 
debug level 2; message: CLIENT -> SERVER: debug level 2; message: CLIENT -> SERVER: debug level 2; message: CLIENT -> SERVER: --b1_2e72266fdeacea641e142a9ffab0a19c debug level 2; message: CLIENT -> SERVER: Content-Type: text/html; charset=us-ascii debug level 2; message: CLIENT -> SERVER: debug level 2; message: CLIENT -> SERVER: Report a problem form from the KCRC website:
Problem: 
Name: 
E-mail: 
Phone: 
Streets: 
Comments: 
debug level 2; message: CLIENT -> SERVER: debug level 2; message: CLIENT -> SERVER: debug level 2; message: CLIENT -> SERVER: debug level 2; message: CLIENT -> SERVER: --b1_2e72266fdeacea641e142a9ffab0a19c-- debug level 2; message: CLIENT -> SERVER: debug level 2; message: CLIENT -> SERVER: . debug level 2; message: SERVER -> CLIENT: 250 2.6.0 <2e72266fdeacea641e142a9ffab0a19c@localhost> [InternalId=32800665241380, Hostname=BL2PR14MB0929.namprd14.prod.outlook.com] 3051 bytes in 0.544, 5.468 KB/sec Queued mail for delivery debug level 2; message: CLIENT -> SERVER: QUIT debug level 2; message: SERVER -> CLIENT: 221 2.0.0 Service closing transmission     channel Thank you for using our mail form 

我已将phpmailer更新为5.2.23版。

任何帮助表示赞赏!!

0 个答案:

没有答案