In this tutorial you will learn about the Laravel 8 Send Emails using Office365 Example and its application with practical example.
In this Laravel 8 Send Mail using Office365 Tutorial I will show you how to send mail using Office365 in laravel 8. In this tutorial you will learn to send mails using Office365 In laravel application. In this post I’ll show you how to integrate Office365 to send email in laravel 8 application.
Laravel 8 provides builtin mailable markdown class to send emails. In Laravel 8 you can use mailtrap, smtp, Mailgun, Postmark, Amazon SES, and sendmail for sending emails. You are required to configure driver details on the .env file.
Laravel 8 Send Emails using Office365 Example
In this step by step tutorial you will understand how to send emails in Laravel using Office365.
- Step 1 – Install Laravel 8 App
- Step 2 – Configuration SMTP in .env
- Step 3 – Create Mailable Class
- Step 4 – Add Email Send Route
- Step 5 – Create Directory And Blade View
- Step 6 – Create Email Controller
- Step 7 – Run Development Server
Step 1 – Install Laravel 8 App
First of all we need to create a fresh laravel project, download and install Laravel 8 using the below command
1 |
composer create-project --prefer-dist laravel/laravel blog |
Step 2 – Configuration SMTP in .env
In this step we will configure Office365 smtp settings in .env file as following:
1 2 3 4 5 6 7 8 |
MAIL_DRIVER=smtp MAIL_HOST=smtp.office365.com MAIL_PORT=587 MAIL_USERNAME=email MAIL_PASSWORD=app_password MAIL_ENCRYPTION=tls MAIL_FROM_ADDRESS=email MAIL_FROM_NAME="your app name" |
Step 3 – Create Mailable Class
Now, create a mailable class for sending emails, use the following command :
1 |
php artisan make:mail NotifyMail |
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 |
<?php namespace App\Mail; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Mail\Mailable; use Illuminate\Queue\SerializesModels; class NotifyMail extends Mailable { use Queueable, SerializesModels; /** * Create a new message instance. * * @return void */ public function __construct() { // } /** * Build the message. * * @return $this */ public function build() { return $this->view('view.name'); } } |
Here you need to add an email template name in build class of the above created notifymail class.
1 2 3 |
return $this->view('view.name'); to return $this->view('emails.demoMail'); |
In next step, we will create email template named demoMail.blade.php inside resources/views/emails directory. That’s why we have added view name email.
Step 4 – Add Send Email Route
After this, we need to define routes in “routes/web.php” file. Lets open “routes/web.php” file and add the following routes in it.
routes/web.php
1 2 3 |
use App\Http\Controllers\SendEmailController; Route::get('send-email', [SendEmailController::class, 'index']); |
Step 5 – Create Directory And Blade View
In this step, we will create view/blade file to setup email template. Open resources/views/Email/demoMail.blade.php file, put the following code.
resources/views/Email/demoMail.blade.php
1 2 3 4 5 6 7 8 9 10 11 12 |
<!DOCTYPE html> <html> <head> <title>Laravel 8 Send Email Example</title> </head> <body> <h1>This is test mail</h1> <p>Laravel 8 send email example</p> </body> </html> |
Step 6 – Create Send Email Controller
In this step we will create a new controller SendEmailController with one method to send email. Open your terminal and run following command to create controller:
1 |
php artisan make:controller SendEmailController |
Then navigate to app/Http/Controllers directory and open SendEmailController.php. Then update the following code into it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Mail; class SendEmailController extends Controller { public function sendEmail() { Mail::to('receiver-email-id')->send(new NotifyMail()); if (Mail::failures()) { return response()->Fail('Sorry! Please try again latter'); }else{ return response()->success('Great! Successfully send in your mail'); } } } |
Step 7 – Run Development Server
In this step, use this PHP artisan serve command to start your server locally:
1 |
php artisan serve |
Then open browser and fire the following URL on it:
1 |
http://127.0.0.1:8000/send-email |