sending email laravel post two

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

Is WordPress VIP wort it unveiling the benefits for enterprises

Is WordPress VIP worth it? Unveiling the Benefits for Enterprises 

March 1, 2024

Your WordPress website is your digital storefront, bustling with potential customers and brimming with valuable content. But as your business flourishes, attracting more visitors, the limitations of standard hosting might become apparent. Slow loading times,…

Wix to WordPress Migration Guide

How to Migrate from Wix to WordPress? Detailed Guide.

February 27, 2024

How likely are you to drop out of the website if its layout is not happening or if it is not user-friendly? You might experience the same if your Wix website is failing to maintain…

Leave a Reply

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