Lennox Omondi
Lennox Omondi

Reputation: 234

Symfony\Component\Mailer\Exception\TransportException with message 'Expected response code "250" but got an empty response.'

I'm using Google Workspace SMTP relay service to send emails from my Laravel app. It has been working well for over more than a year now, but I'm not sure what exactly stopped it from working. When I try to send an email I get the following error:

>>> Illuminate\Support\Facades\Mail::to('[email protected]')->send(new App\Mail\CourseEnrolmentEmail($user, App\Course::first()));

// The error
Symfony\Component\Mailer\Exception\TransportException with message 'Expected response code "250" but got an empty response.'

I suspect it is related to my upgrade from Laravel 8 to Laravel 9, but not sure how to fix it.

My mail.php

'smtp' => [
            'transport' => 'smtp',
            '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,
        ],

My mail config

MAIL_DRIVER=smtp
MAIL_HOST=smtp-relay.gmail.com
MAIL_PORT=587
MAIL_ENCRYPTION=TLS
MAIL_FROM_NAME="My Name"
[email protected]

I'm authenticating via IP, so the password and username fields are not required

My GSuite Gmail routing configuration
Gsuite Gmail routing config
Note In the above config, I have tried checking TLS and also changing Allowed senders to "Only Registered Apps users in my domains" but the problem still persists.

I have tried suggestions from

  1. https://laracasts.com/discuss/channels/laravel/laravel-swift-mailer-exception-expected-response-code-250-but-got-an-empty-response-using-gmail-smtp-relay-database-queue-driver

  2. Laravel 9 - Infomaniak : Expected response code "250" but got code "550", with message "550 5.7.1 Sender mismatch"

  3. https://stackoverflow.com/a/43283422/11752623

  4. https://www.cubebackup.com/blog/how-to-use-google-smtp-service-to-send-emails-for-free/ Method 3

All of them were not successful. I appreciated your help in solving this issue.

Upvotes: 3

Views: 11970

Answers (1)

Lennox Omondi
Lennox Omondi

Reputation: 234

Found a solution, I went to vendor/symfony/mailer/Transport/Smtp/SmtpTransport.php under the assertResponseCode method. I echoed the response which showed the following:

421 4.7.0 Try again later, closing connection. (EHLO) r29-200a50c01d0000xxxxxxxxxxxx87edb.28 - gsmtp

More details about the error from google documentation

The issue was that Swift Mailer was using 127.0.0.1 as the domain for sending the mail which was unknown to Gmail.

So, the solution was to set my domain name in the config/mail.php file


        'smtp' => [
            'transport' => 'smtp',
            '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', 'mydomain.com')//this line here
        ],

More info:

  1. https://insights.rytass.com/gmail-smtp-relay-421-4-7-0-try-again-later-closing-connection-ehlo-cfcdac3cf9c7
  2. https://serverfault.com/questions/929559/postfix-error-421-4-7-0-try-again-later-closing-connection-ehlo

Upvotes: 4

Related Questions