Can't send emails from laravel project through smtp - php

These are the settings in my env file
MAIL_MAILER=smtp
MAIL_HOST=mail.codestudio.pk
MAIL_PORT=465
MAIL_USERNAME=hamza.niazi#codestudio.pk
MAIL_PASSWORD=**********
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=
MAIL_FROM_NAME="${APP_NAME}"
When I test on mailtrap.io it works fine also works fine with my gmail account. But whenever I try to send mail through my domain it gives this error.
Connection could not be established with host mail.codestudio.pk :stream_socket_client(): unable to connect to tcp://mail.codestudio.pk:465 (A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.

I hope this env works for you. I used the same setting to run it on my server
#MAIL_MAILER=smtp
#MAIL_ENCRYPTION=tls
#MAIL_PORT=465
MAIL_HOST=codestudio.pk
MAIL_USERNAME=hamza.niazi#codestudio.pk
MAIL_PASSWORD=**********
MAIL_FROM_ADDRESS=hamza.niazi#codestudio.pk
MAIL_FROM_NAME="${APP_NAME}"

Related

Laravel 8 Send Mail by Gmail get stream_socket_client(): unable to connect to tcp://smtp.gmail.com:587/465 (Connection timed out)

I have tried send mail by
MAIL_PORT=587 MAIL_ENCRYPTION=tls
AND
MAIL_PORT=465 MAIL_ENCRYPTION=ssl
And I allow the option "less secure app"
But always get this notification: Connection could not be established with host smtp.gmail.com :stream_socket_client(): unable to connect to tcp://smtp.gmail.com:587 (Connection timed out)
And same email and password can send mail in other website.
It's same Framework.
but I don't know why that one site can't send email and other can
My Laravel Version is Laravel Framework 8.34.0 and using ubuntu 20.04
How can I fix this problem Thanks everyone

How to resolve stream_socket_client() in production server

I've been having trouble with this, I tried using other mailers but it didn't work, I always get this error when I try to submit
Connection could not be established with host mail.mydomain.com
:stream_socket_client(): unable to connect to
tcp://mail.mydomain.com:465 (A connection attempt failed because the
connected party did not properly respond after a period of time, or
established connection failed because connected host has failed to
respond.
)
Here is my env file
MAIL_MAILER=smtp
MAIL_HOST=mail.mydomain.com
MAIL_PORT=465
MAIL_USERNAME=test#mydomain.com
MAIL_PASSWORD=*********
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=test#mydomain.com
MAIL_FROM_NAME="test"
This works when I'm using xampp (locally), I don't know why it didn't work on our production server.

Cannot send email from Laravel app in production

