Laravel - Connection could not be established with host smtp.gmail.com [ #0] - laravel-4

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

Related

Laravel 8 sending mailgun via api Client error: Unauthorized response: Forbidden

I recently upgraded my Laravel from 5.7.29 to 8.51. I have been using the Mailgun API to send emails for years. The old version of the site is still able to send via mailgun, but the new version keeps returning this message:
Client error: `POST https://api.mailgun.net/v3/mg.clstracking.com/messages.mime` resulted in a `401 Unauthorized` response: Forbidden
I found similar posts that indicate that kind of response if you're working in the EU and fail to change the MAILGUN_ENDPOINT in the services config. The server and site are all in the USA. I have verified I have the correct settings in my .env and that those are passed into my services.php config file and cached using artisan config:cache. I verified this by looking through the /bootstrap/cache/config.php and everything is there - I even checked this against another site with a different domain that is working the same way. I even tried hard-coding my domain and secret into the services.php.
If I change
MAIL_MAILER=smtp
MAIL_HOST=smtp.mailtrap.io
with my username and password, I get no error, but the email isn't sent and checking the logs on mailgun, there is no record. I'm at a loss what else to try.
In env I have:
MAIL_MAILER=mailgun
MAILGUN_DOMAIN=mg.mydomain.com
MAILGUN_SECRET=key-##########################
In config/services.php:
'mailgun' => [
'domain' => env('MAILGUN_DOMAIN'),
'secret' => env('MAILGUN_SECRET'),
'endpoint' => env('MAILGUN_ENDPOINT', 'api.mailgun.net'),
],
In config/mail.php:
'default' => env('MAIL_MAILER', 'mailgun'),
'mailers' => [
'mailgun' => [
'transport' => 'mailgun',
],
],
I use this config in my projects which are in production with gmail
MAIL_MAILER='sendmail'
MAIL_HOST='sendmail.googlemail.com'
MAIL_PORT=587
MAIL_USERNAME='email#gmail.com'
MAIL_PASSWORD="*********"
MAIL_ENCRYPTION='tls'
MAIL_FROM_ADDRESS='email#gmail.com'
MAIL_FROM_NAME="${APP_NAME}"
Maybe helps you.

Expected response code 250 but got code "530", with message "530 5.7.1 Authentication required in Laravel with Mailtrap.io

I looked through the answers on SO but nothing seems to work. The weird thing is that I have the exact same .env variables and config/mail.php in another local project, and in that project everything works as expected.
MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=my_mailtrap_username
MAIL_PASSWORD= my_mailtrap_password
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=test#example.com
MAIL_FROM_NAME=Example
In config/mail.php I have
'driver' => env('MAIL_DRIVER', 'smtp'),
'host' => env('MAIL_HOST', 'smtp.mailgun.org'),
'port' => env('MAIL_PORT', 587),
'from' => [
'address' => env('MAIL_FROM_ADDRESS', 'hello#example.com'),
'name' => env('MAIL_FROM_NAME', 'Example'),
],
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),
I know that MAIL_HOST has a different fallback vs the host that I have specified in my .env, but the second value is the fallback and it should just be using that from my .env file. Again, this works fine in an other local project.
I ran:
php artisan config:clear
php artisan config:cache
php artisan queue:restart
All the facepalm memes in the world can not describe how I feel right now.
I had all the MAIL variables in the .env file declared AGAIN further down in the file, so they got overwritten... I also still had my .env.example file but I don't think that caused the issue.

SOLUTION for: Expected response code 250 but got code "530", with message "530 5.71 Authentication required

This first code was what solved everything. Use this code exactly inside your config/mail.php file. With your own full Gmail address and Google app password.
Where I got the solution
'username' => env('MAIL_USERNAME','full-gmail-address-here'),
'password' => env('MAIL_PASSWORD','Google-app-password-generated-when-setting-up-2-factor-auth'),
You might have written this 2nd code but here it is ... Goes inside your env file.
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587 (or use 465 for ssl on the sixth line below)
MAIL_USERNAME=Your-full-Gmail-address-here
MAIL_PASSWORD=Your-Google-App-Password-here
MAIL_ENCRYPTION=tls (or ssl)
MAIL_AUTH=YES (<= I made this up, if you're a pro you can check if it's necessary)
MAIL_FROM_ADDRESS=Email-address-of-sender
MAIL_FROM_NAME=Name-of-sender
Thirdly, don't forget to include this inside the config/mail.php file
'stream' => [
'ssl' => [
'allow_self_signed' => true,
'verify_peer' => false,
'verify_peer_name' => false,
],
],
Please pass it on to whoever may need it. Thanks
When change in .env you just clear cache & config
php artisan cache:clear
php artisan config:clear
Then test your .env data
php artisan tinker
config('mail')
check your data is correct or not.Your process is correct here.

