What was wrong with good ole using SMTP?

Has anyone here successfully used an XOAuth library for PHP? Specifically, I'm looking to connect to my Office 365 mailbox.

SENSASIBET77 commented: Hai gusy +0

Recommended Answers

All 9 Replies

I have not used this with PHP, but are you connecting to the Graph API or EWS?

Huh? I'm referring to the XOAUTH2 library.

… to connect to the SMTP server. You can no longer connect with just a username and password.

For my particular use case, I use a PHP library called Bounce Mail Handler (formerly PHPMailer-BMH) that was last updated in 2019. It connects to the email server via SMTP using a username and password.

I am trying to do the least amount of work possible to modify this library to connect via SMTP using XOAuth2, which is now required by nearly every mail server I've encounered (e.g. Office 365, Gmail, etc.)

Maybe you were thinking of something like this:

<?php
// Include PHPMailer-BMH library
require 'vendor/autoload.php'; // Adjust the path as needed

// Create a new BounceMailHandler instance
$bmh = new \voku\BounceMailHandler\BounceMailHandler();

// Set up Office 365 SMTP settings
$bmh->setSmtpHost('smtp.office365.com');
$bmh->setSmtpPort(587);
$bmh->setSmtpSecure('tls');
$bmh->setSmtpAuth(true);

// Set OAuth2 credentials
$bmh->setAuthType('XOAUTH2');
$bmh->setOauthClientId('your-client-id');
$bmh->setOauthClientSecret('your-client-secret');
$bmh->setOauthAccessToken('your-access-token'); // Obtained via the client credential flow

// Set bounce email address
$bounceEmail = 'bounce-handler@daniweb.com';

// Compose your bounce email (example)
$bmh->setFrom($bounceEmail, 'Bounce Mail Handler');
$bmh->setRecipient('toneewa@nasa.gov', 'toneewa'); // Recipient for bounce notifications
$bmh->setSubject('Bounce Notification');
$bmh->setBody('A bounce notification was received. Handle it appropriately.');

// Send the bounce email
if ($bmh->send()) {
    echo 'Bounce email sent successfully!';
} else {
    echo 'Error sending bounce email: ' . $bmh->getError();
}

//...
?>
commented: AI generated code? It doesn't make sense :( -8

Wait, huh?? Is that code generated by ChatGPT or something? It doesn't make any sense?

Unfortunately the first link is for support with PHPMailer itself (totally different library), and the second link is just a generic example of how to use the library which we are already doing. Thank you for trying though!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.