Sending Mails with Laravel and Microsoft Office 365 the secure way
Tim Geisendörfer • March 23, 2023laravel mail smtp microsoft
In the ever-changing digital landscape, securing sensitive information has become paramount. Email communication, one of the most widely used methods of data exchange, requires a robust and secure approach to ensure the privacy and integrity of its contents. Laravel, a popular choice for web application development, demands best practices for sending emails securely. This is where Microsoft Office 365 and the Microsoft Graph API come into play.
In this blog post, we will explore the importance of using Microsoft Office 365 with the Microsoft Graph API to send emails from your Laravel applications, as opposed to the insecure and outdated SMTP basic auth protocol. On October 1, 2022, Microsoft disabled Basic Auth for all its services (except SMTP), and has announced plans to phase it out for SMTP as well. We will examine the security benefits and advantages of adopting this modern approach, equipping you with the knowledge and confidence to keep your application's email communications safe from prying eyes and malicious actors.
Advantages of using Microsoft Office 365 with the Microsoft Graph API
Compared to using SMTP, the initial configuration of the Graph API adapter is a little more complex. However, the advantages of using the Graph API far outweigh the initial setup time. It will significantly improve the security of your Laravel app, and when Microsoft disables SMTP Basic Auth you don't have to worry. The Graph API is a RESTful API allowing you to access Microsoft Ecosystem data. We will show you how to use it with your Laravel App in just a few minutes.
Install the Laravel Mail Adapter
We have created a Laravel Mail Adapter for Microsoft Office 365. This adapter allows you to send emails using the Microsoft Graph API.
The first step is to install the Laravel Mail Adapter. This is an easy one using composer:
composer require innoge/laravel-msgraph-mail
Now add a new entry to your mail driver configuration under
'microsoft-graph' => [ 'transport' => 'microsoft-graph', 'client_id' => env('MICROSOFT_GRAPH_CLIENT_ID'), 'client_secret' => env('MICROSOFT_GRAPH_CLIENT_SECRET'), 'tenant_id' => env('MICROSOFT_GRAPH_TENANT_ID'), 'from' => [ 'address' => env('MAIL_FROM_ADDRESS'), 'name' => env('MAIL_FROM_NAME'), ], ],
Now you're done with installing the mail adapter. The next step is to configure your Azure App Registration and fill your .env file with the authentication data.
Create an Azure App Registration
The first step is to create an Azure App Registration. This is a simple process that can be done in the Azure Portal. You must be logged in with an account with access to the Azure Portal. If you don't have an account, you can create one for free.
- Go to the Azure Portal and click on "App Registrations" in the menu.
- Press the "New registration" button.
- Enter a name for your app registration and select "Accounts in this organizational directory only"
- Click on "Register"
The registration form should look like this:
Set App Permissions
- Go to the "API Permissions" tab and click on "Add a permission"
- Select "Microsoft Graph" and "Application permissions"
- Select "Mail.Send"
- Click on "Add permissions"
- Now click on "Grant admin consent for
Create a Client Secret
- Go to the "Certificates & secrets" tab and click on "New client secret"
- Enter a description and select an expiration date
- Click on "Add"
- Copy the client secret value and save it somewhere safe. You will need it later.
Configure the Laravel App
Now that you have created your Azure App Registration, you can configure your Laravel App. Please fill following
variables in your
MICROSOFT_GRAPH_CLIENT_ID= The Application (client) ID of your Azure App Registration
MICROSOFT_GRAPH_TENANT_ID= The tenant ID of your Azure Active Directory (you can find it on your apps Overview page)
MICROSOFT_GRAPH_CLIENT_SECRET= The client secret you created in the previous step
MAIL_FROM_ADDRESS= The email address you want to send emails from
MAIL_FROM_NAME= The name you want to send emails from
We hope this blog post has given you a good overview of the advantages of replacing the SMTP connector with the Microsoft Graph API to send emails from your Laravel applications.
Please note that if you want to do mass mail sending with your application Microsoft Office 365 is the wrong choice. Microsoft only allows 30 emails sent per minute or 10.000 mails per day.
If you have any questions or comments, please feel free to contact us.