Expected response code 220 but got code "", with message "" in Laravel - php

I am using Laravel Mail function to send email. The following is my app/config/mail.php file settings.
'driver' => 'sendmail',
'host' => 'smtp.gmail.com',
'port' => 587,
'from' => array('address' => 'email#gmail.com', 'name' => 'MyName'),
'encryption' => 'tls',
'username' => 'myUsername',
'password' => "password",
'sendmail' => '/usr/sbin/sendmail -bs',
'pretend' => false,
Controller Mail Method
//Send Mail
Mail::send('sendMail', array('key' => 'value'), function($message)
{
$message->to('EmailId#hotmail.com', 'Sender Name')->subject('Welcome!');
});
When I run the code it gives me following error message:
Swift_TransportException
Expected response code 220 but got code "", with message ""
I have created a SendMail.php file in view which contains some data.
How do I resolve this error message?

This problem can generally occur when you do not enable two step verification for the gmail account (which can be done here) you are using to send an email. So first, enable two step verification, you can find plenty of resources for enabling two step verification. After you enable it, then you have to create an app password. And use the app password in your .env file. When you are done with it, your .env file will look something like.
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=<<your email address>>
MAIL_PASSWORD=<<app password>>
MAIL_ENCRYPTION=tls
and your mail.php
<?php
return [
'driver' => env('MAIL_DRIVER', 'smtp'),
'host' => env('MAIL_HOST', 'smtp.gmail.com'),
'port' => env('MAIL_PORT', 587),
'from' => ['address' => '<<your email>>', 'name' => '<<any name>>'],
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),
'sendmail' => '/usr/sbin/sendmail -bs',
'pretend' => false,
];
After doing so, run php artisan config:cache and php artisan config:clear, then check, email should work.

In my case I had to set the
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=465 <<<<<<<------------------------- (FOCUS THIS)
MAIL_USERNAME=<<your email address>>
MAIL_PASSWORD=<<app password>>
MAIL_ENCRYPTION= ssl <<<<<<<------------------------- (FOCUS THIS)
to work it.. Might be useful.
Rest of the code was same as #Sid said.
And I think that editing both environment file and app/config/mail.php is unnecessary. Just use one method.
Edit as per the comment by #Zan
If you need to enable tls protection use following settings.
MAIL_PORT=587
MAIL_ENCRYPTION= tls
See here for some other gmail settings

What helped me... changing sendmail parameters from -bs to -t.
'sendmail' => '/your/sendmail/path -t',

