13

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 = 'xxxxx@gmail.com';
        $mail->Password = 'xxxxx';
        $mail->Mailer = 'smtp';
        $mail->AddReplyTo('support@xxxxx.com', 'xxxxx Support');
        $mail->From = 'xxxxx@gmail.com';
        $mail->FromName = 'xxxxx Applications';
        $mail->Sender = 'xxxxx Applications';
        $mail->Priority = 3;

        //To us
        $mail->AddAddress('xxxxx@xxxxx.com', '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;
        }
Chris J Allen
  • 18,970
  • 20
  • 76
  • 114

3 Answers3

25

By setting

        $mail->SMTPDebug = false;

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

Chris J Allen
  • 18,970
  • 20
  • 76
  • 114
3

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   = 'user@example.com';                     //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('from@example.com', 'Mailer');
        $mail->addAddress('joe@example.net', 'Joe User');     //Add a recipient
        $mail->addAddress('ellen@example.com');               //Name is optional
        $mail->addReplyTo('info@example.com', 'Information');
        $mail->addCC('cc@example.com');
        $mail->addBCC('bcc@example.com');
    
        //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}";
    }
mahen3d
  • 7,047
  • 13
  • 51
  • 103
0

To provide more details to the question:

What is the reason why email is not sent when SMTPDebug information is omitted?

As the version of PHPMailer used was not specified, I tried using the latest version at the time of the question which was v5.2.7 released on the 12th of September, 2013, but I also tried the oldest non-dev version, 5.2.2 (that was available on packagist) and all in between (v5.2.4, v5.2.5 and v5.2.6).

I could not replicate the issue and so it's likely that it was not caused by omitting the SMTPDebug parameter in PHPMailer unless other dependencies, operating system or PHP version played a role in the problem but without details, it's not possible to ascertain.

In these old versions the SMTPDebug parameter was used like so

/**
 * SMTP class debug output mode.
 * Options: 0 = off, 1 = commands, 2 = commands and data
 * @type int
 * @see SMTP::$do_debug
 */
public $SMTPDebug = 0;

...

protected function edebug($str)
{
    if (!$this->SMTPDebug) {
        return;
    }
...

Where edebug was used for simple debug logging:

$this->edebug($e->getMessage() . "\n");

So it would be quite difficult for a bug to occur that would cause the described effect as a result of omitting SMTPDebug.

If the bounty placer is still having trouble, try @mahen3d's suggestions or ask a new question providing a reprex.

Lucan
  • 2,907
  • 2
  • 16
  • 30