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
[email protected]
MAIL_PASSWORD=************
MAIL_ENCRYPTION=ssl
[email protected]
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(“[email protected]”)->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:
- copy the URL – /sendemail/send
- Open your browser
- Paste this URL in your browser’s URL bar
- 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 [email protected] or WhatsApp us on +91 95 37 84 38 39.