Latest Code Tutorials

How To Send Email In Laravel Tutorial


How To Send Email In Laravel Tutorial is the leading topic. A new feature from Laravel 5.3 is a way to simplify sending an email by creating a “mailable” classes that handle setting up your emails. So we will first define the email configuration inside a .env file and then create a mailable class. While building that mailable class’s instance, we pass the data that needs to be displayed in the mail and then we are good to go. Laravel provides a clean, simple API over the great SwiftMailer library with drivers for SMTP, Mailgun, SparkPost, Amazon SES, PHP’s mail function, and sendemail, allowing you to quickly get started sending mail through a local or cloud-based service of your choice.

Send Email In Laravel 5.6

First, we will start this example by downloading the latest Laravel which is 5.6 for this post.

Step 1: Install Laravel 5.6

composer create-project laravel/laravel emailsend --prefer-dist

After that, we need to define the Email configuration inside the .env file.

Step 2: Email Configuration.

For this example, I am using mailtrapIt is kind of application, which you can use to test your emails. After you will signup, you can see that it provides us following kinds of details.

Port:	25 or 465 or 2525
Username:	// some username
Password:	// some password
TLS:	Optional

We have to use these details inside the .env file.

MAIL_USERNAME= // some username
MAIL_PASSWORD= // some password

If you are using Gmail or your company email address, then please replace your respected credentials here.

Step 3: Create Mailable class.

Go to a terminal and type the following command.

php artisan make:mail SendMailable

So, it will create this file inside App\Mail\SendMailable.php. Now, this class contains one property, and that is a name which we need to pass when we build this class’s instance.


namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;

class SendMailable extends Mailable
    use Queueable, SerializesModels;
    public $name;

     * Create a new message instance.
     * @return void
    public function __construct($name)
        $this->name = $name;

     * Build the message.
     * @return $this
    public function build()
        return $this->view('');

Here in this class, I have used the parameterized constructor. So we have $name property in our class. Now, we need to create a blade file inside views folder. We can use that $name property as a data of the view name.blade.php file. Now, first form that view.

Step 4: Create a view file.

Inside views folder, it is better to create a new folder called email and in that folder, make one file called name.blade.php.

    Hi, This is : {{ $name }}

Step 5: Define the route to send an email.

Inside routes  >>  web.php file, we need to define the route to send an email.

Route::get('/send/email', 'HomeController@mail');

Now, we need to write the code inside mail function to send an email.

// HomeController.php

use Illuminate\Support\Facades\Mail;
use App\Mail\SendMailable;

public function mail()
   $name = 'Krunal';
   Mail::to('')->send(new SendMailable($name));
   return 'Email was sent';

We have included Mailable class and Mail facade. For this example, we have hardcode the name value, but in a real-time example, we have dynamic data to generate an email.

Step 6: Start the server.

Go to your terminal and hit the following command.

php artisan serve

Go to this URL: http://localhost:8000/send/email

If your all the configurations are right then you can see the response like “Email was sent.” So this is how you can send the email. So our How To Send Email In Laravel Tutorial is over.

  1. TCSET says

    Thanks, Kelsey!

  2. Umair says

    Thanks, A lot man you made my day.

  3. oscam says

    Good share.
    In my opinion the great cccam are those from Fishbone IPTV cloud
    I would like to see more posts like this

  4. Ajay Malhotra says

    Useful Post and working

  5. Reema says

    How did u made the controller that step is not shown.
    Because I am getting error in controller as “/mail not found”.
    Please suggest.

    1. Reza Kazemifar says

      php artisan make:controller HomeController

  6. swati belure says

    i am getting this error
    View [emails.demo] not found.

    1. srinath says

      make sure that your file saved in ( resources / views /emails / demo.blade.php )

  7. Ahmed Shahzad says

    Good post nice work

  8. youjan says

    how to put header in the mail? I have tried to send the mail with image of base64 encoded but in gmail the image get the raw html code. Could you please help me to solve this issue?

  9. Ved Prakash N says

    i have given correct credientials .. but then too am getting this error:
    Expected response code 220 but got an empty response

    1. John Nnanna says

      Hello @Ved, I was having same issue; I changed my Port in the .env to 25 and that fixed it

  10. UsbBush says


  11. Shafeena says

    This is not working,how can I check this works or not,and I can’t get the mail

  12. utpal says

    Expected response code 250 but got code “530”, with message “530 5.7.1 Authentication required

  13. Sora Dale says

    Thank you so much! Worked wonders!
    If you encountered and error like “Expected response code 250 but got code “530”, with message “530 5.7.1 Authentication required”
    just go to .env and change the port to 25
    also make sure to restart php artisan serve.

  14. Bouchra says

    It shows ‘Email was sent’, i checked my address email, an there is no mail received.
    Thank you

  15. Bush_AS says

    It shows ‘Email was sent’, i checked my address email, an there is no mail received.
    Thank you

  16. kartik rawal says

    seriously awesome post thanks for this, You solve every query for me. I am a beginner but by your solution i made it, Thanks

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.