- This topic has 7 replies, 2 voices, and was last updated 2 years, 8 months ago by
leemchildress. This post has been viewed 1425 times
-
AuthorPosts
-
December 27, 2023 at 1:09 am #10044197
leemchildress
ParticipantI am using php-mailer for a contact form on my website. When I click submit, I DO receive the email; however, on the website, it displays a JSON response saying “Success” rather than updating the screen to say the message has been sent.
What am I missing? Script follows.
<?php
/*
Name: Contact Form
Written by: Okler Themes - (http://www.okler.net)
Theme Version: 10.0.0
*/namespace PortoContactForm;
session_cache_limiter('nocache');
header('Expires: ' . gmdate('r', 0));header('Content-type: application/json');
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;require 'php-mailer/src/PHPMailer.php';
require 'php-mailer/src/SMTP.php';
require 'php-mailer/src/Exception.php';// Step 1 - Enter your email address below.
$email = '[email protected]';// If the e-mail is not working, change the debug option to 2 | $debug = 2;
$debug = 0;// If contact form don't has the subject input change the value of subject here
$subject = ( isset($_POST['subject']) ) ? $_POST['subject'] : 'Define subject in php/contact-form.php line 29';$message = '';
foreach($_POST as $label => $value) {
$label = ucwords($label);// Use the commented code below to change label texts. On this example will change "Email" to "Email Address"
// if( $label == 'Email' ) {
// $label = 'Email Address';
// }// Checkboxes
if( is_array($value) ) {
// Store new value
$value = implode(', ', $value);
}$message .= $label.": " . nl2br(htmlspecialchars($value, ENT_QUOTES)) . "<br>";
}$mail = new PHPMailer(true);
try {
$mail->SMTPDebug = $debug; // Debug Mode
// Step 2 (Optional) - If you don't receive the email, try to configure the parameters below:
//$mail->IsSMTP(); // Set mailer to use SMTP
//$mail->Host = 'mail.yourserver.com'; // Specify main and backup server
//$mail->SMTPAuth = true; // Enable SMTP authentication
//$mail->Username = '[email protected]'; // SMTP username
//$mail->Password = 'secret'; // SMTP password
//$mail->SMTPSecure = 'tls'; // Enable encryption, 'ssl' also accepted
//$mail->Port = 587; // TCP port to connect to$mail->AddAddress($email); // Add another recipient
//$mail->AddAddress('[email protected]', 'Person 2'); // Add a secondary recipient
//$mail->AddCC('[email protected]', 'Person 3'); // Add a "Cc" address.
//$mail->AddBCC('[email protected]', 'Person 4'); // Add a "Bcc" address.// From - Name
$fromName = ( isset($_POST['name']) ) ? $_POST['name'] : 'Website User';
$mail->SetFrom($email, $fromName);// Reply To
if( isset($_POST['email']) && !empty($_POST['email']) ) {
$mail->AddReplyTo($_POST['email'], $fromName);
}$mail->IsHTML(true); // Set email format to HTML
$mail->CharSet = 'UTF-8';
$mail->Subject = $subject;
$mail->Body = $message;// Step 3 - If you don't want to attach any files, remove that code below
if (isset($_FILES['attachment']) && $_FILES['attachment']['error'] == UPLOAD_ERR_OK) {
$mail->AddAttachment($_FILES['attachment']['tmp_name'], $_FILES['attachment']['name']);
}$mail->Send();
$arrResult = array ('response'=>'success');} catch (Exception $e) {
$arrResult = array ('response'=>'error','errorMessage'=>$e->errorMessage());
} catch (\Exception $e) {
$arrResult = array ('response'=>'error','errorMessage'=>$e->getMessage());
}if ($debug == 0) {
echo json_encode($arrResult);
}
December 27, 2023 at 2:02 am #10044198Support
KeymasterHello,
It’s probably missing the
view.contact.jsscript on footer of page. This is the file that makes the validation of the form and submit.
<script src="js/views/view.contact.js"></script>
Please try that and let us know if you need further assistance.
Kind Regards.
December 27, 2023 at 3:02 am #10044199leemchildress
ParticipantThanks. I’m making progress! Now I get a message saying there was an problem with my message but yet it was delivered with no issue. I turned on debug and this is what I get:
Error! There was an error sending your message.
Sending with mail()
Sendmail path: /usr/sbin/sendmail -t -i
Envelope sender: [email protected]
To: [email protected]
Subject: This is test #4
Headers: Date: Tue, 26 Dec 2023 10:00:23 -0600From: "Lee M. Childress" <[email protected]>Reply-To: "Lee M. Childress" <[email protected]>Message-ID: <[email protected]>X-Mailer: PHPMailer 6.6.3 (https://github.com/PHPMailer/PHPMailer)MIME-Version: 1.0Content-Type: text/html; charset=UTF-8
Additional params: [email protected]
Result: true
December 27, 2023 at 3:26 am #10044200Support
KeymasterTry to open that file js/views/view.contact.js and change the line 88 to:
if (data.response != 'error') {
December 27, 2023 at 3:27 am #10044201leemchildress
ParticipantI received your test message.
December 27, 2023 at 3:30 am #10044202leemchildress
ParticipantI made the change and still receive the same error (but I do receive the message).
December 27, 2023 at 4:21 am #10044203Support
KeymasterThis reply has been marked as private.December 27, 2023 at 5:27 am #10044204leemchildress
ParticipantThank you. That resolved the issue.
-
AuthorPosts
This topic is marked as "RESOLVED" and can not rceive new replies.