Connecting to AWS SES with Laravel

Currently working on a user registration form which (as expected) sends out an email to the registered user.
The client decided to go with AWS SES, so I have already had this configured.
.env
MAIL_DRIVER=smtp
MAIL_HOST=email-smtp.eu-west-1.amazonaws.com
MAIL_PORT=587
MAIL_USERNAME=my_username
MAIL_PASSWORD=my_password
MAIL_ENCRYPTION=null
Also defined the following credentials:
SES_KEY=keyRetrievedFromMyCredentialsInSES
SES_SECRET=passwordRetrievedFromMyCredentialsInSES
And also telnet email-smtp.eu-west-1.amazonaws.com 587
Trying 52.19.235.197...
Connected to ses-smtp-eu-west-1-prod-345515633.eu-west-1.elb.amazonaws.com.
What could have gone wrong here?
It may not working without ADDRESS and NAME setting.
MAIL_DRIVER=smtp
MAIL_HOST=null
MAIL_PORT=null
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS = 'hello#example.com'
MAIL_FROM_NAME = 'Example'
After change setting in .env. Run config:cache:
php artisan config:cache
Check documentation -> https://laravel.com/docs/7.x/mail
Install guzzle
composer require guzzlehttp/guzzle
Install AWS SDK for php (laravel)
composer require aws/aws-sdk-php
Check values in .env file
MAIL_DRIVER=ses
MAIL_HOST=email-smtp.eu-west-1.amazonaws.com //aws SMTP Settings -> check mail host
MAIL_PORT=465
MAIL_USERNAME=smpt_username //aws SMTP Settings -> click Create My SMTP Credentials
MAIL_PASSWORD=smpt_password //aws SMTP Settings -> click Create My SMTP Credentials
MAIL_ENCRYPTION=ssl
MAIL_FROM_ADDRESS=your_email //aws Email Addresses -> add email address -> send test email (in sendbox mode you can send email only on same email for testing)
MAIL_FROM_NAME="${APP_NAME}"
AWS_ACCESS_KEY_ID= aws_access_key_id //create user or in existing user check access key
AWS_SECRET_ACCESS_KEY= secret_access_key //create user or in existing user check secret key
AWS_DEFAULT_REGION=eu-west-1 //change with your region
AWS_BUCKET=
In config/service.php
'ses' => [
'key' => env('AWS_ACCESS_KEY_ID'),
'secret' => env('Secret access key'),
'region' => env('AWS_SECRET_ACCESS_KEY', 'eu-west-1'), // change with your region
],
Clear cache
php artisan config:cache

Laravel 5.1 send email (Connection could not be established with host smtp.gmail.com [connection time out #110]

I'm learning Laravel 5.1 mail for my project. So, I try to send simple email first from a new Laravel 5.1 application on my localhost.
this is my .env config file
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=mygmail#gmail.com
MAIL_PASSWORD=mygmailpassword
MAIL_ENCRYPTION=tls
and this is my config/mail.php
'driver' => env('MAIL_DRIVER', 'smtp'),
'host' => env('MAIL_HOST', 'smtp.mailgun.org'),
'port' => env('MAIL_PORT', 587),
'from' => ['address' => 'sender#gmail.com', 'name' => 'Sender'],
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),
'sendmail' => '/usr/sbin/sendmail -bs',
this is my function to send the email
Mail::send('emails.email', ['user'=>'Sender'], function($m){
$m->to('receiver#yahoo.com','receiver')->subject('subject');
});
With this, when I run the application, it always return the error
Swift_TransportException in StreamBuffer.php line 265:
Connection could not be established with host smtp.gmail.com [Connection timed
out #110]
What I've tried:
Turn on 'less secure apps' on google account
2-step verification (generate an app password) and use it on (MAIL_PASSWORD on .env config)
But, none of this succeed. What am I doing wrong here?
PS: My OS is Linux Ubuntu 14.04
Did you try by changing these
MAIL_PORT=465
MAIL_ENCRYPTION=ssl
Write a ticket to your hosting support. I've had the same problem: my mails were successfully sent at localhost, but at the hosting there were troubles.
I've wrote to the technical support and the engineer said that the port 587 is blocked by default and they can unblock it if I wish. When the port was unblocked the error disappeared.
So easy)

Resources