How to send mail using mailable in Laravel 5.3 with example

How to send mail using mailable in Laravel 5.3 with example

How to send mail using mailable in laravel 5.3 with example

In my previous post, i list down the new features about Laravel 5.3 and now in this tutorial i am going to tell you about Mailable one of new features added with Laravel 5.3

I here let you know about mailable class and all of its configuration is done in the build method and within this method, you can call several methods such as from(), subject(), view(), attach().

In previous version of Laravel 5.3, you were sending emails like this :

  1. Mail::send('emails.welcome', ['user' => $user, 'message' => $message], function ($message) use ($user)
  2. {
  3. $message->from('info@expertphp.in', 'ExpertPHP.in');
  4. $message->to($user->email, $user->name)->subject('Welcome to ExpertPHP');
  5. });

Laravel Mailable class are stored in the app/Mail directory.

Step 1: Generate Mailable Class

In this step we will generate a mailable class for reminder using artisan command.

Mailable classes are responsible for handling data and passing them to views.

php artisan make:mail Reminder

After running above command, you will see a file Reminder.php in following directory app/Mail.

Now open this file and put following line of code :

  1. <?php
  2. namespace App\Mail;
  3. use Illuminate\Bus\Queueable;
  4. use Illuminate\Mail\Mailable;
  5. use Illuminate\Queue\SerializesModels;
  6. use Illuminate\Contracts\Queue\ShouldQueue;
  7. class Reminder extends Mailable
  8. {
  9. use Queueable, SerializesModels;
  10. /**
  11. * Create a new message instance.
  12. *
  13. * @return void
  14. */
  15. public function __construct()
  16. {
  17. //
  18. }
  19. /**
  20. * Build the message.
  21. *
  22. * @return $this
  23. */
  24. public function build()
  25. {
  26. return $this->view('emails.welcome');
  27. }
  28. }

You can configure the sender by two way. First you can use from method within this mailable class's build method like this :

  1. public function build()
  2. {
  3. return $this->from('info@expertphp.in')
  4. ->view('emails.welcome');
  5. }
Step 2: Mailer Configuration

To send emails, you should first configure your services.

I am going to use gmail service to send emails so i have to add my gmail username and password which is smtp configured.

You can configured these details directly in config/mail.php file or .env file.

I suggest you to add your credentials in .env file so open your .env file and your following details ;

.env

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=username@gmail.com
MAIL_PASSWORD=your-gmail-password
MAIL_ENCRYPTION=tls

Step 3: Route

In this step, we will add route to send test mail to make sure it working fine or not. so open your web route file and add following route :

routes/web.php
Route::get('welcome-mail','UserController@welcomeMail');
Step 4: Create UserController.php

In this step we will create a controller file with welcomeMail method where we will write code to send test emails.

SO create a UserController.php in following directory app/Http/Controllers/ and add following line of code.

app/Http/Controllers/UserController.php
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use App\Http\Requests;
  5. use Mail;
  6. use App\Mail\Reminder;
  7. class UserController extends Controller
  8. {
  9. /**
  10. * Send Reminder E-mail Example
  11. *
  12. * @return void
  13. */
  14. public function welcomeMail()
  15. {
  16. $to_email = 'ajay.agrahari09@gmail.com';
  17. Mail::to($to_email)->send(new Reminder);
  18. return "E-mail has been sent Successfully";
  19. }
  20. }
Step 5: Create Mail Template

Now I will create a view file to send email text. First create a emails directory and within this directory create a welcome.blade.php file in following path resources/views/emails/.

resources/views/emails/welcome.blade.php
  1. <h3>Hi,</h3>
  2. <p>Welcome to ExpertPHP.</p>

Phone: (+91) 8800417876
Noida, 201301