I'm trying to send emails from Laravel app that I have in production, but it's not working.
It gives me this error:
Connection could not be established with host smtp.googlemail.com :stream_socket_client(): unable to connect to ssl://smtp.googlemail.com:465 (Connection timed out)
But when I try to send email from localhost it works correctly.
my config mail
MAIL_DRIVER=smtp
MAIL_HOST=smtp.googlemail.com
MAIL_PORT=465
MAIL_USERNAME=*********
MAIL_PASSWORD=**********
MAIL_ENCRYPTION=ssl
also email is less secure:on
did you tried it out?
Using gmail smtp via Laravel: Connection could not be established with host smtp.gmail.com [Connection timed out #110]
Also make sure your if your gmail account has 2 factor authentication enabled or not.
If there is any two factor authentication enabled in your account then you can use App specific password from google account page.
There are lots of services you can use like Sendinblue, sendgrid etc.

Connection could not be established with host smtp.gmail.com [Connection refused #111] in laravel 5.6 email send

Email Sending failed from cpnel but it works fine in my localhost.
Here is my email configuration in .env file.
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=465
MAIL_USERNAME=myemail#gmail.com
MAIL_PASSWORD=mypassword
MAIL_ENCRYPTION=ssl
It works fine in localhost but not working in cpanel. In cpanel it shows
Connection could not be established with host smtp.gmail.com [Connection refused #111]
How do i solve this?
Problem Solved.
I made the change two things here MAIL_DRIVER and MAIL_PORT.
MAIL_DRIVER=sendmail
MAIL_PORT=587
So, my full code is:
MAIL_DRIVER=sendmail
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=myemail#gmail.com
MAIL_PASSWORD=mypassword
MAIL_ENCRYPTION=ssl
It works fine
I know this question has been answered but I have an alternative solution that matches the original description and might help someone. You did not answer Loek's question about whether you run cPanel or not. We run cPanel with lfd/csf firewall, and it has TCP_OUT configuration that lists what outbound ports may be connected to. Port 465 (tls) was not in that list, so we got 'Connection Refused' reports, even trying a low level telnet test on the command line of our VPS server:
$ telnet smtp.gmail.com 465
Trying 74.125.140.109...
telnet: connect to address 74.125.140.109: Connection refused
Trying 74.125.140.108...
telnet: connect to address 74.125.140.108: Connection refused
Trying 2a00:1450:400c:c08::6d...
telnet: connect to address 2a00:1450:400c:c08::6d: Connection refused
The same telnet test worked fine from my local machine.
After adding 465 to the TCP_OUT list:
Load WHM control panel
go to ConfigServer Security & Firewall
go to Firewall Configuration
find TCP_OUT and include 465 (or whatever port you are trying to connect to) to the list
click Change at the bottom of the page, then Restart lfd/csf.
Telnet now succeeds, as does smtpauth mail sending via PHPMailer:
$ telnet smtp.gmail.com 465
Trying 74.125.140.109...
Connected to smtp.gmail.com.
Escape character is '^]'.
(this indicates the socket connection was established.. you probably don't want to actually talk SMTP to Google so hit ctrl-] and type quit and hit enter to close the connection)
Of course this isn't cPanel specific, you may be running another firewall manager that is not allowing outbound connections on the port you're trying to connect to.
After couple of days research. I was having issue of Connection could not be established with host smtp.gmail.com [Connection refused ] because of my godaddy server
As GoDaddy blocks SMTP communication over ports 465 and 587 and possibly 25 originating from your site. They will allow mail flow only through their own SMTP servers.
That was issue in my case. hope it helps someone
For reference check this:
https://pk.godaddy.com/community/Using-WordPress/Connection-Refused-for-GMail-SMTP/td-p/33107
I did face this issue before, I did allow less secure apps from google account manager. And my problem was solved.
It seems like that you too need authorization for extension application in order to use gmail.
You can follow given below steps...
Sign-in into your gmail account.
Access this link to change settings.
Then you have to turn on Allow less secure apps.
After that try again to send email through you host.
I made the nexts steps:
Enter to cPanel of GoDaddy
Go to the option > Email Accounts
Clic in the option > + Create and create a email.
and after change
I resolve this problem by changing the file .env
MAIL_MAILER=smtp
MAIL_HOST=localhost
MAIL_PORT=25
MAIL_USERNAME=null
MAIL_PASSWORD=
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS=emailcreated#email.com
MAIL_FROM_NAME="NAME_APP"
I hope that help !!

Connection could not be established with host smtp.mailtrap.io [Connection timed out #110]

I want to send email with laravel and I use mailtrap for this
when I tested it in local host this work correctly but in sharing host it shows me this error:
Connection could not be established with host smtp.mailtrap.io [Connection timed out #110]
this is my .env file:
MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=194f4****98d84
MAIL_PASSWORD=e76fc****f79e7
MAIL_ENCRYPTION=null
when I changed DRIVER value from smtp to sendmail it shows me this error:
"proc_open() has been disabled for security reasons"
Outbound mails could be blocked by your hosting provider. As an example, DigitalOcean blocks the out going emails, for security and spam reasons, and to get it running, you must contact the support team.
I would suggest doing the same with your hosting provider.

Categories