javascript – PHPMailer SMTP troubles – Getting letters but with errors on the website-ThrowExceptions

Exception or error:

I have the following issue: I have a contact form on my Bootstrap 4 .html website. The form is a simple form based on PHPMailer with Bootstrap validator. Previously I received the emails from my contact form without SMTP and everything was fine. Now I decided to use Gmail SMTP and changed my code. I’m receiving the messages to my gmail inbox but I see the errors on the website as soon as the letter is sent.

Could, you, please, let me know where my problem is? I broke my head trying to solve the problem.

ajax.js file:

$(document).ready(function() {
function submitForm(){
            var name =$("input[name=name]").val();
            var email =$("input[name=email]").val();
            var comment =$("textarea[name=comment]").val();
            var captcha=grecaptcha.getResponse();
            var form = $('form')[0];
            var formData = new FormData(form);
            formData.append('name', name );
            formData.append('email', email );
            formData.append('comment', comment );
            formData.append('captcha', captcha );
            $.ajax({
                 url: "include/ajax/send.php",
                 type: "POST",
                 contentType: false,
                 processData: false,
                 data: formData,
                 cache: false,
                 success : function(text){
             if (text == "success"){
                 formSuccess();
             } else {
                 formError();
                 submitMSG(false,text);
             }
         }
     });
 }

php file:

// send email
$mail = new PHPMailer;
// Email Template

$mail->SMTPOptions = array(
    'ssl' => array(
        'verify_peer' => false,
        'verify_peer_name' => false,
        'allow_self_signed' => true
    )
);

//Tell PHPMailer to use SMTP
$mail->isSMTP();

//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 0;


//Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';

//Set the hostname of the mail server
$mail->Host = 'smtp.gmail.com';
// use
// $mail->Host = gethostbyname('smtp.gmail.com');
// if your network does not support SMTP over IPv6

//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
$mail->Port = 465;

//Set the encryption system to use - ssl (deprecated) or tls
$mail->SMTPSecure = 'ssl';

//Whether to use SMTP authentication
$mail->SMTPAuth = true;

//Username to use for SMTP authentication - use full email address for gmail
$mail->Username = "xxx@gmail.com";

//Password to use for SMTP authentication
$mail->Password = "xxx";

/* Replace the email address and the Email Subject */
$EmailTo = "xxx@gmail.com"; // The Send To Email
$Subject= "New Message";         // Email Subject
// reCAPTCHA SEcret Key
$reCaptchaSecretKey = "xxx";


//retrive form variables from ajax
$name =@$_REQUEST["name"];
$email =@$_POST["email"];
$comment =@$_POST["comment"];
$captcha=@$_REQUEST["captcha"];
$formGoogleCaptcha = $captcha;

//check Catptcha
$response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$reCaptchaSecretKey."&response=".$formGoogleCaptcha."&remoteip=".$_SERVER['REMOTE_ADDR']);
$obj = json_decode($response);

if ($obj->success == 1 || $obj->success == true) {


$email_body="";
$email_body = $email_body . "<strong>First Name: </strong>" . $name . "\r\n\r\n";
$email_body = $email_body . "<strong>Email: </strong>" . $email . "\r\n\r\n";
$email_body = $email_body . "<strong>Message: </strong>" . $comment . "\r\n\r\n";

$email_body = nl2br($email_body);
$mail->SMTPDebug = 1;

I also attached an error example

enter image description here

Thank you in advance, mates.

How to solve:

What you’re seeing is debug output, which is appearing because you have debug output enabled. Change this:

$mail->SMTPDebug = 1;

to

$mail->SMTPDebug = 0;

Note that you turn it off early in your script, but then turn it on again later.

A separate problem is that you’re using a very old version of PHPMailer. Get the latest.

Leave a Reply

Your email address will not be published. Required fields are marked *