81

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?

random
  • 9,774
  • 10
  • 66
  • 83
dev90
  • 7,187
  • 15
  • 80
  • 153

7 Answers7

130

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.

Boern
  • 7,233
  • 5
  • 55
  • 86
Sid
  • 5,693
  • 3
  • 33
  • 50
  • 1
    I have looked everywhere and couldn't find the solution, but you totally saved my life!!. Thank you so much!!. I'm voting your answer up! – HenryDev May 03 '16 at 06:17
  • Nice! It's working now. I think that is the proper answer for this error. – Alexandre Thebaldi Nov 13 '16 at 15:51
  • I had the same issue with Yii2 and mailer. To me the key was to endorse the gmail account using the phone number. I didn't do everything suggested here, just following the yii2 documentation for mailer class implementation and this thing with phone number linking. – orouwk Oct 27 '17 at 21:13
  • stream_socket_enable_crypto(): SSL operation failed with code 1. OpenSSL Error messages:\nerror:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed – Ahmed Numaan Mar 07 '19 at 03:45
  • thanks so much! I thought just allowing less secure app access will make it work but just waste my time! – Ali Mar 30 '19 at 23:44
  • Thanks man for your perfect answer. You saved me. Upvoted your answer – Adi Feb 05 '20 at 11:04
35

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

Gayan Kavirathne
  • 2,909
  • 2
  • 18
  • 26
18

What helped me... changing sendmail parameters from -bs to -t.

'sendmail' => '/your/sendmail/path -t',
smoothie
  • 309
  • 2
  • 6
  • Thank you, that worked! How did you come to that idea? – adrian Mar 03 '20 at 13:46
  • You need to provide more information that where should we add this or change this – Sumit Kumar Gupta Mar 11 '20 at 19:16
  • 3
    From the Swiftmail docs: You can run the Sendmail Transport in two different modes specified by command line flags: "-bs" runs in SMTP mode so theoretically it will act like the SMTP Transport. "-t" runs in piped mode with no feedback, but theoretically faster, though not advised. You can think of the Sendmail Transport as a sort of asynchronous SMTP Transport -- though if you have problems with delivery failures you should try using the SMTP Transport instead. Swift Mailer isn't doing the work here, it's simply passing the work to somebody else (i.e. sendmail). – Frugan Jun 03 '21 at 09:04
  • 3
    If using 'sendmail -t' you will not be aware of any failures until they bounce (i.e. send() will always return 100% success). – Frugan Jun 03 '21 at 09:05
2

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);
codedbychavez
  • 193
  • 4
  • 6
2

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

Sumit Kumar Gupta
  • 2,132
  • 1
  • 22
  • 21
1

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.

Jan Żankowski
  • 8,690
  • 7
  • 38
  • 52
0

It worked for me by activating the less secure Application Access setting of my Google account and the following environment variable settings:

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME= your email
MAIL_PASSWORD= your password
MAIL_ENCRYPTION=tls