UPDATE: April 2020
Using the accepted answer for sending email using Office 365 has the high chance of not working since Microsoft is pushing for their Microsoft Graph (the only supported PHP framework right now is Laravel). If fortunately you were still able to make it work in your application, email will either go to the recipient's Junk, Trash, or Spam folder, which you don't want to happen.
Common errors I encountered were:
Failed to authenticate password. // REALLY FRUSTRATED WITH THIS ERROR! WHY IS MY PASSWORD WRONG?!
or
Failed to send AUTH LOGIN command.
or
Unable to send email using PHP SMTP. Your server might not be configured to send mail using this method.
In order to still make it work with the accepted answer, we just have to change a single line, which is the Password parameter line:
$mail->Password = 'YourOffice365Password';
Instead of setting the password with the one you use when you login to your Office365 account, you have to use an App Password instead.
Create App Password
First, in order to create an App Password, the Multi-Factor Authentication of your Office 365 account should be enabled (you may have to contact your administrator for this to be enabled).
After that, login your Office 365 in your favorite browser
- Go to My Account page (you will see the link to this page when you click your name's initials on the upper right)
- Choose Security & Privacy then Additional security verification
- At the top of the page, choose App Passwords
- Choose create to get an app password
- If prompted, type a name for your app password, and click Next
- You will then see the password generated by Office 365 as your App Password
- Copy the password
After copying the password, go back to your working code and replace the Password parameter with the copied password. Your application should now be able to properly send email using Office 365.
Reference:
Create an app password for Microsoft 365
SMTPDebug = 3
to get an SMTP transcript. Office 365 SMTP is unreliable - I've several reports of it failing. – Lionizepostfix
in debian/ubuntu:ps aux | grep postfix
and if you get no output:sudo /etc/init.d/postfix start
. You can also view undelivered emails like so:sudo mailq
. This will be empty if the emails are being sent. – Vaccine