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 protected]', '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('[email protected]', '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 question is related to
php
email
laravel
swiftmailer
I did as per sid saying my env after updating is
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=<mygmailaddress>
MAIL_PASSWORD=<gmailpassword>
MAIL_ENCRYPTION=tls
this did work without 2 step verification. with 2 step verification enabled it did not work for me.
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
[email protected]
MAIL_PASSWORD=example44
MAIL_ENCRYPTION=tls
[email protected]
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
That error message means that there was not response OR the server could not be connected.
The following settings worked on my end:
'stream' => [
'ssl' => [
'allow_self_signed' => true,
'verify_peer' => false,
'verify_peer_name' => false,
],
]
Note that my SMTP settings are:
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=465
MAIL_USERNAME=[full gmail address]
MAIL_PASSWORD=[App Password obtained after two step verification]
MAIL_ENCRYPTION=ssl
What helped me... changing sendmail parameters from -bs to -t.
'sendmail' => '/your/sendmail/path -t',
i was facing this problem and i checked all the answers and nothing worked for me, but then i reset mail.php and didn't touch it and set the mail server from .env file and it worked perfectly, hope this will save the time for someone :).
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
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([email protected])
->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(['[email protected]' => 'A Name'])
->setTo(['[email protected]' => 'A Name'])
->setBody('your message body')
;
// Send the message
$result = $mailer->send($message);
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.
Source: Stackoverflow.com