AppDividend
Latest Code Tutorials

How To Send Email In Laravel Tutorial

9,995

Get real time updates directly on you device, subscribe now.

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.

Host:	smtp.mailtrap.io
Port:	25 or 465 or 2525
Username:	// some username
Password:	// some password
Auth:	PLAIN, LOGIN and CRAM-MD5
TLS:	Optional

We have to use these details inside the .env file.

MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME= // some username
MAIL_PASSWORD= // some password
MAIL_ENCRYPTION=null

If you are using Gmail or your company email address, then please replace your respected credentials here.

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 App\Mail\SendMailable.php. Now, this class contains one property, and that is a name which we need to pass when we build this class’s instance.

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;

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('emails.name');
    }
}

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.

<div>
    Hi, This is : {{ $name }}
</div>

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 Illuminate\Support\Facades\Mail;
use App\Mail\SendMailable;

public function mail()
{
   $name = 'Krunal';
   Mail::to('krunal@appdividend.com')->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.

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.

5 Comments
  1. TCSET says

    Thanks, Kelsey!

  2. Umair says

    Thanks, A lot man you made my day.

  3. oscam says

    bonjour
    Good share.
    In my opinion the great cccam are those from Fishbone IPTV cloud
    I would like to see more posts like this
    Thanks

  4. Ajay Malhotra says

    Useful Post and working

  5. Reema says

    Hi..
    How did u made the controller that step is not shown.
    Because I am getting error in controller as “/mail not found”.
    Please suggest.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.