10 min read

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.

MAIL_MAILER=smtp 

MAIL_HOST=smtp.mailtrap.io 

MAIL_PORT=2525 

MAIL_USERNAME=sample@example.com 

MAIL_PASSWORD=************ 

MAIL_ENCRYPTION=ssl 

MAIL_FROM_ADDRESS=sample@yourdomain.com 

MAIL_FROM_NAME=”${APP_NAME}” 

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.

<?php 

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) 
           ->subject(subject’) 
           ->with([‘data’ => $this->data]); 
   } 
} 

Step 3:

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

<?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(“receiver@email.com”)->send(new sendEmail($obj)); 

   } 

} 

Step 4:

Create the template view file email_template.blade.php 

<!DOCTYPE html> 

<html> 

<head> 

    <title>Title</title> 

</head> 

<body> 

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

</body> 

</html> 

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 

Conclusion

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 info@qrolic.com or WhatsApp us on +91 95 37 84 38 39.

"Have WordPress project in mind?

Explore our work and and get in touch to make it happen!"