if you are using Swift Mailer:
please ensure that your $transport variable is similar to the below,
based on tests i have done, that error results from ssl and port misconfiguration.
note: you must include 'ssl' or 'tls' in the transport variable.
EXAMPLE CODE:
// Create the Transport
$transport = (new Swift_SmtpTransport('smtp.gmail.com', 465, 'ssl'))
->setUsername(you#example.com)
->setPassword(password)
;
// Create the Mailer using your created Transport
$mailer = new Swift_Mailer($transport);
// Create a message
$message = (new Swift_Message('News Letter Subscription'))
->setFrom(['app#example.com' => 'A Name'])
->setTo(['someone#example.com' => 'A Name'])
->setBody('your message body')
;
// Send the message
$result = $mailer->send($message);

I was facing the same issue. After researching too much on Google and StackOverflow. I found a solution very simple
First, you need to set environment like this
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=example#gmail.com
MAIL_PASSWORD=example44
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=example#gmail.com
you have to change your Gmail address and password
Now you have to on less secure apps by following this link https://myaccount.google.com/lesssecureapps

For me the problem was the port. I first incorrectly used port 465, which works for SSL but not TLS. So the key thing was changing the port to 587.

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 error when sending custom email address using smtp server

I have a project that has a module that sends email from request. Im using beautymail package for the email template. I can send an email using a gmail account, but there is this email that I get from my client that has a custome email address in it. Like this xx.xxxxx#propnex.sg, they said that the email is a smtp server. So I tried configuring my .env and other configuration files in laravel. But I get this error upon sending Connection could not be established with host mail.propnex.sg :stream_socket_client(): SSL operation failed with code 1. OpenSSL Error messages: error:1408F10B:SSL routines:ssl3_get_record:wrong version number Could someone tell me why Im getting this error and what should I do to get rid of this? Thanks a lot
.env config
MAIL_DRIVER=smtp
MAIL_HOST=mail.propnex.sg
MAIL_PORT=587
MAIL_USERNAME=xx.xxxxx#propnex.sg
MAIL_PASSWORD=xxxxxxxx
MAIL_ENCRYPTION=ssl
Mail.php
'from' => [
'address' => 'xx.xxxxx#propnex.sg',
'name' => 'Propnex',
],
'reply_to' => ['address' => 'xx.xxxxx#propnex.sg', 'name' => 'Propnex'],
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),
'port' => env('MAIL_PORT', 587),
'driver' => env('MAIL_DRIVER', 'smtp'),
'host' => env('MAIL_HOST', 'mail.propnex.sg'),
I just solved it by making a small changes in my .env and mail.php file like this
MAIL_ENCRYPTION=""
'encryption' => env('MAIL_ENCRYPTION', ''),
Tell me what are the drawbacks in this unsecured/alternative answer of mine. I want feedbacks. Thanks a lot.
You're using SSL (MAIL_ENCRYPTION=ssl) so you need to change MAIL_PORT=465 in .env file.
SSL VS TLS encryption
for TLS use Port : 587
for SSL use Port : 465
You might have crossed check every bit of line you have. The issue is with mailtrap.io it self. If your configuration is actually like this below.
MAIL_MAILER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=31466861b7bXXX
MAIL_PASSWORD=fe2b6503618XXX
MAIL_ENCRYPTION=null
I tested with two separate framework to proof this error. check the SMTP credentials try resetting the credentials.
You are setting mail encryption as tls in mail.php and on the other hand in the env file
you are setting it to ssl. Try setting it the same in both the files.
But i would recommend that you skip the certificate.
You can skip verification of the ssl certificate by using the code below in the mail.php file:
'stream' => [
'ssl' => [
'allow_self_signed' => true,
'verify_peer' => false,
'verify_peer_name' => false,
],
],
in you .env file
change this
'encryption' =>ssl
line into
'encryption' =>tls
and make sure you have some value for MAIL_FROM_ADDRESS also .
this worked for both localhost & server environments fine.

Email doesn't send from Laravel to Webmail [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 4 years ago.
I can't send email from my Laravel live project to an webmail account.
I have configured all possible files to send email.
config/mail.php
'driver' => env('MAIL_DRIVER', 'smtp'),
'host' => env('MAIL_HOST', 'sg2plcpnl0003.prod.sin2.secureserver.net '),
'port' => env('MAIL_PORT', 465),
'from' => [
'address' => env('MAIL_FROM_ADDRESS', 'info#ezmoverandrental.com'),
'name' => env('MAIL_FROM_NAME', 'Test'),
],
'encryption' => env('MAIL_ENCRYPTION', 'ssl'),
'sendmail' => '/usr/sbin/sendmail -bs',
.env
MAIL_DRIVER=mail //also tried with 'sendmail' and 'mail'
MAIL_HOST=sg2plcpnl0003.prod.sin2.secureserver.net
MAIL_PORT=587 //also tried with '645'
MAIL_USERNAME=info#ezmoverandrental.com
MAIL_PASSWORD='webmail_password'
MAIL_ENCRYPTION=ssl //also tried with 'tls'
Another interesting part is there is no error shows in my 'storage/log' file. Just I see in console that it returns '0'.
Here is the send mail method
public function sendMail($emailDataArray)
{
Mail::send('mail', $emailDataArray, function($message) use ($emailDataArray)
{
$message->to('info#ezmoverandrental.com')->subject('New Online Estimate');
$message->from('info#ezmoverandrental.com');
});
return;
}
That means my project is connected successfully with the webmail but unfortunately mail doesn't send.
I have already tried with 'sendmail' and 'mail' drive
Anybody Help Please.
Your Method is Ok just Double check the values and make sure mail port,Drivers and the ENCRYPTION values are the same in the env and mail.php are same.
.env file
`MAIL_PORT=587
MAIL_DRIVER=smtp
MAIL_ENCRYPTION=tls`
mail.php file
`'port' => env('MAIL_PORT', 587)
'driver' => env('MAIL_DRIVER', 'smtp')
'encryption' => env('MAIL_ENCRYPTION', 'tls') `
It seems your method is okay. Suggest you double check your mail host .env.
You could use Mail Gun or Mailtrap
**Mailtrap .env configuartion**
MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=your mailtrap username hashcode
MAIL_PASSWORD=your mailtrap password hashcode

