Send mail with gmail in laravel 7 - laravel

I'm trying to send mail with Gmail in laravel 7. I get the result in localhost but when I upload the project on the host I recieve this error and email not send:
local.ERROR: Connection could not be established with host smtp.gmail.com :stream_socket_client(): unable to connect to tcp://smtp.gmail.com:587 (Connection refused) {"userId":1,"exception":"[object] (Swift_TransportException(code: 0): Connection could not be established with host smtp.gmail.com :stream_socket_client(): unable to connect to tcp://smtp.gmail.com:587 (Connection refused) at /home/wbrubkcom/libs/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport/StreamBuffer.php:269)
My env config for email is:
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=myemail
MAIL_PASSWORD=myemailpassword
MAIL_ENCRYPTION=tls
I searched in StackOverflow and test different ways such as useing Gmail app password or change port to 465 or change encryption to ssl but I don't get the result.

You need to go to
Manage google account->security->less secure app
and then allow it ..
may be that will resolve your issue.

Related

Connection could not be established with host smtp.sendgrid.net :stream_socket_client(): unable to connect to tcp://smtp.sendgrid.net:465

I am trying to send mail using sendgrid in Laravel but it is working on localserver but as i hosted it on server it is giving me following error message:
my mail settings in .env file:
MAIL_DRIVER=smtp
MAIL_HOST=smtp.sendgrid.net
MAIL_PORT=587
MAIL_USERNAME=myusername
MAIL_PASSWORD=XXX
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=info#xyz.com
MAIL_FROM_NAME=xyz
For laravel >= 7
And you are getting this error :
Connection could not be established with host smtp.sendgrid.net :stream_socket_client(): unable to connect to tcp://smtp.sendgrid.net:587 (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
Simply update your .env file
From:
MAIL_MAILER=smtp
MAIL_DRIVER=smtp
MAIL_HOST=smtp.sendgrid.net
MAIL_PORT=587
MAIL_USERNAME=apikey
MAIL_PASSWORD=your_sendgrid_api_key
MAIL_ENCRYPTION=tls
MAIL_FROM_NAME="Your Sender Name"
MAIL_FROM_ADDRESS=info#example.com
To:
MAIL_MAILER=sendMail
MAIL_DRIVER=sendMail
MAIL_HOST=smtp.sendgrid.net
MAIL_PORT=587
MAIL_USERNAME=apikey
MAIL_PASSWORD=your_sendgrid_api_key
MAIL_ENCRYPTION=tls
MAIL_FROM_NAME="Your Sender Name"
MAIL_FROM_ADDRESS=info#example.com
Then run this command to make sure .env changes is sync with the code.
php artisan config:cache
Update your env file to:
MAIL_MAILER=sendmail
MAIL_HOST=smtp.sendgrid.net
MAIL_PORT=587
MAIL_USERNAME=apikey
MAIL_PASSWORD=apiPassword
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=info#example.com
MAIL_FROM_NAME="${APP_NAME}"
Also in your Laravel project under the config folder look for
mail.php
and make changes as follows:
'default' => env('MAIL_MAILER', 'sendmail'),
N/B: SMTP works on localhost and sendmail works on live server.
If you say it works locally but not on production server, try a php artisan config:cache on your web host server. Make sure the ENV configuration is loaded properly (if your host does not support SSH connections, you need to manually update the configuration from ENV into /app/config/* files.
If the issue persists, there are only two possibilities:
Make sure your web host allows outgoing SMTP connections.
Make sure sendgrid.net did not ban your web host outgoing SMTP IP address.
I Experienced this while using a Linode server. Apparently, Some service provider like Linode blocks an SMTP port until you reach out to them via support and ask them for unblock before you can use that PORT and send Email. That was the fix for me
when I migrated to laravel 9, sending with sendgrid didn't work anymore.
The problem was because the new symfony mailer was trying to send with ssl beside tls was set in the config file.
So in the config file, I changed the port from 587 to 465 and it worked again.

stream_socket_client(): unable to connect to tcp://smtp.sendgrid.net:587

I send Emails using Sendgrid Service. In my local environment, it's working perfectly. but Production Server Environment I got the following error.
Swift_TransportException GET /rpc/insurances/sendregisemail/a9e13e20-2910-11ea-88e2-534f2fd3cc6a .
Connection could not be established with host smtp.sendgrid.net :stream_socket_client(): unable to connect to tcp://smtp.sendgrid.net:587 (Connection refused)
This solved my problem
MAIL_DRIVER=smtp or MAIL_MAILER=smtp for Laravel >= 7
MAIL_PORT=587
To
MAIL_DRIVER=sendmail or MAIL_MAILER=sendmail for Laravel >= 7
MAIL_PORT=587

Swift_TransportException Connection could not be established with host smtp.gmail.com [Network is unreachable #101]

Some months ago, I used gmail setting in my .env file,
MAIL_DRIVER=sendmail
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=myEmail#gmail.com
MAIL_PASSWORD=myPassword
MAIL_ENCRYPTION=tls
It was working perfectly fine. But suddenly it stopped working.
Now, it is showing an error, which is:
ErrorException (E_WARNING) proc_open() has been disabled for security reasons
What's going on?? I have tried all the methods found on internet but didn't worked.
Did Gmail got any new security system?
I have tried changing,
MAIL_DRIVER=sendmail to MAIL_DRIVER=smtp
But getting below error,
Swift_TransportException Connection could not be established with host smtp.gmail.com [Network is unreachable #101]

Error: connection could not be established with host smtp.gmail.com #0 - LARAVEL

I am trying send emails but I am getting the error below:
"connection could not be established with host smtp.gmail.com #0".
I made test with different ports (465, 578), with SSL and TLS and the last with the mail account and it is working.
I think that the problem is on XAMPP.
What am I doing wrong and how can I resolve it?
Try using:
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_ENCRYPTION=tls
MAIL_USERNAME=y*****4#gmail.com
MAIL_PASSWORD=a*****
Hope, it works for you.

Email Sending not working in CentOS 7 but same credentials works on localhost

I created an email account on godaddy which works perfectly on server and on localhost. When I did the testing on Linode server using the same email credentials. It did not work and gave 500 Internal Server Error.
Swift_TransportException Connection could not be established with host
sg6plcpnl0067.prod.sin3.secureserver.net [ #0]
Am I missing something?
.env details
MAIL_DRIVER=smtp
MAIL_HOST=sg6plcpnl0067.prod.sin3.secureserver.net
MAIL_PORT=465
MAIL_USERNAME=noreply#myserver.in
MAIL_PASSWORD=mypassword
MAIL_ENCRYPTION=ssl
MAIL_FROM_NAME=myserver
MAIL_FROM_ADDRESS=noreply#myserver.in

Resources