Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHPMailer only sends email when SMTPDebug = true

I'm using PHPmailer. It works when $mail->SMTPDebug = true; but when I remove that line, it silently fails. I say silently fails as it doesn't give any errors, and yet the email doesn't seem to be delivered.

        $mail = new PHPMailer;

        $mail->SMTPDebug = true;
        $mail->SMTPAuth = true;
        $mail->CharSet = 'utf-8';
        $mail->SMTPSecure = 'ssl';
        $mail->Host = 'smtp.gmail.com';
        $mail->Port = '465';
        $mail->Username = '[email protected]';
        $mail->Password = 'xxxxx';
        $mail->Mailer = 'smtp';
        $mail->AddReplyTo('[email protected]', 'xxxxx Support');
        $mail->From = '[email protected]';
        $mail->FromName = 'xxxxx Applications';
        $mail->Sender = 'xxxxx Applications';
        $mail->Priority = 3;

        //To us
        $mail->AddAddress('[email protected]', 'xxxxx xxxxx');
        //To Applicant
        $mail->AddAddress($email, $first_name.''.$last_name);
        $mail->IsHTML(true);

        $last_4_digits = substr($card_num, -4);
        //build email contents

        $mail->Subject = 'Application From '.$first_name.' '.$last_name;
        $mail->Body    = $body_html;
        $mail->AltBody = $body_text;

        if(!$mail->send()) {
           echo 'Message could not be sent.';
           echo 'Mailer Error: ' . $mail->ErrorInfo;
           exit;
        }
like image 940
Chris J Allen Avatar asked Sep 18 '13 10:09

Chris J Allen


2 Answers

By setting

        $mail->SMTPDebug = false;

instead of omitting the line completely, it works every time.

like image 115
Chris J Allen Avatar answered Oct 06 '22 10:10

Chris J Allen


I think you are using an outdated version of the phpmailer, please use composer to install. Check of the version of the PHP mailer is 6.4+

Below is an example of how to install the latest version using the composer.

composer require phpmailer/phpmailer

When you look at the official repository code - line 402

https://github.com/PHPMailer/PHPMailer/blob/master/src/PHPMailer.php

you see that $SMTPDebug is set to 0, so it can't be the reason why it fails silently.

public $SMTPDebug = 0;

Below provides an example guide to send the email using phpmailer. This example worked for me without the use of the SMTPDEBUG not specifying. Also, PHPMailer(true) makes the exceptions enabled which can be useful so that it doesn't fail silently.

    //Load Composer's autoloader
    require 'vendor/autoload.php';
    
    //Instantiation and passing `true` enables exceptions
    $mail = new PHPMailer(true);
    
    try {
       
 //Server settings


        $mail->isSMTP();                                            //Send using SMTP
        $mail->Host       = 'smtp.example.com';                     //Set the SMTP server to send through
        $mail->SMTPAuth   = true;                                   //Enable SMTP authentication
        $mail->Username   = '[email protected]';                     //SMTP username
        $mail->Password   = 'secret';                               //SMTP password
        $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;         //Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
        $mail->Port       = 587;                                    //TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above
    
        //Recipients
        $mail->setFrom('[email protected]', 'Mailer');
        $mail->addAddress('[email protected]', 'Joe User');     //Add a recipient
        $mail->addAddress('[email protected]');               //Name is optional
        $mail->addReplyTo('[email protected]', 'Information');
        $mail->addCC('[email protected]');
        $mail->addBCC('[email protected]');
    
        //Attachments
        $mail->addAttachment('/var/tmp/file.tar.gz');         //Add attachments
        $mail->addAttachment('/tmp/image.jpg', 'new.jpg');    //Optional name
    
        //Content
        $mail->isHTML(true);                                  //Set email format to HTML
        $mail->Subject = 'Here is the subject';
        $mail->Body    = 'This is the HTML message body <b>in bold!</b>';
        $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
    
        $mail->send();
        echo 'Message has been sent';
    } catch (Exception $e) {
        echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
    }
like image 44
mahen3d Avatar answered Oct 06 '22 09:10

mahen3d