Send emails with Office 365 SMTP

David Carr

Laravel Framework Tutorials PHP & MySQL

Office 365 is great for handling emails but it can also be used as an SMTP relay for your website. I've tried dedicated SMTP services with mixed results some work better than others. What I like about using Office 365 is it's easy to set up and very reliable. The added benefit of Office 365 is your sent emails saved in your sent folder! very handy.

Here are the SMTP credentials you need out of the box:

Host: smtp.office365.com
username: email_address
password: email_password
port: 587
encryption: tls

This will work without any setup, but if you have 2FA set up this will not work, instead, you need to create an app password. 

App passwords are passwords to be used for third party applications, this way you never expose your account password so it's a recommended approach even if you don't use 2FA but seriously use 2FA!

To set up app passwords login to your account 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.

To use Office 365 with Laravel setup the .env file as follows

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"

 

Read articles directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Copyright © 2006 - 2024 DC Blog - All rights reserved.