Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #10044197
    leemchildress
    Participant

    I 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);
    }


    #10044198
    Support
    Keymaster

    Hello,

    It’s probably missing the view.contact.js script 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.


    #10044199
    leemchildress
    Participant

    Thanks. 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


    #10044200
    Support
    Keymaster

    Try to open that file js/views/view.contact.js and change the line 88 to:

    if (data.response != 'error') {

    #10044201
    leemchildress
    Participant

    I received your test message.


    #10044202
    leemchildress
    Participant

    I made the change and still receive the same error (but I do receive the message).


    #10044203
    Support
    Keymaster
    This reply has been marked as private.
    #10044204
    leemchildress
    Participant

    Thank you. That resolved the issue.


Viewing 8 posts - 1 through 8 (of 8 total)

This topic is marked as "RESOLVED" and can not rceive new replies.