trevorpan's avatar

Connection could not be established with host "127.0.0.1:2525": stream_socket_client(): Unable to connect to 127.0.0.1:2525 (Operation timed out)

This one is really annoying. Not sure exactly what's going on. I've found three similar posts about this issue but the answers are not clearing this up.

Using Beyond Code's Helo app, vite, laravel 9. When running phpunit the Helo app receives emails. However, when testing manually on a local site I receive the Operation timed out error.

// .env
MAIL_MAILER=smtp
MAIL_HOST=127.0.0.1
MAIL_PORT=2525
MAIL_USERNAME=Inbox-Name
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
// config/mail.php
    'mailers' => [
        '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'),
            'auth_mode' => null,
            'verify_peer' => false,
            'verify_peer_name' => false,
        ],

Nothing seems out of touch. Any ideas?

Have also flushed the browser history/cache and run php artisan optimize:clear

0 likes
7 replies
LaryAI's avatar
Level 58

It looks like you have the correct configuration set up in your .env and config/mail.php files. It's possible that the issue is related to your firewall settings. Try temporarily disabling your firewall and running the tests again. If that works, you can then try to configure your firewall to allow connections on port 2525.

If that doesn't work, you can try using a different port for your SMTP server. You can do this by changing the MAIL_PORT value in your .env file to a different port (e.g. MAIL_PORT=25). Then, you'll need to update the port value in your config/mail.php file to match the new port (e.g. 'port' => env('MAIL_PORT', 25)).

If that still doesn't work, you can try using a different SMTP server. You can do this by changing the MAIL_HOST value in your .env file to the hostname of the new SMTP server (e.g. MAIL_HOST=smtp.example.com). Then, you'll need to update the host value in your config/mail.php file to match the new hostname (e.g. 'host' => env('MAIL_HOST', 'smtp.example.com')).

If you're still having issues, you can try using a different mailer (e.g. MAIL_MAILER=sendmail). You can find more information about configuring the mailer in the Laravel documentation: https://laravel.com/docs/7.x/mail#configuration.

Hope this helps!

kristencolquhoun's avatar

If you are using Vagrant/Homestead, use 10.0.2.2 as your SMTP-Host. For Docker, use host.docker.internal as your SMTP-Host.

1 like
renzocastillo's avatar

@trevorpan hi did you solve it? Currently using laravel herd and receiving

[2024-02-26 10:19:19] local.ERROR: Connection could not be established with host 127.0.0.1 :stream_socket_client(): unable to connect to tcp://127.0.0.1:2525 (Connection refused) {"exception":"[object] (Swift_TransportException(code: 0): Connection could not be established with host 127.0.0.1 :stream_socket_client(): unable to connect to tcp://127.0.0.1:2525 (Connection refused) at /Users/renzocastillo/Dev/build-laravel/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport/StreamBuffer.php:261)
trevorpan's avatar
trevorpan
OP
Best Answer
Level 15

@renzocastillo what's your env environment variable say?

//Use https if you've set that otherwise http
APP_URL=http(s)://yoursite.test
1 like
renzocastillo's avatar

Thank you very much, Trevor! I did that and now I was able to receive the email at my Herd Mail GUI.

My APP_URL .env variable had a different value than the website's local domain from which I was accessing my project. If possible, could you tell me how is this related to email deliverability?

Please or to participate in this conversation.