PHPMailer with Microsoft 365

In this example we will show you how to use PHPMailer to send e-mails with Microsoft 365.

To be able to send your emails via your Microsoft 365 email address, you’ll first need to enable SMTP authentication for that email address in your Microsoft 365 admin center. Otherwise, Microsoft 365 will block requests to the SMTP server.

To get started, open the Active users tab in your Microsoft 365 admin center. You can click here to open the right page or expand the hamburger icon in the top-left corner of the admin center and go to Users > Active users.

Then, click on the email account that you want to use to send your WordPress site’s emails. This will expand a slide-out with more options.

In the slide-out, go to the Mail tab. Then, click the Manage email apps option. Next check the option Authenticated SMTP and click on the Save changes button.

  •  Download PhpMailer folder and upload its content to the server

Again make sure you download the PHPMailer package and upload it to public directory on your server.

  • Create phpmail.php file and add the code

Create phpmail.php file add the following code lines, save it and upload it into the public directory using the FTP client.

// Import PHPMailer classes into the global namespace // These must be at the top of your script, not inside a function 
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require './PHPMailer/src/Exception.php';
require './PHPMailer/src/PHPMailer.php';
require './PHPMailer/src/SMTP.php';
$mail = new PHPMailer(true); // Passing `true` enables exceptions 
try { 
//Server settings 
$mail->SMTPDebug = 2; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = ''; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = "[email protected]";
$mail->Password = "your-mail-password";
$mail->SMTPSecure = 'tls'; // Enable SSL encryption, TLS also accepted with port 465
$mail->Port = 587; // TCP port to connect to 
$mail->setFrom('[email protected]', 'Mailer'); //This is the email your form sends From 
$mail->addAddress('[email protected]', 'Joe User'); // Add a recipient address
$mail->isHTML(true); // Set email format to HTML 
$mail->Subject = 'Subject line goes here';
$mail->Body = 'Body text goes here'; 

$mail->send(); echo 'Message has been sent'; 
} catch (Exception $e) { echo 'Message could not be sent.'; 
echo 'Mailer Error: ' . $mail->ErrorInfo;