How to solve expected response code 220 but got code with message in Laravel

0 votes

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?

Mar 31, 2020 in Laravel by kartik
• 37,510 points
41,608 views

2 answers to this question.

0 votes

Hii @kartik,

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);
answered Mar 31, 2020 by Niroj
• 82,880 points
Hii..,I am new to Laravel can you explain to me In which file we need to write this code?

Hello @Vidya Kore,

For the purpose of  sending Email In Laravel Through Email Sending  Service Provider you have to  installed Laravel application on a web server.

First, you need to create a mailable class with any name.

php artisan make:mail config

Once you execute the above-mentioned command, your mail class will be created with the name of config. So, it will create this file inside App\Mail\config.php.

Next, you have to find it in the Mail directory in the app/Mail. You have a template, which contains basic functions so you can modify that template with the above metioned code.

You can follow this doc for installation of laravel.

You can also refer Laravel Tutorial For Beginners to get started.

Hope this is helpfull!!
Thank you!!

hi thanks for your reply can you please help me with my code

here is my code :

.env code:

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=465
MAIL_USERNAME=chvid3122@gmail.com
MAIL_PASSWORD=......
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=chvid3122@gmail.com
MAIL_FROM_NAME=vidyadatta

I have created mailable class here named as testmail

testmail.php

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;

use Illuminate\Contracts\Queue\ShouldQueue;

use Illuminate\Mail\Mailable;

use Illuminate\Queue\SerializesModels;

class testemail extends Mailable

{

    use Queueable, SerializesModels;

    /**

     * Create a new message instance.

     *

     * @return void

     */

     public $name = '';

    public function __construct($name)

    {

       $this->name = $name;

    }

    /**

     * Build the message.

     *

     * @return $this

     */

    public function build()

    {

        return $this->view('mail');

    }

}

MailController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Mail\testemail;

use Mail;

class MailController extends Controller

{

    public function sendemail()

    {

        $name = "vidya kore";

        Mail::to(['vidyakore3108@gmail.com'])->send(new testemail($name));

        echo "send email using gmail smtp";

    }

}

mail.blade.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Mail\testemail;

use Mail;

class MailController extends Controller

{

    public function sendemail()

    {

        $name = "vidya kore";

        Mail::to(['vidyakore3108@gmail.com'])->send(new testemail($name));

        echo "send email using gmail smtp";

    }

}

this is giving me an error as :

Swift_TransportException

Expected response code 220 but got an empty response

Hello @vidya,

This error 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=667
MAIL_USERNAME=chvid3122@gmail.com
MAIL_PASSWORD=edureka123
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', 667),
    '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.

Hope it helps!!
Thank you!!