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

laracoft's avatar

How to connect SMTP using only IPv4

production.ERROR: Connection could not be established with host "ssl://email.example.com:465": stream_socket_client(): Unable to connect to ssl://email.example.com:465 (Connection refused)
  1. I'm getting this error above when my MAIL_MAILER=smtp
  2. After checking with openssl, I get the same error because the server is connecting using IPv6
  3. When I force openssl to use IPv4, it works
  4. How can I force Laravel to connect using IPv4?
  5. If I try to set MAIL_HOST=x.x.x.x, I get the error below
[2025-05-28 16:05:44] production.ERROR: Connection could not be established with host "ssl://1.2.3.4:465": stream_socket_client(): Peer certificate CN=`email.example.com' did not match expected CN=`1.2.3.4'
1 like
9 replies
Glukinho's avatar
Level 31

I believe IPv4/v6 is not a Laravel business but a thing that operating system should be aware of.

You can:

  1. Disable IPv6 at all on operating system level (for example, in CentOS this is done by nmtui program)

  2. Add to hosts file something like: 1.2.3.4 email.example.com so email.example.com will always be resolved using IPv4 address 1.2.3.4. Hosts file is inside /etc folder in Linux and in C:\Windows\system32\drivers\etc for Windows.

I'd prefer #1.

Glukinho's avatar

There is a third way, dirty and bad: you can set MAIL_HOST=1.2.3.4 (ip address) and disable certificate checking in mailer settings, like so:

// config/mail.php

'mailers' => [
    'smtp' => [
        // ...other settings...
        'stream' => [
                'ssl' => [
                    'allow_self_signed' => true,
                    'verify_peer' => false,
                    'verify_peer_name' => false,
                ],
            ],
            'verify_peer' => false,
    ],
],
laracoft's avatar

Thanks, I hardcoded the IP in /etc/host before reading your answer, will give you best answer anyway. Thank you.

Glukinho's avatar

@laracoft You are welcome, but remember that as soon as your mail provider changes IP, you get a lot of funny time trying to understand what is broken. I still recommend #1 way (disabling IPv6 on system level).

demonz's avatar

Bro i got the same issue, i had to use mailtrap port 587 and idk why it worked, i wanna go back to smtp.gmail.com but it won't work. I don't know where is the root issue, because I didn't change anything before it suddenly broke. Is it digitalocean or ploi io changing their configuration, or it's me ?

2 likes
Glukinho's avatar

@demonz Check here: https://support.google.com/a/answer/176600?hl=en

I think the problem is:

Starting May 1, 2025, Google Workspace accounts no longer support less secure apps, third-party apps, or devices that ask you to sign in to your Google Account with your username and password.

Gmail SMTP was always hard to set up, I recommend to use other services for email sending.

mkwsra's avatar

@demonz It's not you, I reached to DigitalOcean and to MailJet, both said they had nothing to with it, and it's very strange issue that I faced with some droplets and in some droplets (running the same code) didn't face any issue whatsoever.

But this has been driving me like crazy. :(

demonz's avatar

@mkwsra you can use port 2525 with tls , it will work for MailJet, I heard that digitalocean blocked ports 25,587,465 , then i raise a ticket and I asked them to unblock them, so mailtrap with 587 worked for me after that, but the odd part gmail account with port 587 or 465 didn't work ...

1 like

Please or to participate in this conversation.