Using gmail smtp via Laravel: [Connection timed out #110] - php

I know this question is asked many times and have different solutions and I have tried all but no one has worked. This is my settings.
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=465
MAIL_USERNAME=abc#mydomain.com
MAIL_PASSWORD=mypassword
MAIL_ENCRYPTION=ssl
And application is currently running on Centos 7 OS.
I always get
Swift_TransportException in StreamBuffer.php line 269: Connection could not be established with host smtp.gmail.com [Connection timed out #110]
at Swift_Transport_StreamBuffer->initialize(array('protocol' => 'ssl', 'host' => 'smtp.gmail.com', 'port' => '465', 'timeout' => '30', 'blocking' => '1', 'tls' => false, 'type' => '1', 'stream_context_options' => array())) in AbstractSmtpTransport.php line 113
I have tried all solutions, by Replacing: smtp.gmail.com with 173.194.65.108, or replacing smtp.gmail.com by gmail-smtp-msa.l.google.com. When I do these two steps, it got server not found error.
If I change mail driver from smtp to sendmail or mail, no error is thrown but no email is sent.
If I use ip address of smtp.gmail.com, I got same result. If I change port from 465 to 587 and encryption from ssl to tls, nothing happens.
I have also created a file in etc/gai.conf and put precedence ::ffff:0:0/96 100. It worked one time. But later after two hours, it stopped working and started throwing same error.
I have also tried using my gmail id but all in vain.
The only solution that I couldn't be able to test is verifying httpd_can_sendmail as whenever I run command getsebool httpd_can_sendmail I get getsebool: SELinux is disabled. Is this the problem or is there any other way to get it fixed?

Here is my working mail config file:
return [
'driver' => 'smtp',
'host' => 'smtp.gmail.com',
'port' => 587,
'from' => ['address' => '*******#gmail.com', 'name' => '****'],
'encryption' => 'tls',
'username' => '*******#gmail.com',
'password' => '*******',
'sendmail' => '/usr/sbin/sendmail -bs',
];
but from your question i can see you already tried all this, so i can assume it is a network problem based on error 'connection time out', what i mean is that something is blocking your connection, it might be your firewall if you have.
so first you should check that the port 587 is open, or you can check if other application can connect with smtp.gmail.com (don't know how to do it in centOs).
And at last this might not be a problem but you have to enable 'Allow less secure apps' in your google account just for testing. (This is not permanent solution but for testing, if it works then you should enable two step verification in google account and then create new application, you can create new special password which you can use as your password for smtp server)

In my case it was necessary to clear config cache to pull up .env file new settings
php artisan config:cache

Since you are using gmail, but I don't know what password you are setting, the MAIL_PASSWORD variable must be the same as the gmail application password. In this case, Gmail provides a third-party application service so that these, (in this case your application), can send emails using this type of authentication.

Related

Laravel: Connection could not be established with host smtp.gmail.com :stream_socket_client() [duplicate]

I'm trying to send an email from Gmail using Laravel from localhost. I'm getting this error: Connection could not be established with host smtp.gmail.com [ #0]
I'm using ssl with port 465. I also tried 587 but it didn't work.
I also tried this but it didn't work. I found a lot of people suffering from the same problems, but the solutions I found didn't work.
Editor's note: disabling SSL verification has security implications. Without verification of the authenticity of SSL/HTTPS connections, a malicious attacker can impersonate a trusted endpoint (such as GitHub or some other remote Git host), and you'll be vulnerable to a Man-in-the-Middle Attack.
Be sure you fully understand the security issues before using this as a solution.
In Laravel project directory, edit config/mail.php and add the following:
'stream' => [
'ssl' => [
'allow_self_signed' => true,
'verify_peer' => false,
'verify_peer_name' => false,
],
]
It worked for me.
Fyi, my SMTP settings are:
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=465
MAIL_USERNAME=[Full Gmail Address]
MAIL_PASSWORD=[Google App Password obtained after two step verification on Google Account Page]
MAIL_ENCRYPTION=ssl
This one line change in .env file will make it work
MAIL_DRIVER=sendmail
If it also has no effect try to switch between the mail port
MAIL_PORT=587
Or
MAIL_PORT=465
This will work only if you enable "Allow Less secure app access" under google account
In your .env file you will need to set the email address and password of your email account:
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=test#gmail.com
MAIL_PASSWORD=testpassword
and in mail.php
<?php
return [
'driver' => env('MAIL_DRIVER', 'smtp'),
'host' => env('MAIL_HOST', 'smtp.mailgun.org'),
'port' => env('MAIL_PORT', 587),
'from' => ['address' => 'yourEmail#gmail.com', 'name' => 'Your Title'],
'encryption' => 'tls',
'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),
'sendmail' => '/usr/sbin/sendmail -bs',
'pretend' => false,
];
and clear the config cache with:
php artisan config:cache
here's what is working with me
MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=<your email>
MAIL_PASSWORD=<your app password>
MAIL_ENCRYPTION=tls
if you are two auth verification with your account , should be generate new app password and use into you env file. this is what working with me.
Have you tried changing the encryption to tls? I currently use a Gmail SMTP sever to send emails from my Laravel app. I use TLS and port 587
in localhost you can set your MAIL_DRIVER=smtp but on real server like cpanel you must to set MAIL_DRIVER=sendmail and edit your config/mail.php
'default' => env('MAIL_MAILER', 'sendmail'),
but its not good idea because after you send mail on google you get error on gmail like this :
Be careful with this message
Gmail could not verify that it actually came from tenetup.reminderme#gmail.com. Avoid clicking links, downloading attachments, or replying with personal information.
Change mail driver in both .env and mail.php into MAIL_DRIVER=mailgun

LARAVEL 9. ERROR LARAVEL EMAIL: Unable to connect with STARTTLS: stream_socket_enable_crypto(): SSL operation failed with code 1

Greetings to the community!
I have recently developed a web page with Laravel 9.X, which works correctly in local, but when I upload it to my Host, I am having problems with sending emails.
When I have been testing locally, it has been working correctly, and I have been using mailtrap. Now that I have uploaded it to the server, I have my own SMTP, and it is not working for me. I have the ssl certificate created, but there is no way it will send me any mail. I get the following error:
Unable to connect with STARTTLS: stream_socket_enable_crypto(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed.
SSL working fine when i visit the site with https.
I have configured my .env file, where I have put:
HOST: my smtp server; PORT: 587; ENCRYPTION TYPE: SSL; USERNAME AND PASSWORD: Those of my email account.
I have been touching also my file located in config/mail.php, putting the same configuration as in the .env file. I have also tried with other ports like 25, 465... among others.
I have tried to access the mail account from outlook, using the IMAP protocol, and I have been able to connect correctly, so the mail can be accessed.
I have checked lot of things in Google, and also every blogs y found in Stack Overflow, but nothing worked.
Any idea what is going on?
Am I missing something to configure?
I had this problem after upgrading to the new 9 version, so you need to follow two steps.
Edit your .env file located in the root of the project The place where your mail settings are described, change tls to null. If you do not have such a line, then add
MAIL_ENCRYPTION=null
If you have version 8 then this will be enough, but in version Laravel 9 you will need to open the file app/config/mail.php Find a section mailers smtp add two options 'auth_mode' => null and 'verify_peer' => false an example of how it would look
'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,
'auth_mode' => null,
'verify_peer' => false,
],

Connection could not be established with host free.mboxhosting.com [Connection refused #111] with laravel in free hosting

I've been hosted my laravel project in a free hosting (in awardspace). Everything works ok, except sending email. When I try to send an email by compiling the form from the contact page of my project, it returns me this error:
Swift_TransportException
Connection could not be established with host free.mboxhosting.com [Connection refused #111]
This is the outgoing mail settings that free hosting service offers:
This is how i've been set the file .env
MAIL_DRIVER=smtp
MAIL_HOST=free.mboxhosting.com
MAIL_PORT=465
MAIL_USERNAME=contatti#pprivitera.dx.am
MAIL_PASSWORD=mypassword
MAIL_ENCRYPTION=ssl
MAIL_FROM_ADDRESS=contatti#pprivitera.dx.am
MAIL_FROM_NAME=User
And this is how I've been set the config/mail.php file
'driver' => env('MAIL_DRIVER', 'smtp'),
'host' => env('MAIL_HOST', 'free.mboxhosting.com'),
'port' => env('MAIL_PORT', 465),
'from' => [
'address' => env('MAIL_FROM_ADDRESS', 'contatti#pprivitera.dx.am'),
'name' => env('MAIL_FROM_NAME', 'User'),
],
'encryption' => env('MAIL_ENCRYPTION', 'ssl'),
'username' => env('contatti#pprivitera.dx.am'),
'password' => env('mypassword')
Tests I did:
1)I tried to change MAIL_DRIVER from "smtp" to "sendmail" both from .env file and from the mail.php file, but it doesn't work because the website after 4 minutes of loading returns me: The GET method is not supported for this route. Supported methods: POST (I don't understand why, because in local the contact form works perfectly and anyway the Route /contact/submit is set as ::post and the method of my contact form is set as "=post" too)
2)I tried to change the MAIL_ENCRYPTION from "ssl" to "null" so I've changed also the port from 465 to 25 or 587 (both from .env file and from the config/mail.php file) but it returns me the error ([Connection refused #111])
3)I tried to use mailtrap with my credential, both with ssl encryption and with null encryption, using 2525 port or 465 (depend from encryption type used), but it returns me the same error:
Connection could not be established with host smtp.mailtrap.io [Connection refused #111]
4)With mailtrap if I change the MAIL_DRIVER from smtp to sendmail, it returns me the error that I've described in the 1st point
5)In my local machine everything works perfectly, just using mailtrap with MAIL_DRIVER set as smtp and the rest of configuration set with credential of my mailtrap account
I don't understand what's wrong in the configuration. Thanks for the help!
Try to use the MAIL_ENCRYPTION as tls. I think that this encryption will to works.
Try to use this two:
MAIL_ENCRYPTION=tls
MAIL_PORT=25

Test mail using gmail smtp using swiftmailer? [duplicate]

When I try to send an e-mail through my website running Laravel 4, I get this exception:
{"error":{"type":"Swift_TransportException","message":"Expected response code 250 but got code \"535\", with message \"535-5.7.8 Username and Password not accepted. Learn more at\r\n535 5.7.8 http:\/\/support.google.com\/mail\/bin\/answer.py?answer=14257 y70sm14744455qgd.3 - gsmtp\r\n\"","file":"\/var\/www\/vendor\/swiftmailer\/swiftmailer\/lib\/classes\/Swift\/Transport\/AbstractSmtpTransport.php","line":386}}
Here is my mail config:
return array(
'driver' => 'smtp',
'host' => 'smtp.gmail.com',
'port' => 465,
'from' => array('address' => 'mymail#gmail.com', 'name' => 'myname'),
'encryption' => 'ssl',
'username' => 'mymail#gmail.com',
'password' => 'lol',
'sendmail' => '/usr/sbin/sendmail -bs',
'pretend' => false,
);
I've tried the disable link i've found by googling this issue except it didn't make a difference.
Is there a way to tell Google "stop blocking this IP, it's me" ?
I tried the same thing and got the same error. So i personally checked my gmail account and i had a message from Gmail itself letting me know that they'd blocked an access attempt to my email account.
They showed an option to disable this security setting by visiting https://www.google.com/settings/security/lesssecureapps.
Things will actually be more straight forward if you visit that link already logged in into your Gmail account.
Warning
As #kodfire stated:
On May 30, 2022, this setting will no longer be available.
support.google.com/accounts?p=less-secure-apps&hl=en
Go to this link and disable unlock Captcha
https://accounts.google.com/b/0/DisplayUnlockCaptcha
Try this:
Change port to 587
Goto gmail setting https://www.google.com/settings/security/lesssecureapps
and active it.
https://www.google.com/settings/security/lesssecureapps and active it.
https://accounts.google.com/b/0/DisplayUnlockCaptcha and active it.
'port' => env('MAIL_PORT', 587), <br>
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
I had lot of issues finding answer for this. Then after doing lot of trial and error i have found an solution to this. Most of the solution above worked for other but it help me upto 50%.
So, This is how it worked for me (100%):
Activate 2 step Verification in google
Now you will be able to create app. Open it.
Create new app (other app) and give your app name.
Now generate password.
Next in laravel, Goto .env file and change
MAIL_USERNAME= 'App Name you created'
MAIL_PASSWORD= 'Generated Password for that app'
This should be able to send emails from your gmail account.
Please leave a comment if it doesn't works for you.
Did you activate 2-step google authentification? If so, you need to define a new "application-specific password" password: http://www.google.com/landing/2step/.
Remove the #gmail.com from username, already you mentioned smtp. So no need #gmail.com on your username. I'm 100% sure it is your problem, because I had that problem.

Swift_TransportException Failed to authenticate on SMTP server with username email#domain.com using 2 possible authenticators on shared host

I'm develop a website using Laravel 4.1. I config my email system and send email successfully on built-in server localhost:8000. But when I host the web on a shared hosting, it always tell: Swift_TransportException
Failed to authenticate on SMTP server with username "email#domain.com" using 2 possible authenticators
I don't know what the reason is? I've search on this site but no solution works.
return array(
'driver' => 'smtp',
'host' => 'smtp.gmail.com',
'port' => 587,
'from' => array('address' => 'email#gmail.com', 'name' => 'Test email'),
'encryption' => 'ssl',
'username' => 'your_gmail_username',
'password' => 'your_gmail_password',
'sendmail' => '/usr/sbin/sendmail -bs',
'pretend' => false,
);
I also used my our domain's email but it doesn't work neither. Hope for help.
In my case, I had a Cpanel dedicated server. My host informed me that exim mail server was failing and restarting a couple times a day.
It looked like all connection threads were being used up and then locked until it restarts.
My host added another 100 connection threads to the pool. Most probably this help you as well.

Categories