Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel mail strange timeout errors

I have some problems for a long time now.

My app is sending mails to the customers.
In last months I've migrated this app to the Laravel (5.4 currently).
Many times I am receiving error:

Swift_TransportException in AbstractSmtpTransport.php line 404:
Connection to my-smtp.company.com:25 Timed Out

Problem is that I cannot get rid of this error message.
It happen in about 10% of cases - or queued task and mails sent in realtime.
Strange is that those mails are send out in fact but error show up anyway.

I am using Windows server and for queued mails running listener this way:

D:\php-7.1.1-x64\php.exe D:\wwwroot\myapp\artisan queue:listen --timeout=60 --tries=1

I've made some tests and looks like when errors is throws it is always after 33-36 second after firing queue job or executing code in browser.

Changed max_execution_time time to the 60 seconds but that didn't helped.

Anyone can help me?

like image 606
Grzesiek Avatar asked Nov 09 '22 01:11

Grzesiek


1 Answers

I have the same problem, and my app running on Laravel 6.0

My solution:

1) create a custom mail service provider.

2) use SwiftTransport method "setTimeout".

<?php

namespace App\Providers;

use Illuminate\Mail\MailServiceProvider as MailProvider;
use Illuminate\Mail\TransportManager;

class MailServiceProvider extends MailProvider
{
    /**
     * Register the Swift Transport instance.
     *
     * @return void
     */
    protected function registerSwiftTransport()
    {
        $this->app->singleton('swift.transport', function ($app) {
            $transport = new TransportManager($app);
            $transport->setTimeout(config('mail.connection_timeout'));
            return $transport;
        });
    }

}

3) replace provider config in \config\app.php

'provider' => [
...
// Illuminate\Mail\MailServiceProvider::class,
App\Providers\MailServiceProvider::class,
...
],
like image 58
IISU Avatar answered Nov 15 '22 06:11

IISU