Laravel Send Email Using Office365 Example

10-Apr-2023

.

Admin

Laravel Send Email Using Office365 Example

Hello Friends,

Now, let's see an example of laravel sending emails using the office365 example. we will help you to give an example of how to send mail using office365 in laravel. Here you will learn to send emails using office365 integrated with laravel. We will look at an example of laravel sending emails using the office365 example. Here, Creating a basic example of how to send emails in the laravel app using office365.

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 into 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 build class of the above created notifymail 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>laravel send email using office365 example</title>

</head>

<body>

<h1>This is test mail from NiceSnippets.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