Send email with laravel Mailable using SMTP driver

By Author | May 04, 2019 | | 849 Views | Category : Laravel

Laravel utilises SwiftMailer library for handling mails using various drivers like SMTP, Mailgun, Amazon SES and sendmail. In this tutorial we will learn how to send mails by this default laravel mechanism  and is also very efficient. In laravel each mail se represented by a “mailable” class which is stored in “app/Mail” folder. These folder and classes are immediately created when we hit the command given in Step-2 to create new instance of mailable class.

All the configurations for this class is in build method. Some of the commonly used methods are from, subject, view, attach. A simple example of how we can use laravel method chaining in mailables class is given below.

return $this->from('')


To start with our example first thing is to create a new project or if you have existing project you can skip the step. Create new instance of laravel by using below command in you cmd / terminal.

laravel new email-example



Step 1: Configuration in env file

Now in .env file which is placed in root folder we can find some preset configurations related to MAIL which we can edit as below parameters or if somehow you are not able to find these configurations in you .env file, simple add these below lines and give your values in them.

In this example we are using gmail server to send mail. If you are not sure how to set parameter then you can check link for article.






We can also directly change in config/mail.php file which ultimately takes the variable values from .env file. But more preferred way is by giving you configuration values from env file.


Step 2: Create Mailable class

Now we will create a new instance of Mailable class by following command in cmd / terminal .

php artisan make:mail TestMail
Blogs-Air:email-example bloggingavenue$ php artisan make:mail Testmail

Mail created successfully.

This command will create the TestMail class in app/mail directory.

In build method we can use methods like from, subject, view etc.

namespace App\Mail;

use Illuminate\Bus\Queueable;

use Illuminate\Mail\Mailable;

use Illuminate\Queue\SerializesModels;

use Illuminate\Contracts\Queue\ShouldQueue;

class Testmail extends Mailable


    use Queueable, SerializesModels;


     * Create a new message instance.


     * @return void


    public function __construct()





     * Build the message.


     * @return $this


    public function build()


        return $this->view('email.testMail');




Step 3: Register route

In this step we are registering our new route which we can hit in order to call our mail controller



Step 4: Create controller

php artisan make:controller MailController

In this step we can create our mail controller which will respond to our newly created route.

namespace App\Http\Controllers;

use App\Mail\Testmail;

use Illuminate\Http\Request;

use Illuminate\Support\Facades\Mail;

class MailController extends Controller


    public function mailing()


    $myEmail = '';

    Mail::to($myEmail)->send(new TestMail());

return "Mail Send Successfully";




Step 5: View file in Mail body

This is the view class which is referenced in mailable class to send data in mail body.


Congratulations for your fist email.

Thank you...

Hope this post is helpful for you.



Tags :