Skip to content
  • About Us
  • Contact Us
  • Privacy Policy
  • Terms & Conditions
  • Site Map

XpertPhp

Expertphp Is The Best Tutorial For Beginners

  • Home
  • Javascript
    • Jquery
    • React JS
    • Angularjs
    • Angular
    • Nodejs
  • Codeigniter
  • Laravel
  • Contact Us
  • About Us
  • Live Demos

Home » Laravel » How To Send Email Using Mailtrap In Laravel 9

How To Send Email Using Mailtrap In Laravel 9

How To Send Email Using Mailtrap In Laravel 9

Posted on March 4, 2022August 7, 2022 By XpertPhp

Welcome to you In this article. Today, we will discuss you how to Send Email using mailtrap in laravel 9. we are using SMTP to send email in this article. laravel provides a mailtrap package library so we can use that and easily send the mail.

Read Also: Laravel 9 Send Email Example

so you can see our laravel 9 email sending tutorial.

Overview

Step 1: Install Laravel 9

Step 2: Create Mailtrap Account

Step 3: Email Configuration

Step 4: Create a Mailable class

Step 5: Create Blade Files

Step 6: Create Route in web.php file

Step 7: Run Our Laravel Application

Step 1: Install Laravel 9

We are going to install laravel 9, so first open the command prompt or terminal and go to xampp htdocs folder directory using the command prompt. after then run the below command.

PHP
1
composer create-project --prefer-dist laravel/laravel laravel9_sendmail

Step 2: Create Nailtrap Account
In this step, you go to the mailtrap.io official website and create the account and get user id and user email.

Step 3: Email Configuration

After the complete installation of laravel. we have to mail configuration. now we will open the .env file and define the mail_driver, mail_host, mail_port, mail_username, mail_password and mail_encryption in the .env file. See below changes in a .env file.

PHP
1
2
3
4
5
6
MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=587
MAIL_USERNAME= // Enter your username
MAIL_PASSWORD= // Enter your password
MAIL_ENCRYPTION=tls

Step 4: Create a Mailable class

We will create a mail class using the below command. Now, if you want to pass the object for the view then you can write and pass the object in this file. so here we pass the student_detail in the below file.
.

PHP
1
php artisan make:mail MySendMail

MySendMail.php

PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<?php
 
namespace App\Mail;
 
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
 
class MySendMail extends Mailable
{
    use Queueable, SerializesModels;
    public $student_detail;
 
    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct($student_detail)
    {
        $this->student_detail = $student_detail;
    }
 
    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->view('emails.mySendMail');
    }
}
?>

 

Step 5: Create Blade Files

First, we will create the new directory “resources/views/emails” and that directory in creating a “resources/views/emails/mySendMail.blade.php” file.

mySendMail.blade.php

PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<!DOCTYPE html>
<html>
<head>
    <title>Laravel 9 Send Email Example - XpertPhp</title>
</head>
<body>
    <div>
<p>{{ $student_detail['first_name'] }}</p>
<p>{{ $student_detail['last_name'] }}</p>
<p>{{ $student_detail['address'] }}</p>
<p>Thank you</p>
    </div>
</body>
</html>

Step 6: Create Route in web.php file

We have to need put the below route in the routes/web.php file.

PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?php
use App\Http\Controllers\StudentController;
 
/*
|--------------------------------------------------------------------------
| 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/email',[StudentController::class, 'mail']);
?>

StudentController.php

PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Mail;
use App\Mail\MySendMail;
 
class StudentController extends Controller
{
    public function mail()
{
$student_detail = [
        'first_name' => 'test',
        'last_name' => 'xyz'
        'address' => 'test xyz'
];
   Mail::to('[email protected]')->send(new MySendMail($student_detail));
  
   return 'Email has been sent';
}
}
?>

Step 7: Run Our Laravel Application
We can start the server and run this example using the below command.

1
php artisan serve

Now we will run our example using the below Url in the browser.

1
http://127.0.0.1:8000/student

Please follow and like us:
error
fb-share-icon
Tweet
fb-share-icon

Recommended Posts:

  • laravel eloquent update query example
  • Laravel 9 CRUD Operation With Ajax Example
  • laravel 6 Instamojo Payment Gateway Integration Example Tutorial
  • laravel 6 multiple database connections example tutorial
  • Laravel 7 Socialite Linkedin Login Tutorial Example
Laravel Tags:laravel 9 example, laravel 9 tutorial

Post navigation

Previous Post: Laravel 9 Fullcalendar Ajax Example Tutorial
Next Post: Laravel 9 Stripe Payment Gateway Integrate Example

Latest Posts

  • Laravel Check If Foreach is Empty Example
  • Laravel Check Array Empty in Blade
  • PHP Datatables CRUD Example
  • How to Convert Date and Time from one timezone to another in php
  • how to get current date and time in php
  • Drag and Drop Reorder Items with jQuery, PHP & MySQL
  • Laravel 9 Toastr Notifications Example Tutorial
  • Laravel 9 CRUD Operation Example Using Google Firebase
  • Laravel 9 CKeditor Image Upload With Example
  • Laravel 9 Summernote Image Upload With Example

Tools

  • Compound Interest Calculator
  • Hex to RGB Color Converter
  • Pinterest Video Downloader
  • Birthday Calculator
  • Convert JSON to PHP Array Online
  • JavaScript Minifier
  • CSS Beautifier
  • CSS Minifier
  • JSON Beautifier
  • JSON Minifier

Categories

  • Ajax
  • Angular
  • Angularjs
  • Bootstrap
  • Codeigniter
  • Css
  • Htaccess
  • Javascript
  • Jquery
  • Laravel
  • MongoDB
  • MySql
  • Nodejs
  • Php
  • React JS
  • Shopify Api
  • Ubuntu

Tags

angular 10 tutorial angular 11 ci tutorial codeigniter 4 image upload Codeigniter 4 Tutorial codeigniter tutorial CodeIgniter tutorial for beginners codeigniter with mysql crud operation eloquent relationships file upload File Validation form validation Image Upload jQuery Ajax Form Handling jquery tricks jquery tutorial laravel 6 Laravel 6 Eloquent Laravel 6 Model laravel 6 relationship laravel 6 relationship eloquent Laravel 6 Routing laravel 7 Laravel 7 Eloquent laravel 7 routing laravel 7 tutorial Laravel 8 laravel 8 example laravel 8 tutorial laravel 9 example laravel 9 tutorial Laravel Framework laravel from scratch Laravel Socialite laravel social login nodejs pagination payment gateway php with mysql react js tutorial rewrite rule send mail validation wysiwyg editor

Copyright © 2018 - 2022,

All Rights Reserved Powered by XpertPhp.com