我已将
phpmailer 6.8.1
(从 phpmailer 5.2
升级)安装到我的服务器中(使用 php v7.0.33
)。当我收到测试电子邮件时,phpmailer 正在工作,但是一旦处理/发送了电子邮件数据,ajax
调用就会返回到调用页面,在该页面中我向用户显示电子邮件已发送的消息,这就是我正在查找的部分纠正。网站的contact.php
页面通过process_form.php
调用将表单数据发送到ajax
:
form.submit.addEventListener('click', function(){
let first_name = document.getElementById('first_name').value;
let telephone = document.getElementById('telephone').value;
let email = document.getElementById('email').value;
let textarea1 = document.getElementById('textarea1').value;
let submit = document.getElementById('btn-submit').value;
let messages = document.getElementById('form-messages').value;
const request = new XMLHttpRequest();
//debugger;
/* WHEN FUNCTION COMES BACK FROM AJAX CALL WE RUN THIS FUNCTION */
request.onload = function() {
console.log('line 318 ', request.responseText);//
let responseObject = null;
/* CONVERTING JSON STRING INTO A STANDARD JS OBJECT */
try{
responseObject = JSON.parse(request.responseText);
//console.log(responseObject);
} catch (e) {
console.error('Could not parse JSON! (line ~327 in js code)', e.message);
}
/* IF THERE WAS NO ERROR - PASS INTO FUNTION */
if (responseObject) {
handleResponse(responseObject);
}
}; //END: request.onload = function() {
const requestData = 'first_name='+first_name+'&telephone='+telephone+'&email='+email+'&textarea1='+textarea1;
console.log('requestData = ' + requestData);
/* MAKING A POST REQUEST HERE */
request.open('post', '../scripts/process_form.php');
request.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
/* SENDING REQUEST BELOW */
request.send(requestData);
}); //END:
这是页面控制台中的错误消息:
e: SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data at line 1 column 5 of JSON data
来自
json_encode
的 process_form.php
在处理完表单数据后发送。此时,在代码中,它是在发送电子邮件之后(验证所有表单字段)或没有发送电子邮件,因为表单字段中记录了错误,并且它指定了哪个表单字段具有不正确的数据(因此是数组) :
echo json_encode(
array(
'ok' => $ok,
'messages' => $messages
)
);
$ok
是布尔值,指示用户输入是否正确
$messages
表示字符串“成功发送电子邮件”或指定通过表单传递的错误字段。
我没有从
process_form.php
收到任何信息,并且在 contact.php
上调试时:
console.log('responseObject = ', responseObject);
responseObject
是 null
对于:
request.onload = function() {
console.log('line 318 (responseText = ) ', request.responseText);//
我知道我发送正确:
const requestData = 'first_name='+first_name+'&telephone='+telephone+'&email='+email+'&textarea1='+textarea1;
console.log('line 344 (requestData = ) ' + requestData);
/* MAKING A POST REQUEST HERE */
request.open('post', '../scripts/process_form.php');
request.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
/* SENDING REQUEST BELOW */
request.send(requestData);
来自控制台日志:
line 344 (requestData = ) first_name=Brian D Rauch&telephone=6092843554&[email protected]&textarea1=Email Test 9/27/2023 (28)
而且
responseText
可能很混乱 - 信息太多,不知道它来自哪里:
line 318 (responseText = ) 2023-09-27 19:08:23 CLIENT -> SERVER: EHLO allcountyguitar.com<br>
2023-09-27 19:08:23 CLIENT -> SERVER: STARTTLS<br>
2023-09-27 19:08:23 CLIENT -> SERVER: EHLO allcountyguitar.com<br>
2023-09-27 19:08:23 CLIENT -> SERVER: AUTH LOGIN<br>
2023-09-27 19:08:23 CLIENT -> SERVER: [credentials hidden]<br>
2023-09-27 19:08:23 CLIENT -> SERVER: [credentials hidden]<br>
2023-09-27 19:08:23 CLIENT -> SERVER: MAIL FROM:<[email protected]><br>
2023-09-27 19:08:23 CLIENT -> SERVER: RCPT TO:<[email protected]><br>
2023-09-27 19:08:23 CLIENT -> SERVER: RCPT TO:<[email protected]><br>
2023-09-27 19:08:23 CLIENT -> SERVER: RCPT TO:<[email protected]><br>
2023-09-27 19:08:23 CLIENT -> SERVER: DATA<br>
2023-09-27 19:08:23 CLIENT -> SERVER: Date: Wed, 27 Sep 2023 19:08:23 +0000<br>
2023-09-27 19:08:23 CLIENT -> SERVER: To: [email protected]<br>
2023-09-27 19:08:23 CLIENT -> SERVER: From: "AllCountyGuitar (via KiteandWindsurfer)" <[email protected]><br>
2023-09-27 19:08:23 CLIENT -> SERVER: Cc: [email protected]<br>
2023-09-27 19:08:23 CLIENT -> SERVER: Reply-To: Information <[email protected]><br>
2023-09-27 19:08:23 CLIENT -> SERVER: Subject: Inquiry from AllCountyGuitar Website!<br>
2023-09-27 19:08:23 CLIENT -> SERVER: Message-ID: <[email protected]><br>
2023-09-27 19:08:23 CLIENT -> SERVER: X-Mailer: PHPMailer 6.8.1 (https://github.com/PHPMailer/PHPMailer)<br>
2023-09-27 19:08:23 CLIENT -> SERVER: MIME-Version: 1.0<br>
2023-09-27 19:08:23 CLIENT -> SERVER: Content-Type: text/html; charset=iso-8859-1<br>
2023-09-27 19:08:23 CLIENT -> SERVER: <br>
2023-09-27 19:08:23 CLIENT -> SERVER: <br>
2023-09-27 19:08:23 CLIENT -> SERVER: <div style='color:#33691e;background-color:#f1f8e9; font-family: Tahoma; padding:20px; width:60%; border:solid 1px #dcedc8;'><br>
2023-09-27 19:08:23 CLIENT -> SERVER: <h1>Email Inquiry From AllCountyGuitar.com:</h1><br>
2023-09-27 19:08:23 CLIENT -> SERVER: <p>Date: Wed, Sep 27, 2023</p><br>
2023-09-27 19:08:23 CLIENT -> SERVER: <p>Name: Brian D Rauch</p><br>
2023-09-27 19:08:23 CLIENT -> SERVER: <p>Telephone: 6092843554</p><br>
2023-09-27 19:08:23 CLIENT -> SERVER: <p>Email Address: [email protected]</p><br>
2023-09-27 19:08:23 CLIENT -> SERVER: <br><br>
2023-09-27 19:08:23 CLIENT -> SERVER: <div>Comment:<br><br>Email Test 9/27/2023 (27)</div><br>
2023-09-27 19:08:23 CLIENT -> SERVER: <h3>END OF MESSAGE</h3><br>
2023-09-27 19:08:23 CLIENT -> SERVER: </div><br>
2023-09-27 19:08:23 CLIENT -> SERVER: <br>
2023-09-27 19:08:23 CLIENT -> SERVER: <br>
2023-09-27 19:08:23 CLIENT -> SERVER: .<br>
2023-09-27 19:08:24 CLIENT -> SERVER: QUIT<br>
{"ok":true,"messages":["Thank you, your message has been sent!"]}
最后,
responseObject
永远不会超越null
responseObject = JSON.parse(request.responseText);
请回复并提供任何需要的进一步信息。
最好的, 布莱恩
//$mail->SMTPDebug = 1;
解决了这个问题。然而,如果没有 @David、@aynber 和 @tkausl 的VERY 有用的评论,这是不可能实现的。
phpmailer 的升级很有压力,因为我不是技术恐惧者,因此非常犹豫。早期我设置了调试参数,直到追根溯源才注意到它。再次非常感谢所有为此提供帮助的人,以及将 StackOverflow 作为探索和寻求帮助的论坛。
对大家来说最好的是, 布莱恩