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 :
- Mail::send('emails.welcome', ['user' => $user, 'message' => $message], function ($message) use ($user)
- {
- $message->from('info@expertphp.in', 'ExpertPHP.in');
- $message->to($user->email, $user->name)->subject('Welcome to ExpertPHP');
- });
Laravel Mailable class are stored in the app/Mail
directory.
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 :
- <?php
- namespace App\Mail;
- use Illuminate\Bus\Queueable;
- use Illuminate\Mail\Mailable;
- use Illuminate\Queue\SerializesModels;
- use Illuminate\Contracts\Queue\ShouldQueue;
- class Reminder 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('emails.welcome');
- }
- }
You can configure the sender by two way. First you can use from
method within this mailable class's build method like this :
- public function build()
- {
- return $this->from('info@expertphp.in')
- ->view('emails.welcome');
- }
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 ;
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.phpRoute::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.
- <?php
- namespace App\Http\Controllers;
- use Illuminate\Http\Request;
- use App\Http\Requests;
- use Mail;
- use App\Mail\Reminder;
- class UserController extends Controller
- {
- /**
- * Send Reminder E-mail Example
- *
- * @return void
- */
- public function welcomeMail()
- {
- $to_email = 'ajay.agrahari09@gmail.com';
- Mail::to($to_email)->send(new Reminder);
- return "E-mail has been sent Successfully";
- }
- }
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/.
- <h3>Hi,</h3>
- <p>Welcome to ExpertPHP.</p>