LaravelLaravel & VueJsVueJs

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.

★ READ ALSO ★  The Cost Of JavaScript - Freek Van der Herten's blog on PHP and Laravel

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 AppMailSendMailable.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 AppMail;

use IlluminateBusQueueable;
use IlluminateMailMailable;
use IlluminateQueueSerializesModels;
use IlluminateContractsQueueShouldQueue;

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 IlluminateSupportFacadesMail;
use AppMailSendMailable;

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.

★ READ ALSO ★  LaraClassified - Geo Classified Ads CMS

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.

Source link

Leave a Reply

Your email address will not be published. Required fields are marked *