Swift_TransportException in Laravel 5.2 mail sending

I am trying to send mail for my laravel app from a gmail account with Allow less secure apps: ON and 2-Step Verification OFF
.env part for mail:
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=library#gmail.com
MAIL_PASSWORD=********
MAIL_ENCRYPTION=ssl
config/mail.php:
'driver' => env('MAIL_DRIVER', 'smtp'),
'host' => env('MAIL_HOST', 'smtp.gmail.com'),
'port' => env('MAIL_PORT', 587),
'from' => ['address' => 'library#gmail.com', 'name' => 'Admin'],
'encryption' => env('MAIL_ENCRYPTION', 'ssl'),
'username' => env('library#gmail.com'),
'password' => env('********'),
'sendmail' => '/usr/sbin/sendmail -bs',
'pretend' => env('MAIL_PRETEND', false),
First I tried using 'encryption' => env('MAIL_ENCRYPTION','tls'). But got the following error message:
ERROR: exception 'Swift_TransportException' with message 'Expected
response code 250 but got code "530", with message "530-5.5.1
Authentication Required. Learn more at
530 5.5.1 https://support.google.com/mail/answer/14257
z3sm16020712par.17 - gsmtp"' in
/home/shafi/Projects/Lib/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport/AbstractSmtpTransport.php:383
I visited https://support.google.com/mail/answer/14257 and found everything is at the recommended state.
After this I used 'encryption' => env('MAIL_ENCRYPTION','ssl') but this time the same ERROR.
What should I do to fix the error? What am I missing?
Visit http://www.google.com/accounts/DisplayUnlockCaptcha and sign in with your Gmail username and password. If asked, enter the letters in the distorted picture.
Note: If this still didn't help then retry the process couple of more times and wait then try to send the email from your Laravel apps. This should resolve the issue.
Step 1: Revert your config/mail.php to original file. The config/mail.php should look like this
'driver' => env('MAIL_DRIVER', 'smtp'),
'host' => env('MAIL_HOST', 'smtp.mailgun.org'),
'port' => env('MAIL_PORT', 587),
'from' => ['address' => 'library#gmail.com', 'name' => 'Admin'],
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),
'sendmail' => '/usr/sbin/sendmail -bs',
Step 2: The .env file should be like
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=465
MAIL_USERNAME=library#gmail.com
MAIL_PASSWORD=whateverisyourpassword
MAIL_ENCRYPTION=ssl
Port is 465 and not 587
Step 3: Test the below code by pasting in appropriate controller function
\Mail::raw('This is an test e-mail', function ($message) {
$message->to("someone#gmail.com", "someone");
$message->subject("hi checking");
$message->getSwiftMessage();
});
P.S: Never ever forget to stop and then again start the server by using php artisan serve every time if some change is made to .env file. If not then you may be sucked into The Labyrinth of Time.
For me, sending mail worked by just adding the correct info in the .env file like so:
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=your_gmail_email
MAIL_PASSWORD=your_gmail_password
MAIL_ENCRYPTION=tls
Don't change anything in the mail.php file. You don't want your email credentials all over the place. The .env file was made to store all of this.
I also turned on access to "less secure apps" on that Gmail account.
And lastly, run: php artisan config:cache
Hopefully that helps.
I had the same problem previously, the root cause was I changed the mail.php file.
To send email with smtp.gmail.com, you just need to change the configuration in .env file and enable allow less secure apps. Don't change anything in the mail.php file.
I'd recommend you to take the time to read this :
https://phpnotebook.com/how-to-fix-swift_transportexception-530-in-laravel-5-7.html

