isSMTP(); // Use SMTP $mail->SMTPDebug = SMTP::DEBUG_OFF; // Set DEBUG_LOWLEVEL for SMTP diagnostics $mail->SMTPAuth = true; // Enable SMTP authentication $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; // Enable implicit TLS encryption $mail->Port = 587; // TCP port; MSFT doesn't like 465 $mail->AuthType = 'XOAUTH2'; // Set AuthType to use XOAUTH2 ('LOGIN' for Basic auth) // Sender and recipients $mail->setFrom('from@example.com', 'Mailer'); // 'Header' From address with optional sender name $mail->addAddress('joe@example.net', 'Joe User'); // Add a To: recipient /** * Authenticate * Note that any ClientCertificatePrivateKey should include the -----BEGIN PRIVATE KEY----- and * -----END PRIVATE KEY----- */ $oauthTokenProvider = new SendOauth2B( [ 'mail' => $mail, // PHPMailer instance 'clientId' => 'long string', // for Google service account, Unique ID 'clientSecret' => 'long string', // or null if using a certificate 'clientCertificatePrivateKey' => 'ultra long string', // or null if using a clientSecret 'clientCertificateThumbprint' => 'long string', // or null if using a clientSecret 'serviceProvider' => 'Microsoft', // literal: also 'Google' or 'GoogleAPI' 'authTypeSetting' => $mail->AuthType, // is set above - or insert here as 'XOAUTH2' 'mailSMTPAddress' => 'me@mydomain.com', // Envelope/mailFrom/reverse-path From address 'refreshToken' => 'very long string', // null if grantType is 'client_credentials' 'grantType' => 'authorization_code', // or 'client_credentials' 'tenant' => 'long string', // MSFT tenant GUID. Null for Gmail 'hostedDomain' => 'mydomain.com', // Any Google (and optional). Null for MSFT 'projectID' => 'string', // GoogleAPI only. Else null 'serviceAccountName' => 'string', // GoogleAPI service account only. Else null 'impersonate' => 'you@mydomain.com', // Google API service account only. Else null // default to 'mailSMTPAddress', must be // a Google Wspace email adddress, not @gmail 'gmailXoauth2Credentials' => 'your credentials.json', // File name - defaults to: // gmail-xoauth2-credentials.json 'writeGmailCredentialsFile' => 'yes' or 'no', // Defaults to 'yes'; meaning the // credentials json is dynamically created ] ); $mail->setOAuth($oauthTokenProvider); // Pass OAuthTokenProvider to PHPMailer $mail->Host = 'smtp.office365.com'; // Set SMTP server (smtp.gmail.com for Gmail) // Content $mail->isHTML(true); // Set email format to HTML $mail->Subject = 'Here is the subject'; $mail->Body = 'This is the HTML message body in bold!'; $mail->AltBody = 'This is the body in plain text for non-HTML mail clients'; $mail->send(); echo 'Message has been sent'; } catch (Exception $e) { echo 'Message could not be sent. Mailer Error: ' . htmlspecialchars($mail->ErrorInfo, ENT_QUOTES); }