PHPMailer only sends email when SMTPDebug = true
Asked Answered
D

3

14

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;
        }
Dorseydorsiferous answered 18/9, 2013 at 10:2 Comment(5)
I experience the same issue, what's the problem with just leaving the line there?Testosterone
Because it spits out a huge long debug to the screen? or is there a way to disable that?Dorseydorsiferous
Does you have make any changes on SMTP mailer class files ?Solferino
I haven't no. Like i say though it works with debug enabled, email is delivered straight away.Dorseydorsiferous
if you use a separate file just to send the smtp, then it wont matter if it spits out the debug. Try that?Testosterone
D
26

By setting

        $mail->SMTPDebug = false;

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

Dorseydorsiferous answered 18/9, 2013 at 10:44 Comment(0)
P
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   = '[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}";
    }
Portwin answered 26/4, 2021 at 10:52 Comment(1)
Not sure and thanks for the reply but it was asked 7 years ago ;)Dorseydorsiferous
S
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.

Surah answered 29/4, 2021 at 22:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.