Try to send mail with Laravel 5.0 but got error "Expected response code 220 but got code "", with message """ [duplicate]

I am using Laravel Mail function to send email. The following is my app/config/mail.php file settings.
'driver' => 'sendmail',
'host' => 'smtp.gmail.com',
'port' => 587,
'from' => array('address' => 'email#gmail.com', 'name' => 'MyName'),
'encryption' => 'tls',
'username' => 'myUsername',
'password' => "password",
'sendmail' => '/usr/sbin/sendmail -bs',
'pretend' => false,
Controller Mail Method
//Send Mail
Mail::send('sendMail', array('key' => 'value'), function($message)
{
$message->to('EmailId#hotmail.com', 'Sender Name')->subject('Welcome!');
});
When I run the code it gives me following error message:
Swift_TransportException
Expected response code 220 but got code "", with message ""
I have created a SendMail.php file in view which contains some data.
How do I resolve this error message?
This problem can generally occur when you do not enable two step verification for the gmail account (which can be done here) you are using to send an email. So first, enable two step verification, you can find plenty of resources for enabling two step verification. After you enable it, then you have to create an app password. And use the app password in your .env file. When you are done with it, your .env file will look something like.
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=<<your email address>>
MAIL_PASSWORD=<<app password>>
MAIL_ENCRYPTION=tls
and your mail.php
<?php
return [
'driver' => env('MAIL_DRIVER', 'smtp'),
'host' => env('MAIL_HOST', 'smtp.gmail.com'),
'port' => env('MAIL_PORT', 587),
'from' => ['address' => '<<your email>>', 'name' => '<<any name>>'],
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),
'sendmail' => '/usr/sbin/sendmail -bs',
'pretend' => false,
];
After doing so, run php artisan config:cache and php artisan config:clear, then check, email should work.
In my case I had to set the
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=465 <<<<<<<------------------------- (FOCUS THIS)
MAIL_USERNAME=<<your email address>>
MAIL_PASSWORD=<<app password>>
MAIL_ENCRYPTION= ssl <<<<<<<------------------------- (FOCUS THIS)
to work it.. Might be useful.
Rest of the code was same as #Sid said.
And I think that editing both environment file and app/config/mail.php is unnecessary. Just use one method.
Edit as per the comment by #Zan
If you need to enable tls protection use following settings.
MAIL_PORT=587
MAIL_ENCRYPTION= tls
See here for some other gmail settings
What helped me... changing sendmail parameters from -bs to -t.
'sendmail' => '/your/sendmail/path -t',
if you are using Swift Mailer:
please ensure that your $transport variable is similar to the below,
based on tests i have done, that error results from ssl and port misconfiguration.
note: you must include 'ssl' or 'tls' in the transport variable.
EXAMPLE CODE:
// Create the Transport
$transport = (new Swift_SmtpTransport('smtp.gmail.com', 465, 'ssl'))
->setUsername(you#example.com)
->setPassword(password)
;
// Create the Mailer using your created Transport
$mailer = new Swift_Mailer($transport);
// Create a message
$message = (new Swift_Message('News Letter Subscription'))
->setFrom(['app#example.com' => 'A Name'])
->setTo(['someone#example.com' => 'A Name'])
->setBody('your message body')
;
// Send the message
$result = $mailer->send($message);
I was facing the same issue. After researching too much on Google and StackOverflow. I found a solution very simple
First, you need to set environment like this
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=example#gmail.com
MAIL_PASSWORD=example44
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=example#gmail.com
you have to change your Gmail address and password
Now you have to on less secure apps by following this link https://myaccount.google.com/lesssecureapps
For me the problem was the port. I first incorrectly used port 465, which works for SSL but not TLS. So the key thing was changing the port to 587.

Categories