Laravel 9 Mail | Laravel 9 Send Email Tutorial (ok)
https://www.itsolutionstuff.com/post/laravel-9-mail-laravel-9-send-email-tutorialexample.html
C:\xampp\htdocs\test\routes\web.php
<?php
use App\Http\Controllers\MailController;
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return view('welcome');
});
Route::get('send-mail', [MailController::class, 'index']);C:\xampp\htdocs\test\.env
APP_NAME=Laravel
APP_ENV=local
APP_KEY=base64:64GiWeIqzBVPSIepXOMiB+B8pHXAFE4TS+BuUDAlkvo=
APP_DEBUG=true
APP_URL=http://localhost/test
LOG_CHANNEL=stack
LOG_DEPRECATIONS_CHANNEL=null
LOG_LEVEL=debug
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=test
DB_USERNAME=root
DB_PASSWORD=
BROADCAST_DRIVER=log
CACHE_DRIVER=file
FILESYSTEM_DRIVER=local
QUEUE_CONNECTION=sync
SESSION_DRIVER=database
SESSION_LIFETIME=120
MEMCACHED_HOST=127.0.0.1
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379
MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=phamngoctuong1805@gmail.com
MAIL_PASSWORD=lufcvcbdnbjcrvta
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=phamngoctuong1805@gmail.com
MAIL_FROM_NAME="${APP_NAME}"
AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_DEFAULT_REGION=us-east-1
AWS_BUCKET=
AWS_USE_PATH_STYLE_ENDPOINT=false
PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_APP_CLUSTER=mt1
MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"C:\xampp\htdocs\test\resources\views\emails\demoMail.blade.php
C:\xampp\htdocs\test\app\Mail\DemoMail.php
C:\xampp\htdocs\test\app\Http\Controllers\MailController.php
Kết quả:


Hi Dev,
In this quick example, let's see laravel 9 send email example. if you want to see an example of laravel 9 send mail example then you are the right place. This post will give you a simple example of sending an email in laravel 9 smtp. if you have a question about laravel 9 send mail smtp example then I will give a simple example with a solution.
Laravel 9 provides an inbuilt mail configuration for sending emails. you can use several drivers for sending email in laravel 9. you can use smtp, Mailgun, Postmark, Amazon SES, and send email. you have to configure on the env file what driver you want to use.
In this tutorial, I will give you step by step instructions to send emails in laravel 9. you can create blade file design and also with dynamic information for mail layout. so let's see step by step guide and send an email to your requirement.

Step 1: Install Laravel 9
This step is not required; however, if you have not created the laravel app, then you may go ahead and execute the below command:
Step 2: Make Configuration
In first step, you have to add send mail configuration with mail driver, mail host, mail port, mail username, mail password so laravel 9 will use those sender configuration for sending email. So you can simply add as like following.
.env
Read Also: How to Send Mail in Laravel 8 using Mailtrap?
Step 3: Create Mail Class
In this step we will create mail class DemoMail for email sending. Here we will write code for which view will call and object of user. So let's run bellow command.
now, let's update code on DemoMail.php file as bellow:
app/Mail/DemoMail.php
Step 4: Create Controller
In this step, we will create MailController with index() method where we write code for sending mail to given email address. so first let's create controller by following command and update code on it.
Now, update code on MailController file.
app/Http/Controllers/MailController.php
Step 5: Create Routes
In this step, we need to create routes for list of sending email. so open your "routes/web.php" file and add following route.
routes/web.php
Step 6: Create Blade View
In this step, we will create blade view file and write email that we want to send. now we just write some dummy text. create bellow files on "emails" folder.
resources/views/emails/demoMail.blade.php
Run Laravel App:
All the required steps have been done, now you have to type the given below command and hit enter to run the Laravel app:
Now, Go to your web browser, type the given URL and view the app output:
Read Also: Laravel 9 Import Export Excel and CSV File Tutorial
Output:

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

