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)
I'm getting this error above when my MAIL_MAILER=smtp
After checking with openssl, I get the same error because the server is connecting using IPv6
When I force openssl to use IPv4, it works
How can I force Laravel to connect using IPv4?
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'
I believe IPv4/v6 is not a Laravel business but a thing that operating system should be aware of.
You can:
Disable IPv6 at all on operating system level (for example, in CentOS this is done by nmtui program)
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.
Laravel uses this Symfony Mailer component for sending emails; there is an option to bind to ipv4, so maybe you can add custom option source_ip = 0.0.0.0 to your mailer config:
@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).
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 ?
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.
@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.
@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 ...