Posted On:

Posted In:

Emailing is crucial for a business to communicate with its clients. Many organizations send auto-generated emails through their websites to accomplish various tasks like authentication checking, password resetting, making clients aware of a company’s products or services inform a client about a new product launch, and other marketing campaigns. It became a prime requirement of businesses to add email functionality to their websites. 

It used to be difficult and time-consuming for developers to implement this function in their projects. Laravel has recognized potential problems that occur while implementation and offered some robust solutions to tackle those problems. 

There are many ways you can add emailing functionality to your project. Here are instructions for how to do it with SMTP protocol. 

Step 1:

First of all, you need to set the following environment variables in your .env file.



[email protected] 



[email protected] 


Step 2:

Run this command to create a mail class:

php artisan make:mail sendEmail 

This command will create a class in App/Mail folder. 

Now open sendEmail class and update the code with following code.


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

class sendEmail extends Mailable 

   use Queueable, SerializesModels; 
    * Create a new message instance. 
    * @return void 

   public $data; 
   public function __construct($data) { 
       $this->data = $data; 

    * Build the message. 
    * @return $this 

   public function build() { 
       return $this->view(‘email_template) 
           ->with([‘data’ => $this->data]); 

Step 3:

Create a controller file at app/Http/Controllers/MailController.php 


namespace App\Http\Controllers; 

use App\Http\Controllers\Controller; 

use App\Mail\sendEmail; 

use Illuminate\Support\Facades\Mail; 


class MailController extends Controller 


   public function send() { 

       $obj = [ 

           ‘message‘=> ‘message’, 


     Mail::to(“[email protected]”)->send(new sendEmail($obj)); 



Step 4:

Create the template view file email_template.blade.php 

<!DOCTYPE html> 






    <h3> {{$data[‘message’]}} </h3> 



Step 5:

Open the web.php file and add the following code to send emails.

Route::get(‘/sendemail/send’, ‘EmailController@send‘);

How to Send a mail

If you want to test this:

  1. copy the URL – /sendemail/send
  2. Open your browser
  3. Paste this URL in your browser’s URL bar
  4. Check the inbox of the mail address you’ve provided in the controller.

You will receive a test email 


Implementing an emailing functionality in your Laravel project is super easy. I hope this brief tutorial has provided you little help in your projects. For more such tutorials keep visiting our blog page.

If you are looking for a software firm to create your business website or mobile app, your search ends here. We build affordable and robust software solutions for businesses to digitally thrive on. Contact us to get a quote. You can mail us at [email protected] or WhatsApp us on +91 95 37 84 38 39.

Latest Posts

Four Easy Steps to Migrate from Drupal to WordPress

May 15, 2024

While talking about CMSs, Drupal is considered by many people. It is scalable, and secure but its technicality is a…

The Ultimate Checklist for Weebly to WooCommerce Migration

April 24, 2024

Weebly’s intuitive interface has empowered countless entrepreneurs to launch their online stores with ease. However, as your business flourishes and…

Leave a Reply

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