How to Send Automatic Email in Laravel 9?
https://www.itsolutionstuff.com/post/how-to-send-automatic-email-in-laravel-9example.html
How to Send Automatic Email in Laravel 9?
By Hardik Savani May 24, 2022 Category : LaravelPauseUnmuteLoaded: 3.34%Fullscreen
Hi Dev,
This article goes in detailed on send automatic email laravel 9. I would like to share with you laravel 9 auto send email. you'll learn auto send email laravel 9. We will use laravel 9 send scheduled emails. Let's get started with laravel 9 send mail automatically.
In this example, we will create an auto send an email for a birthday wish. so, we will create a "users" table with a "birthdate" column. Then we will create one seeder to add some users. Then after we will create one command to send an email that the birthdate match today's date. after creating the command successfully we will be scheduled daily. so users will get automatically their birthday on email.
So, let's follow below step to make it done this example:
Step 1: Install Laravel
This step is not required; however, if you have not created the laravel app, then you may go ahead and execute the below command:
composer create-project laravel/laravel example-appStep 2: Create Migration
here, we will create new migration for adding new column "birthdate" in users table. so let's run bellow command:
php artisan make:migration add_birthdate_columnAfter this command you will find one file in following path "database/migrations" and you have to put bellow code in your migration file for create products table.
<?php use Illuminate\Database\Migrations\Migration;use Illuminate\Database\Schema\Blueprint;use Illuminate\Support\Facades\Schema; return new class extends Migration{ /** * Run the migrations. * * @return void */ public function up() { Schema::table('users', function (Blueprint $table) { $table->date('birthdate')->nullable(); }); } /** * Reverse the migrations. * * @return void */ public function down() { }};Now you have to run this migration by following command:
php artisan migrateRead Also: Laravel 9 Authentication using Breeze Tutorial
Step 3: Update Model
In this step, we will add "birthdate" column in $fillable array.
let's copy below code and paste it.
app/Models/User.php
Step 4: Create Seeder
In this step we will create Seeder for adding dummy users. So let's run bellow command.
now, let's update code on UserSeeder.php file as bellow:
database/seeders/UserSeeder.php
Then run below seeder to create dummy users.
You will find following users as below in mysql table:

Step 5: Create Mail Class
In this step we will create mail class BirthDayWish for email send on match date of birth. So let's run bellow command.
now, let's update code on BirthDayWish.php file as bellow:
app/Mail/BirthDayWish.php
Here, we will create blade view file for email. In this file we will write text for birthday wish.. create bellow files on "emails" folder.
resources/views/emails/birthdayWish.blade.php
next, you have to add send mail configuration with mail driver, mail host, mail port, mail username, mail password so laravel will use those sender configuration for sending email. So you can simply add as like following.
.env
Step 6: Create Command
In this step, we will create new command call AutoBirthDayWish. let's create command using below terminal command:
now, let's update code on AutoBirthDayWish.php file as bellow:
app/Console/Commands/AutoBirthDayWish.php
Next, we need to scheduled this command daily. so let's do it in Kernel.php file:
app/Console/Kernel.php
Step 7: Cron Job Setup on Server
Here, i will show you how to setup cron job command on server. you need to install crontab on server. if you are using ubuntu server then it already installed. so let's run bellow command and add new entry for cron job.
Now, add bellow line to crontab file. make sure you need to set your project path correctly on it.
Read Also: How to Install Tailwind CSS in Laravel 9?
Now, you can check on server as well.
Now, you will get email birthday wish on match. you will receive email as like below:

I hope it can help you...
Last updated
Was this helpful?
