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('', '');
  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('')
  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 ;



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 :

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.

  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 = '';
  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/.

  1. <h3>Hi,</h3>
  2. <p>Welcome to ExpertPHP.</p>

Phone: (+91) 8800417876
Noida, 201301
ataşehir escort maltepe escort bostancı escort kadıköy escort maltepe escort ataşehir escort tuzla escort pendik escort ümraniye escort kartal escort
pendik escort
izmir escort izmir escort izmir escort izmir escort denizli escort antalya escort antalya escort antalya escort izmir escort izmir escort izmir escort izmir escort izmir escort
hatay escort nevsehir escort nigde escort kirikkale escort batman escort osmaniye escort kirklareli escort tokat escort rize escort adiyaman escort gumushane escort batman escort
porno izle
cialis satış cialis fiyat cialis 20 mg cialis 100 mg cialis 5 mg fiyat
sohbet okey oyna
ankara escort marmaris escort bursa escort mersin escort antalya escort izmir escort samsun escort bodrum escort çeşme escort kuşadası escort adana escort eskişehir escort gaziantep escort esenyurt escort pendik escort tuzla escort fatih escort ümraniye escort mecidiyeköy escort kartal escort bahçelievler escort bahçeşehir escort ataşehir escort bakırköy escort nişantaşı escort avcılar escort büyükçekmece escort kurtkoy escort konya escort beşiktaş escort taksim escort beylikdüzü escort malatya escort sakarya escort şişli escort maltepe escort kadıköy escort ataköy escort bostancı escort şirinevler escort bağcılar escort üsküdar escort etiler escort başaksehir escort ortaköy escort kağıthane escort sultangazi escort halkalı escort çekmeköy escort şaşkınbakkal escort mamak escort sincan escort keçiören escort çankaya escort beypazarı escort akyurt escort pursaklar escort kahramankazan escort göztepe escort alsancak escort bornova escort buca escort karabaglar escort karşıyaka escort konak escort narlıdere escort izmit escort başiskele escort kandira escort körfez escort karamürsel escort dilovası escort gölcük escort gebze escort kocaeli escort kayseri escort elazığ escort denizli escort diyarbakir escort adana escort ankara escort izmir escort antalya escort gaziantep escort kayseri escort konya escort mersin escort denizli escort kocaeli escort diyarbakır escort