Laravel 10 Send Emails using Office365 Example

Laravel 10 Send Emails using Office365 Example

Sending emails is an essential feature of web applications, whether it is for password reset, email confirmation, or sending newsletters. Laravel 10 provides various drivers for sending emails, including SMTP, Sendmail, and Amazon SES. In this tutorial, we will be using the SMTP driver and Office365 as the email service provider.

Before we begin, make sure that you have the following:

  • A Laravel 10 project set up and running
  • An Office365 account

Step 1: Install Required Packages

The first step is to install the required packages using Composer. Run the following command in the terminal:

composer require guzzlehttp/guzzle

Step 2: Set Up Office365 Email Configuration

Next, we need to configure our Office365 email settings. Open the .env file in the root directory of your Laravel project and add the following lines:


These are the email configuration settings for Laravel 10 to use Office365 as the email service provider. Let me explain each setting:

  • MAIL_MAILER: Specifies the email driver to be used. In this case, we are using the SMTP driver.
  • MAIL_HOST: The SMTP server host address for Office365.
  • MAIL_PORT: The SMTP port number for Office365.
  • MAIL_USERNAME: Your Office365 email address.
  • MAIL_PASSWORD: Your Office365 email password.
  • MAIL_ENCRYPTION: The encryption method to be used for the SMTP connection. In this case, we are using tls.
  • MAIL_FROM_ADDRESS: The email address that will be used as the sender’s address in the emails sent from the application.
  • MAIL_FROM_NAME: The name that will be used as the sender’s name in the emails sent from the application. Here, we are using the APP_NAME environment variable, which should be defined in the .env file.

Replace your_office365_email_address and your_office365_email_password with your Office365 email address and password, respectively. Also, make sure to set MAIL_ENCRYPTION to tls.

Step 3: Create Email Template

In this step, we will create an email template that will be sent to the recipient. Create a new file called welcome.blade.php in the resources/views/emails directory and add the following code:

<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Welcome Email</title>
    <h1>Welcome to our website</h1>
    <p>Thank you for signing up to our website. We are excited to have you on board.</p>

This is an example of an email template that can be used for the welcome email. It is written in HTML and contains a simple greeting message. The <!DOCTYPE html> declaration specifies the type of document as an HTML document. The <html> tag marks the start of the HTML document, and the <head> tag contains the metadata for the document, such as the title and character encoding. The <body> tag contains the content of the document, including the greeting message.

You can customize this template to include your own branding, messaging, and design elements. When using this template with Laravel, you would typically save it as a Blade view file in the resources/views/emails directory, and use the markdown method to render it as HTML for the email body. For example, in the WelcomeEmail class’s build method, you would replace $this->markdown('emails.welcome') with $this->markdown('emails.welcome')->with('data', $this->data) to pass data to the email template.

Step 4: Create Email Controller

Now, let’s create an email controller that will handle the email sending. Run the following command in the terminal:

php artisan make:controller EmailController

Open the app/Http/Controllers/EmailController.php file and add the following code:


namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;
use App\Mail\WelcomeEmail;

class EmailController extends Controller
    public function sendEmail()
        $email = 'recipient_email_address';
        $data = [
            'name' => 'Recipient Name',
        Mail::to($email)->send(new WelcomeEmail($data));
        return "Email sent successfully";

This is an example of an email controller that can be used to send a welcome email to a recipient. The sendEmail method is defined to send the email using the Mail facade and the WelcomeEmail class, which is responsible for building the email message.

In this example, the recipient’s email address is hardcoded as a string in the $email variable, but you can replace this with a dynamic value, such as a value obtained from a form input or database record. The $data array contains any data that needs to be passed to the email template, such as the recipient’s name.

After calling the to method of the Mail facade to specify the recipient’s email address, the send method is called with an instance of the WelcomeEmail class as an argument to send the email. Finally, the method returns a string message indicating that the email was sent successfully.

Note that in a real application, you may need to implement additional logic to handle error conditions, validate input data, and so on.

Replace recipient_email_address with the recipient’s email address. We are passing the recipient’s name as an array to the email template.

Step 5: Create Email Class

In this step, we will create an email class that will contain the logic for sending the email. Run the following command in the terminal:

php artisan make:mail WelcomeEmail --markdown=emails.welcome

This command will create a new WelcomeEmail class in the app/Mail directory and a welcome.blade.php file in the resources/views/emails directory.

Open the app/Mail/WelcomeEmail.php file and add the following code:

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;

class WelcomeEmail extends Mailable
use Queueable, SerializesModels;

public $data;

public function __construct($data)
    $this->data = $data;

public function build()
    return $this->markdown('emails.welcome')->with('data', $this->data);

This is an example of a Laravel Mailable class that can be used to generate the welcome email. It is named WelcomeEmail and extends the Mailable class provided by Laravel.

The class includes the Queueable and SerializesModels traits to enable queueing and serialization of the email message. The $data variable is a public property that contains the data that will be passed to the email template.

The constructor method is used to pass the $data variable to the class. The build method is used to build the email message, and it returns an instance of the Illuminate\Mail\Mailable class. In this method, the markdown method is called to specify the email template to use, and the with method is used to pass the $data variable to the template.

Note that you can use different types of email templates, such as HTML or plain text, by using the appropriate method (e.g. view for HTML, text for plain text) instead of markdown. You can also customize the template file path and name by passing a different value to the markdown method.

Once you have defined this Mailable class, you can use it to send emails in your Laravel application by creating an instance of the class and passing it to the Mail facade’s send method, as shown in the EmailController example earlier.

Step 6: Test Email Sending

Finally, let’s test our email sending. Open the `routes/web.php` file and add the following code:

use App\Http\Controllers\EmailController;

Route::get('/send-email', [EmailController::class, 'sendEmail']);

This code will create a new route `/send-email` that will trigger the `sendEmail` method of the `EmailController`. Run the following command in the terminal to start the Laravel development server:

php artisan serve

Open your browser and navigate to `http://localhost:8000/send-email`. If everything is set up correctly, you should see the message “Email sent successfully”. Check your recipient email address to confirm if you have received the email.


Sending emails in Laravel 10 using Office365 is straightforward. We have used the SMTP driver to send emails, and Office365 as the email service provider. In this tutorial, we have covered the basic steps to send an email in Laravel 10, including configuring the email settings, creating an email template, creating an email controller, creating an email class, and testing the email sending.

This Article Ideas has been taken from the following websites; which are following:

Laravel 8 Send Emails using Office365 Example

Laravel Microsoft Graph Mail driver – Mail Driver for Office365


Leave a Reply

Your email address will not be published. Required fields are marked *