Laravel Mail send is one of the best tool in the web technology of PHP, Laravel providing easy to send email even you can send multiple mail although, Laravel mailing is easy to send and easy to understanding for the developer.
In this post we will learn how to send mail using in Laravel mail, for sending mail we need to set little config in the environment file - .env.
There are many way to send mail like 1. Laravel mail 2. other with the library file.
However we can easy to send multiple mail without hassles also email template load from blade file we can easy to design html template in blade file and make it styles and send the mail with best template.
In blade file you can easy to pass data and make it dynamic template also.
Also we can send attachment with mail template.
In this example we are using gmail email service provider but apart from this there are many service provider available in the market for Laravel sending mail.
Email service provider in the market.
- Gmail
- Sendmail
- Mailgun
- Sendgrid
- Postmark
- Amazon SES
But here we are using gmail service provider it's free to use.
Here i would like to say if you still not clone the Laravel project please clone the Laravel project here below link given.
Step 1 : Set config
After clone the laravel goto project and open .env file, Here you can see below config in in your.env file.
MAIL_MAILER=smtp MAIL_HOST=smtp.gmail.com MAIL_PORT=587 MAIL_USERNAME=put-here-your-gmail-username MAIL_PASSWORD=put-here-your-gmail-or-provider-password MAIL_ENCRYPTION=tls MAIL_FROM_ADDRESS=here-mail-address-form-you-want-to-send MAIL_FROM_NAME="${APP_NAME}" // name which you want to show mail receiver
Above configuration you can see in your environment file by default laravel giving us mailtrap for testing mail but we will use google gmail service.
If you send an email from Gmail account then you have to change configuration in your gmail account below given.
- Enable 2-step verification in your gmail account.
- Other one is enable lower restriction in your gmail account.
Step 2 : Clear Cache
After changing the .env file it may happen that your file may not work proper or work with old configuration, so we will clear cache in environment file for better practice.
php artisan config:cache
After thet you can create controller or you can direct run the route for send mail.
Step 3: Create controller
But here we will create controller for send the email with different example.
php artisan make:controller SendMailController --plain
Once you create controller set route in web.php for SendMailController
Step 4 : Define route
routes/web.php
Route::get('simple-mail','SendMailController@simpleMail'); Route::get('html-mail','SendMailController@htmlTemplateMail'); Route::get('attachment-mail','SendMailController@attachmentMail');
This will redirect to SendMailController sendMail method.
Step 5 : SendMailController
In this controller we will define the the different method of send mail like simple mail, template mail, attachment mail etc.
app/Http/Controllers/SendMailController.php
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\User; class HomeController extends Controller { public function simpleMail() { $name = ['name' => 'webappfix.com']; Mail::send(['text'=>'mail'], $name, function($message) { $message->to('abc@gmail.com', 'webappfix.com') ->subject('Test Mail'); $message->from('webappfix@gmail.com','webappfix'); }); dd('Mail successfully sended'); } public function htmlTemplateMail() { $name = ['name' => 'webappfix.com']; Mail::send('template.mail', $name, function($message) { $message->to('abc@gmail.com', 'webappfix.com') ->subject('Test template Mail'); $message->from('webappfix@gmail.com','webappfix'); }); dd('Mail successfully sended'); } public function attachmentMail() { $name = ['name' => 'webappfix.com']; Mail::send('template.mail', $name, function($message) { $message->to('abc@gmail.com', 'webappfix.com') ->subject('Test template Mail'); $message->attach(base_path('test.pdf')); $message->from('webappfix@gmail.com','webappfix'); }); dd('Mail successfully sended'); } }
Above is the example of different mail sending like simple text mail, html template mail, or with attachment mail etc.
Step 6 : Create Blade file
For sending template mail we need to create blade file in resources
Goto resources/views and create template folder, after create the template folder make a blade file with the name of mail.blade.php
resources/views/template/mail.blade.php
Hello, Good Morning Mr.{{ $data->name }}
Above is the email send which will show in the html template, In this template you can make styles html for user more attractive from mail template.
Step 7 : Run the laravel project
Goto your command promote and then go your project path and hit the below command.
php artisan server
Once start the server you can hit the different url for receive different mail.
http://localhost:8000/simple-mail http://localhost:8000/html-mail http://localhost:8000/attachment-mail
Well, now you can check your mail address you will receive mail in your inbox.
Apart from that laravel also providing laravel queue mail, In that email will set in queue and send mail one by one
I hope this mail tutorial help you to reach to send mail.
Thank You for read the post.
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 :
- Laravel One to One Eloquent Relationship Tutorial
- Custom Validation Message Laravel
- Laravel 6 - QR Code Generator Example
- How to setup Google's Two Factor Authentication in Laravel
- Select2 ajax example
- Laravel OneSignal Push Notification Integrate
- Laravel Upgrading To 10.0 From 9.x Version
- resize image and upload example laravel intervention
- laravel cache clear command
- [Fixed] Binary Does Not Exist Laravel