Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

depalmo's avatar

Laravel SMTP mail driver configuration/timeouts

The official Laravel's documentation is lacking details, so I'm asking here.

We're using Laravel v10 with SMTP mail server (Mailgun) and have rather frequent issue of connection being reset. These are exact error messages:

Expected response code "250" but got empty code.
fgets(): SSL: Connection reset by peer

These two errors do not appear at the same time, but they are rather frequent. I would say that about 99% of emails are delivered, while 1% fails with one of these two messages.

Do note that all emails are sent using Queue, not directly.

After discussing with Mailgun support, this is their response:

From our backend, we can see that in the course of your sending, your connection is going idle and eventually causes a timeout with our SMTP server, and stalls the queue you've created. I think when you're getting those errors, it's happening when the connection is terminated so you're receiving back such errors like the empty response code you saw on the 250s and the Connection reset by peer.

How can I change my configuration so that it would connect per each email? Or how can I set the time it can idle, but then it must re-connect? I noticed timeout option in config/mail.php file, but there's no description of what it does, how it's used and what parameter does it accepts (miliseconds, seconds, minutes, ...).

This is my current configuration:

    'mailers' => [
        'smtp' => [
            'transport' => 'smtp',
            'url' => env('MAIL_URL'),
            'host' => env('MAIL_HOST', 'smtp.mailgun.org'),
            'port' => env('MAIL_PORT', 587),
            'encryption' => env('MAIL_ENCRYPTION', 'tls'),
            'username' => env('MAIL_USERNAME'),
            'password' => env('MAIL_PASSWORD'),
            'timeout' => null,
            'local_domain' => env('MAIL_EHLO_DOMAIN'),
        ],
    ],
0 likes
1 reply
LaryAI's avatar
Level 58

To address the issue of connection resets and timeouts when using Mailgun with Laravel's SMTP mail driver, you can adjust the timeout setting in your config/mail.php file. This setting controls the maximum number of seconds to wait for a response from the SMTP server.

Here's a step-by-step solution:

  1. Set the Timeout Value: You can set the timeout value in the config/mail.php file. The value is in seconds. For example, setting it to 60 seconds:

    'mailers' => [
        'smtp' => [
            'transport' => 'smtp',
            'url' => env('MAIL_URL'),
            'host' => env('MAIL_HOST', 'smtp.mailgun.org'),
            'port' => env('MAIL_PORT', 587),
            'encryption' => env('MAIL_ENCRYPTION', 'tls'),
            'username' => env('MAIL_USERNAME'),
            'password' => env('MAIL_PASSWORD'),
            'timeout' => 60, // Set timeout to 60 seconds
            'local_domain' => env('MAIL_EHLO_DOMAIN'),
        ],
    ],
    
  2. Ensure Each Email Uses a New Connection: To ensure that each email uses a new connection, you can configure the queue worker to handle this. Laravel's queue system can be configured to restart the worker after processing a certain number of jobs. This can help in ensuring that the connection is reset periodically.

    In your config/queue.php file, you can set the retry_after and timeout settings for your queue connection. For example:

    'connections' => [
        'database' => [
            'driver' => 'database',
            'table' => 'jobs',
            'queue' => 'default',
            'retry_after' => 90, // Retry after 90 seconds
            'timeout' => 60, // Timeout after 60 seconds
        ],
    ],
    
  3. Restart Queue Workers Periodically: You can also configure your supervisor configuration to restart the queue workers periodically. This can be done by setting the --max-jobs or --max-time options in your supervisor configuration file. For example:

    [program:laravel-worker]
    process_name=%(program_name)s_%(process_num)02d
    command=php /path/to/your/project/artisan queue:work --sleep=3 --tries=3 --max-jobs=100 --max-time=3600
    autostart=true
    autorestart=true
    user=your-username
    numprocs=8
    redirect_stderr=true
    stdout_logfile=/path/to/your/project/worker.log
    

    This configuration will restart the worker after processing 100 jobs or after 1 hour, whichever comes first.

By setting the timeout value and ensuring that the queue workers are restarted periodically, you can mitigate the issue of connection resets and timeouts with Mailgun's SMTP server.

If you continue to experience issues, you may also want to reach out to Mailgun support for further assistance, as they may have additional recommendations specific to their service.

Please or to participate in this conversation.