How to Send Mail Using Office365 in Laravel?

Sep 21, 2022 . Admin



Hello Friends,

Today our leading topic is how to send mail using office365 in laravel. In this article, we will implement a send emails using office365 integrated with laravel. if you have questions about how to send emails in the laravel app using office365 then I will give a simple example with a solution. I explained simple steps by step to send emails in laravel using office365. So, let's follow a few steps to create an example of laravel sending emails using the office365 example.

In this tutorial will guide you step by step on sending emails in laravel using office365. So, you need to follow all the below-given steps one by one. And you can send email in laravel.

First of all, open your office365 app and set up app passwords login to your account and click Manage security and privacy.

Next click on create and manage app passwords.

All your app passwords will be listed on this page, to add a new one-click create.

Enter a name for the app password such as your website name.

Note down the generated app password it will not be displayed again. (the below is just an example)

Now with the pass password, you’re ready to start sending emails with SMTP.

Step 1: Install Laravel App

In this step, use the following command to install or download the laravel application:

composer create-project --prefer-dist laravel/laravel blog
Step 2: Configuration SMTP in .env

In this step, you need to configure smtp details in .env file like following:

.env
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

In this step, use the below-given command to create NotifyMail mailable class:

php artisan make:mail NotifyMail
App\Mail
<?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');
    }
}

By whatever name you will create an email template. That you want to send. Do not forget to add an email template name in the build class of the above-created notify email class.

return $this->view('view.name');
to
return $this->view('emails.demoMail');

In the next step, we will create an email template named demoMail.blade.php inside the resources/views/emails directory. That’s why we have added view name email.

Step 4: Add Send Email Route

In this step, open /web.php, so navigate to the routes directory. And then add the following routes for sending an email:

web.php
<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\SendEmailController;

/*
|--------------------------------------------------------------------------
| 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('send-email', [SendEmailController::class, 'index']);
Step 5: Create Directory And Blade View

In this step, create directory name emails inside the resources/views directory. Then create a demoMail.blade.php blade view file inside the resources/views/emails directory. And update the following code into it:

<!DOCTYPE html>
<html>
<head>
    <title>send mail using office365 in laravel</title>
</head>
<body>
 
    <h1>This is test mail from Mywebtuts.com</h1>
    <p>Laravel send email example</p>
 
</body>
</html>
Step 6: Create Send Email Controller

In this step, use the following command to create controller name SendEmailController:

php artisan make:controller SendEmailController

Then navigate to app/Http/Controllers directory and open SendEmailController.php. Then update the following code into it:

<?php
 
namespace App\Http\Controllers;
 
use Illuminate\Http\Request;
 
use Mail;
 
class SendEmailController extends Controller
{
     
    /**
     * Write code on Method
     *
     * @return response()
     */
    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');
        }
    } 
}
Run Development Server

Start the development server. Use the PHP artisan serve command and start your server:

php artisan serve

Now you are ready to run our example so run the below command to quick run.

http://localhost:8000/send-email

I hope it can help you...

#Laravel