9951 explained code solutions for 126 technologies


phpmailerHow do I use PHPMailer with OAuth2 authentication for Microsoft accounts?


PHPMailer can be used with OAuth2 authentication for Microsoft accounts by following these steps:

  1. Install the PHPMailer composer package.
  2. Get the OAuth2 token from the Microsoft account.
  3. Configure the PHPMailer object with the OAuth2 token.

Example code

// Create a new PHPMailer instance
$mail = new PHPMailer;

// Set the OAuth2 access token
$mail->setOAuthToken($token);

// Set the from address
$mail->setFrom('[email protected]', 'First Last');

// Set the subject
$mail->Subject = 'PHPMailer OAuth2 Mail';

// Set the SMTP host
$mail->Host = 'smtp.example.com';

// Set the SMTP authentication type
$mail->SMTPAuthType = 'XOAUTH2';

// Send the message
$mail->send();

The above code will configure the PHPMailer object with the OAuth2 token and send the message using the SMTP authentication type XOAUTH2.

Parts of the code:

  • $mail = new PHPMailer;: Creates a new PHPMailer instance.
  • $mail->setOAuthToken($token);: Sets the OAuth2 access token.
  • $mail->setFrom('[email protected]', 'First Last');: Sets the from address.
  • $mail->Subject = 'PHPMailer OAuth2 Mail';: Sets the subject.
  • $mail->Host = 'smtp.example.com';: Sets the SMTP host.
  • $mail->SMTPAuthType = 'XOAUTH2';: Sets the SMTP authentication type.
  • $mail->send();: Sends the message.

Helpful links

Edit this code on GitHub