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,520 points
42,263 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!!

0 votes

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.

answered Dec 16, 2020 by Gitika
• 65,890 points

Related Questions In Laravel

0 votes
1 answer

How to pass CSRF token with ajax request in Laravel?

Hey, In between head, tag put <meta name="csrf-token" ...READ MORE

answered Mar 24, 2020 in Laravel by Dey
39,686 views
0 votes
1 answer

How to give custom field name in laravel form validation error message?

Hello @kartik, You can specify custom error message ...READ MORE

answered Sep 30, 2020 in Laravel by Niroj
• 82,880 points
4,832 views
0 votes
1 answer

How to use withErrors with Exception error messages in Laravel?

Hello @kartik, Try this: return Redirect::to('admin/users/create') ...READ MORE

answered Oct 28, 2020 in Laravel by Niroj
• 82,880 points
4,812 views
0 votes
1 answer
+1 vote
1 answer

How to make anchor tag with routing using Laravel?

Hey @kartik, First you have to go to ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,880 points
22,937 views
0 votes
1 answer

What is redirection in Laravel?

Named route is used to give specific ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,880 points
2,959 views
0 votes
1 answer

How to install Laravel via composer?

Hello, This is simple you just need to ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,880 points
2,894 views
+1 vote
1 answer

What are named routes in Laravel and How can specify route names for controller actions?

Hey @kartik, Named routing is another amazing feature of ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,880 points
44,358 views
0 votes
1 answer

Error:Swift_TransportException Expected response code 220 but got code "", with message ""

Hello @kartik, This problem can generally occur when ...READ MORE

answered Jul 30, 2020 in Laravel by Niroj
• 82,880 points
4,330 views
0 votes
1 answer

Error:login in Laravel. How to solve?

Hey, First check if your model login has a field password in ...READ MORE

answered Mar 26, 2020 in Laravel by Niroj
• 82,880 points
7,677 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP