How to Send Emails via PHP Using Microsoft 365 SMTP: A Comprehensive Guide
In today's digital age, sending emails programmatically is a crucial feature for many web applications. This guide will walk you through the process of setting up and using Microsoft 365 SMTP to send emails via PHP.
Prepare Microsoft 365 Email Account to Use SMTP Authentication
- Access Microsoft 365 Admin Center
- Log in to the Microsoft 365 admin center
- Click on "Show all" to reveal all options
- Disable Security Defaults
- Navigate to the Microsoft Entra Admin Center (formerly known as Identity)
- Go to Overview < Properties
- Under "Security Defaults," click on "Manage security defaults"
- Select "Disabled" (not recommended)
- Choose a reason for disabling (e.g., "My organization is unable to use apps/devices")
- Confirm by clicking "Disable"
- Finally after disabling security defaults for your organisation Microsoft Entra Admin Center security defaults, should show a warning like this
Note: Disabling security defaults will also disable Multifactor Authentication (MFA) for new email accounts. You'll need to manually enable MFA for each new account created afterward.
- Enable Authenticated SMTP
- In the Microsoft admin center, go to Users < Active users
- Select the email account you want to configure
- In the user settings panel, go to "Mail"
- Click on "Manage email apps"
- Ensure that "Authenticated SMTP" is enabled
- Disable Multifactor Authentication for the Sending Account
- In the Microsoft admin center, go to Users < Active users
- Click on "Multi-factor authentication"
- Search for the email account you want to use for sending
- In the right pane under "Quick steps," click on "Disable"
Sending Emails via PHP
- Gather Required Information
- SMTP Server: smtp.office365.com
- SMTP Port: 587
- Encryption: STARTTLS
- Username: Your full email address (e.g., sendemail@yourdomain.com)
- Password: Your email account password
- Install PHPMailer
If you haven't already installed PHPMailer, you can do so using Composer:
bashcomposer require phpmailer/phpmailer
- Configure PHPMailer in Your PHP Script
Use the following PHP script as a template:
php<?php use PHPMailerPHPMailerPHPMailer; use PHPMailerPHPMailerException; require 'vendor/autoload.php'; $mail = new PHPMailer(true); try { // Server settings $mail->isSMTP(); $mail->Host = 'smtp.office365.com'; $mail->SMTPAuth = true; $mail->Username = 'sendemail@yourdomain.com'; $mail->Password = 'YOUR_PASSWORD'; // Replace with your email account password $mail->SMTPSecure = 'tls'; // Use 'tls' for STARTTLS encryption $mail->Port = 587; // Recipients $mail->setFrom('sendemail@yourdomain.com', 'Your Name'); $mail->addAddress('recipient@example.com', 'Recipient Name'); // Content $mail->isHTML(true); $mail->Subject = 'Test Email'; $mail->Body = 'This is a test email sent from PHP using Office 365 SMTP.'; $mail->AltBody = 'This is a test email sent from PHP using Office 365 SMTP.'; // Send email $mail->send(); echo 'Message has been sent'; } catch (Exception $e) { echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}"; }
- Test Your Setup
- Upload the script to your server
- Run the script in your browser or via CLI
- Check for successful email delivery or error messages
Important Notes
- Security Consideration: Basic authentication (username and password) is less secure than OAuth2. Be cautious when storing and handling passwords.
- SMTP Configuration: Microsoft has been moving towards disabling basic authentication for SMTP. Ensure this method is compatible with your current Microsoft 365 configuration. To send email using secure OAuth2. Read this article from Microsoft How to set up a multifunction device or application to send emails using Microsoft 365 or Office 365
- Error Handling: Always check the error messages returned by PHPMailer for troubleshooting.
- MFA Impact: Remember that disabling security defaults affects MFA for new accounts. Manually enable MFA for each new email account created after this change.
By following these steps, you should be able to successfully send emails via PHP using Microsoft 365 SMTP. This setup can be particularly useful for automated email notifications, contact forms, or any other email functionality in your PHP applications.