In this article, we will learn how we can send emails using Laravel. Laravel provides a clean, simple email API for email. With Laravel’s email drivers, you can easily send mail through SMTP, Mailgun, Postmark, and Amazon SES.
Prerequisites
- A basic understanding of Laravel
How to send emails using Laravel 9?
First, let’s create a Laravel 9 project using the following command.
composer create-project laravel/laravel:^9.0 laravel-email
After executing the above command Laravel will create the project like below.
Now, let’s configure SMTP details in a .env file like below so that we can send emails using our Gmail account.
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=user_name // Enter your gmail
MAIL_PASSWORD=password // Enter your gmail password
MAIL_ENCRYPTION=tls
Here we are using Gmail for sending mail so we have to allow non-secure apps to access Gmail we can do this by going to your Gmail settings here.
Let’s create a route using with we will be able to send mail. For that add the following code to the web.php file located inside the routes folder.
use App\Http\Controllers\SendEmailController;
Route::get('send-email', [SendEmailController::class, 'index']);
Now, let’s create a controller named SendEmailController
using the following command.
php artisan make:controller SendEmailController
After creating the controller, update the controller file with the following code.
<?php
namespace App\Http\Controllers;
use App\Mail\SendMail;
use Illuminate\Support\Facades\Mail;
class SendEmailController extends Controller
{
public function index()
{
Mail::to('email-address')->send(new SendMail());
if (Mail::failures()) {
return response()->Fail('Sorry! Please try again latter');
} else {
return response()->success('Great! Successfully send in your mail');
}
}
}
Then, use the below command to create SendMail mailable class using which mail will be sent.
php artisan make:mail SendMail
After successfully executing the above command SendEmail.php file will be created inside the app/Mail folder. Replace the SendMail file with the following code.
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
class SendMail extends Mailable
{
use Queueable, SerializesModels;
/**
* Create a new message instance.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
$attachmentLocation = public_path('attachments/Laravel.jpeg'); //your attachment location
return $this->view('emails.testMail') ->attach($attachmentLocation);
}
}
Now let’s create a view file that will contain our mail template. The location of the file named testMail.blade.php
will be inside the resources>views>emails folder. After creating the file add the following code.
<!DOCTYPE html>
<html>
<head>
<title>Send email using Laravel 9</title>
</head>
<body>
<h1>This is a test email send from scanskill.</h1>
</body>
</html>
After completing the above process execute the server locally using the following command.
php artisan serve
Then execute the following URL in your web browser.
http://127.0.0.1:8000/send-email
Conclusion
In this article, we have learned how to send emails using Laravel 9 with the help of the Gmail account. Similarly, we can send emails using other different mail provider services. If you need to perform any operation CRUD operation before sending an email you can get brief ideas about performing CRUD operations here.