Watch Youtube Video (HINDI) :
In this comprehensive post example, we will see how to send email in laravel 10, I will show an example and guide you one by one step from scratch for sending mail in laravel 10 latest version. Mail sending is the most important part of today's digital marketing part.
We will make a mail class and also we will configure google gmail SMTP configuration then after I will show to step by step process of getting send emails to your users. also, you can follow youtube videos if you want to know more practical.
There are many changes given by the laravel 10 version for sending an email. We can use markdown mail, email sends using the queue, simple mail send template mail send or many more things given with a clear understanding.
I will show you a DemoMail and how to configure mail settings or credentials and then we will send mail all things I will show you in this post. so let's start.
Step 1. Create Project
In the first step, we will create a new fresh laravel 10 project. Skip this step if you already installed it. use the below command to create a new fresh laravel 10 project.
composer create-project laravel/laravel example-app
Step 2. Mail Configuration
Open your .env file, you will see mail configuration keys, there you need to set your mail to send servicer provider details. I am using the gmail service for youtube videos. you can follow if you want to generate it.
MAIL_MAILER=smtp MAIL_HOST=smtp.gmail.com MAIL_PORT=587 MAIL_USERNAME=YOUR_MAIL_USERNAME MAIL_PASSWORD=YOUR_MAIL_PASSWORD MAIL_ENCRYPTION=tls MAIL_FROM_ADDRESS=YOUR_MAIL_ADDRESS MAIL_FROM_NAME="webappfix"
Step 3. Make Mail Class
In this step, we will create a mail class, use the below command and create a DemoMail class.
php artisan make:mail DemoMail
This is the main class that we create using the command. here in this mail class we will set the subject name, define the blade view file, and construct a method to exchange data between classes.
app/Mail/DemoMail.php<?php namespace App\Mail; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Mail\Mailable; use Illuminate\Mail\Mailables\Content; use Illuminate\Mail\Mailables\Envelope; use Illuminate\Queue\SerializesModels; class DemoMail extends Mailable { use Queueable, SerializesModels; public $mailData; /** * Create a new message instance. */ public function __construct($mailData) { $this->mailData = $mailData; } /** * Get the message envelope. */ public function envelope(): Envelope { return new Envelope( subject: 'Demo Mail', ); } /** * Get the message content definition. */ public function content(): Content { return new Content( view: 'emails.demoMail', ); } /** * Get the attachments for the message. * * @return array*/ public function attachments(): array { return []; } }
Step 4. Make MailController
Now, we will create a MailController, use given the following command, and create a controller.
php artisan make:controller MailController
Open your controller file and add an index() method as below given MailController, this method will send mail just y pressing the URL in the browser. for this method, we will now define the route.
app/Http/Controllers/MailController.php<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Mail; use App\Mail\DemoMail; class MailController extends Controller { public function index() { $mailData = [ 'title' => 'Mail from Webappfix', 'body' => 'This is for testing email usign smtp', ]; Mail::to('pjanbleeker@skafi.xyz')->send(new DemoMail($mailData)); dd('Email send successfully.'); } }
Step 5. Define Route
Open your web.php file, paste this route file, and also call your controller at the top of the web.php file.
routes/web.phpuse App\Http\Controllers\MailController; Route::get('send-mail',[MailController::class,'index']);
Step 6. Blade File Email Template
Create a blade file given path. After that, you can create your custom-made design template, I am giving just simple a paragraph for the mail send demonstration.
resources/views/emails/demoMail.blade.php<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Webappfix</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>
Step 7. Start Server
We will start your local development server, and use the below command in your project terminal.
php artisan serve
Now open this URL in your browser, By running this URL mail will be sent to the given respected receiver mail address. Open your mail address index and you will ss there inbox you will receive a mail with the given subject.
http://localhost:8000/send-mail
We always thanks to you for reading our blogs.
Dharmesh Chauhan
(Swapinfoway Founder)Hello Sir, We are brothers origin from Gujarat India, Fullstack developers working together since 2016. We have lots of skills in web development in different technologies here I mention PHP, Laravel, Javascript, Vuejs, Ajax, API, Payment Gateway Integration, Database, HTML5, CSS3, and Server Administration. So you need our service Please Contact Us
Haresh Chauhan
(Co-Founder)We Are Also Recommending You :
- Default index.php Root Path Change And Remove Public From The Url
- Laravel 9 Authorize.Net Payment Gateway Integration
- Get message-id mail send mailable laravel
- Arr::collapse() | Laravel Helper Function
- Pay10 Payment Gateway Integration In Laravel 10
- Laravel debugbar install
- Laravel 9 CRUD Operation From Scratch Step By Step Guideline
- Call to undefined function Gregwar\Captcha\imagecreatetruecolor()
- Email Send Using Queue In Laravel
- Laravel 9 Ajax Image Upload With Preview Tutorial