How to Send Emails in Laravel 9: A Step-by-Step Guide

In this guide, we'll walk through the process of sending emails in Laravel 9 using the SMTP protocol. By following these steps, you’ll learn how to set up email configurations, create a mail class, and send emails using a controller.

Step 1: Install Laravel 9

If you haven't created a Laravel application yet, you can easily do so by executing the following command:

composer create-project laravel/laravel example-app

Step 2: Configure Mail Settings

To send emails, you need to configure your mail settings in the .env file. Add the following lines to set up your SMTP configuration:


MAIL_MAILER=smtp MAIL_HOST=smtp.gmail.com MAIL_PORT=465 MAIL_USERNAME=mygoogle@gmail.com MAIL_PASSWORD=your_gmail_app_password MAIL_ENCRYPTION=tls MAIL_FROM_ADDRESS=mygoogle@gmail.com MAIL_FROM_NAME="${APP_NAME}"

Make sure to replace your_gmail_app_password with the actual app password generated for your Google account.

Step 3: Create a Mail Class

Next, we’ll create a mail class called DemoMail. This class will handle the email content and recipient information. Run the following Artisan command:


php artisan make:mail DemoMail

Then, update the app/Mail/DemoMail.php file as follows:


<?php namespace App\Mail; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Mail\Mailable; use Illuminate\Queue\SerializesModels; class DemoMail extends Mailable { use Queueable, SerializesModels; public $mailData; /** * Create a new message instance. * * @param array $mailData * @return void */ public function __construct($mailData) { $this->mailData = $mailData; } /** * Build the message. * * @return $this */ public function build() { return $this->subject('Mail from ItSolutionStuff.com') ->view('emails.demoMail'); // Specify the email view } }

Step 4: Create a Controller

Now, let’s create a controller named MailController with an index() method to send the email. Execute the command below:


php artisan make:controller MailController

Update the app/Http/Controllers/MailController.php file with the following code:


<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Mail; use App\Mail\DemoMail; class MailController extends Controller { /** * Send email using the DemoMail class. * * @return \Illuminate\Http\Response */ public function index() { $mailData = [ 'title' => 'Mail from ItSolutionStuff.com', 'body' => 'This is for testing email using SMTP.' ]; Mail::to('your_email@gmail.com')->send(new DemoMail($mailData)); return response()->json(['message' => 'Email sent successfully.']); } }

Step 5: Define Routes

Next, you need to define a route for sending emails. Open the routes/web.php file and add the following route:

<?php use Illuminate\Support\Facades\Route; use App\Http\Controllers\MailController; Route::get('send-mail', [MailController::class, 'index']);

Step 6: Create a Blade View

Finally, create a Blade view for the email content. Create a new file in the resources/views/emails directory named demoMail.blade.php and add the following HTML:

html
<!DOCTYPE html> <html> <head> <title>Email from ItSolutionStuff.com</title> </head> <body> <h1>{{ $mailData['title'] }}</h1> <p>{{ $mailData['body'] }}</p> <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p> <p>Thank you!</p> </body> </html>

Run Your Laravel Application

With all the steps completed, you can run your Laravel application using the following command:

php artisan serve

Visit http://localhost:8000/send-mail in your browser to trigger the email sending functionality.

Post a Comment

0 Comments