I have installed phpmailer 6.8.1
(upgraded from phpmailer 5.2
) into my server (using php v7.0.33
). The phpmailer is working as I am receiving the test emails however once the email data is processed/sent the ajax
call returns to the calling page where I show the user a message that email was sent and it is this part which I am looking to correct. The website's contact.php
page sends the form data to process_form.php
via ajax
call:
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:
This is the error message in the page console:
e: SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data at line 1 column 5 of JSON data
The json_encode
from process_form.php
sends after processing the form data. At this point in the code it is after the email was sent (all form fields validated) or no email was sent as error recorded in form field(s) and it specifies which form field(s) had incorrect data (thus the array):
echo json_encode(
array(
'ok' => $ok,
'messages' => $messages
)
);
$ok
is boolean indicating correct user input or not
$messages
indicates a string "successfully sent email" or specifies the error fields from what was passed via the form.
I receive nothing from process_form.php
and when debugging on contact.php
:
console.log('responseObject = ', responseObject);
responseObject
is null
for:
request.onload = function() {
console.log('line 318 (responseText = ) ', request.responseText);//
I know I am sending correctly:
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);
From console log: line 344 (requestData = ) first_name=Brian D Rauch&telephone=6092843554&email=bxr0354@njit.edu&textarea1=Email Test 9/27/2023 (28)
And responseText
is perhaps messing - way too much information and no idea where it derives:
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:<kiteandwindsurfer@gmail.com><br>
2023-09-27 19:08:23 CLIENT -> SERVER: RCPT TO:<allcountyguitar@gmail.com><br>
2023-09-27 19:08:23 CLIENT -> SERVER: RCPT TO:<kiteandwindsurfer@gmail.com><br>
2023-09-27 19:08:23 CLIENT -> SERVER: RCPT TO:<bxr0354@njit.edu><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: allcountyguitar@gmail.com<br>
2023-09-27 19:08:23 CLIENT -> SERVER: From: "AllCountyGuitar (via KiteandWindsurfer)" <kiteandwindsurfer@gmail.com><br>
2023-09-27 19:08:23 CLIENT -> SERVER: Cc: kiteandwindsurfer@gmail.com<br>
2023-09-27 19:08:23 CLIENT -> SERVER: Reply-To: Information <bxr0354@njit.edu><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: <ZFfDRcpjYvDQf0bwoMdYSmWhrJgpx23UTmCYCNLaZc@allcountyguitar.com><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: bxr0354@njit.edu</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!"]}
and lastly the responseObject
never gets beyond null
responseObject = JSON.parse(request.responseText);
Commenting out the $mail->SMTPDebug
declaration solved the issue.
// $mail->SMTPDebug = 1;
Arriving at this would not have been possible without the VERY helpful comments by @David, @aynber and @tkausl.
The upgrading of PhpMailer was stressful as I am not any technophobe thus very tentative. Early on, I set the debug parameter and didn't pay it any mind until tracing the problem to its roots.