0

I have a Laravel project, it sends several emails through PHPMailer each day and seems to work correctly.

I assume the function I use to send emails works fine as almost always the emails are sent correctly. Is the usual email function with a try and catch, as shown below:

public static function sendMailWithCsvs($email, $subject, $body, $file, $filename)
{
    $mail  = new PHPMailer(true);

    try {
        $mail->IsSMTP();
        $mail->Host       = 'smtp.office365.com';
        $mail->Port       = 587;
        $mail->SMTPSecure = 'tls';
        $mail->SMTPAuth   = true;
        $mail->Username   = env('MAIL_USERNAME');
        $mail->Password   = env('MAIL_PASSWORD');
        $mail->SetFrom($mail->Username,'foo bar');
        $mail->CharSet = "utf-8";
        $mail->IsHTML(true);

        $mail->AddAddress($email);
        $mail->Subject = $subject;
        $mail->Body = $body;

        // attach file code omitted

        if(!$mail->send()) {
            Log::info('Message could not be sent.');
            Log::info('Mailer Error: ' . $mail->ErrorInfo);
        } else {
            Log::info('Mail sent', array('email' => $emails));
        }
    } catch (Exception $e) {
        Log::error($e);
    }
}

But sometimes and apparently with no explanation, at least I haven't found an explanation yet. It fails and displays the message "local.ERROR: SMTP Error: Could not connect to SMTP host."

I've searched, but I have found solutions only when the error is permanent and no email is sent at all. I haven't found anything about "usually it's working fine and sometimes it fails"

[2020-11-24 08:18:45] local.ERROR: SMTP Error: Could not connect to SMTP host. {"exception":"[object] (PHPMailer\PHPMailer\Exception(code: 0): SMTP Error: Could not connect to SMTP host. at myroute\vendor\phpmailer\phpmailer\src\PHPMailer.php:2052) [stacktrace] #0 myroute\vendor\phpmailer\phpmailer\src\PHPMailer.php(1878): PHPMailer\PHPMailer\PHPMailer->smtpConnect(Array) #1 myroute\vendor\phpmailer\phpmailer\src\PHPMailer.php(1601): PHPMailer\PHPMailer\PHPMailer->smtpSend('Date: Tue, 24 N...', 'This is a multi...') #2 myroute\vendor\phpmailer\phpmailer\src\PHPMailer.php(1437): PHPMailer\PHPMailer\PHPMailer->postSend() #3 myroute\app\Helpers\EmailHelper.php(48): PHPMailer\PHPMailer\PHPMailer->send() #4 myroute\app\Console\Commands\ShippingReport(75): App\Helpers\EmailHelper::sendMail(Object(Barryvdh\DomPDF\PDF), Array, 'file of ship\xC3...', 'body text...', 'filename...') #5 [internal function]: App\Console\Commands\ShippingReport->handle() #6 myroute\vendor\laravel\framework\src\Illuminate\Container\BoundMethod.php(32): call_user_func_array(Array, Array) #7 myroute\vendor\laravel\framework\src\Illuminate\Container\BoundMethod.php(90): Illuminate\Container\BoundMethod::Illuminate\Container\{closure}() #8 myroute\vendor\laravel\framework\src\Illuminate\Container\BoundMethod.php(34): Illuminate\Container\BoundMethod::callBoundMethod(Object(Illuminate\Foundation\Application), Array, Object(Closure)) #9 myroute\vendor\laravel\framework\src\Illuminate\Container\Container.php(576): Illuminate\Container\BoundMethod::call(Object(Illuminate\Foundation\Application), Array, Array, NULL) #10 myroute\vendor\laravel\framework\src\Illuminate\Console\Command.php(183): Illuminate\Container\Container->call(Array) #11 myroute\vendor\symfony\console\Command\Command.php(255): Illuminate\Console\Command->execute(Object(Symfony\Component\Console\Input\ArgvInput), Object(Illuminate\Console\OutputStyle)) #12 myroute\vendor\laravel\framework\src\Illuminate\Console\Command.php(170): Symfony\Component\Console\Command\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Illuminate\Console\OutputStyle)) #13 myroute\vendor\symfony\console\Application.php(1009): Illuminate\Console\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput)) #14 myroute\vendor\symfony\console\Application.php(273): Symfony\Component\Console\Application->doRunCommand(Object(App\Console\Commands\ShippingReport), Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput)) #15 myroute\vendor\symfony\console\Application.php(149): Symfony\Component\Console\Application->doRun(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput)) #16 myroute\vendor\laravel\framework\src\Illuminate\Console\Application.php(90): Symfony\Component\Console\Application->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput)) #17 myroute\vendor\laravel\framework\src\Illuminate\Foundation\Console\Kernel.php(133): Illuminate\Console\Application->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput)) #18 myroute\artisan(37): Illuminate\Foundation\Console\Kernel->handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput)) #19 {main} "}

The versions I'm using on my project:

  • Laravel Framework v5.8.38
  • PHPMailer v6.1.8
  • PHP v7.3.6
Alex
  • 1,230
  • 2
  • 24
  • 46
  • could it be just a timeout issue due to slow connection? You can enable SMTP debug as per this answer https://stackoverflow.com/questions/13574166/phpmailer-send-gmail-smtp-timeout – Oras Nov 24 '20 at 08:12
  • @Oras I added `$Mail->SMTPDebug = 2;` and today I see the same error but no extra error info added in laravel.log or any other log. So I still don't know why it's failing sometimes... – Alex Nov 25 '20 at 07:25

0 Answers0