This commit is contained in:
Marcus Bointon 2024-03-26 18:26:42 +01:00
parent 749e9a3a9d
commit 5d698fc44d
No known key found for this signature in database
GPG Key ID: DE31CD6EB646AA24
1 changed files with 33 additions and 34 deletions

View File

@ -11,26 +11,24 @@
* *
* The wrapper is installed with Composer from the decomplexity/SendOauth2 repo; see its README. * The wrapper is installed with Composer from the decomplexity/SendOauth2 repo; see its README.
* *
* The wrapper can also be invoked using less (or even no) arguments; this is for those websites * The wrapper can also be invoked using fewer (or even no) arguments; this is for those websites
* that use PHPMailer in several places. See the repo for details. * that use PHPMailer in several places. See the repo for details.
*/ */
// Import PHPMailer classes
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
// Import SendOauth2B class
use decomplexity\SendOauth2\SendOauth2B;
// Uncomment the next two lines to display PHP errors // Uncomment the next two lines to display PHP errors
// error_reporting(E_ALL); // error_reporting(E_ALL);
// ini_set("display_errors", 1); // ini_set("display_errors", 1);
// Load Composer's autoloader // Load Composer's autoloader
require 'vendor/autoload.php'; require 'vendor/autoload.php';
// Import SendOauth2B class into the global namespace
use decomplexity\SendOauth2\SendOauth2B;
// Import PHPMailer classes into the global namespace
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
// Set timezone for SMTP // Set timezone for SMTP
date_default_timezone_set('Etc/UTC'); date_default_timezone_set('Etc/UTC');
@ -39,8 +37,8 @@ $mail = new PHPMailer(true);
try { try {
// Server settings // Server settings
$mail->SMTPDebug = SMTP::DEBUG_OFF; // Set DEBUG_LOWLEVEL for SMTP diagnostics
$mail->isSMTP(); // Use SMTP $mail->isSMTP(); // Use SMTP
$mail->SMTPDebug = SMTP::DEBUG_OFF; // Set DEBUG_LOWLEVEL for SMTP diagnostics
$mail->SMTPAuth = true; // Enable SMTP authentication $mail->SMTPAuth = true; // Enable SMTP authentication
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; // Enable implicit TLS encryption $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; // Enable implicit TLS encryption
$mail->Port = 587; // TCP port; MSFT doesn't like 465 $mail->Port = 587; // TCP port; MSFT doesn't like 465
@ -50,34 +48,35 @@ try {
$mail->setFrom('from@example.com', 'Mailer'); // 'Header' From address with optional sender name $mail->setFrom('from@example.com', 'Mailer'); // 'Header' From address with optional sender name
$mail->addAddress('joe@example.net', 'Joe User'); // Add a To: recipient $mail->addAddress('joe@example.net', 'Joe User'); // Add a To: recipient
/** /**
* Authenticate * Authenticate
* Note that any ClientCertificatePrivateKey should include the -----BEGIN PRIVATE KEY----- and * Note that any ClientCertificatePrivateKey should include the -----BEGIN PRIVATE KEY----- and
* -----END PRIVATE KEY----- * -----END PRIVATE KEY-----
*/ */
$oauthTokenProvider = new SendOauth2B( $oauthTokenProvider = new SendOauth2B(
['mail' => $mail, // PHPMailer instance [
'clientId' => 'long string', // for Google service account, Unique ID 'mail' => $mail, // PHPMailer instance
'clientSecret' => 'long string', // or null if using a certificate 'clientId' => 'long string', // for Google service account, Unique ID
'clientCertificatePrivateKey' => 'ultra long string', // or null if using a clientSecret 'clientSecret' => 'long string', // or null if using a certificate
'clientCertificateThumbprint' => 'long string', // or null if using a clientSecret 'clientCertificatePrivateKey' => 'ultra long string', // or null if using a clientSecret
'serviceProvider' => 'Microsoft', // literal: also 'Google' or 'GoogleAPI' 'clientCertificateThumbprint' => 'long string', // or null if using a clientSecret
'authTypeSetting' => $mail->AuthType, // is set above - or insert here as 'XOAUTH2' 'serviceProvider' => 'Microsoft', // literal: also 'Google' or 'GoogleAPI'
'mailSMTPAddress' => 'me@mydomain.com', // Envelope/mailFrom/reverse-path From address 'authTypeSetting' => $mail->AuthType, // is set above - or insert here as 'XOAUTH2'
'refreshToken' => 'very long string', // null if grantType is 'client_credentials' 'mailSMTPAddress' => 'me@mydomain.com', // Envelope/mailFrom/reverse-path From address
'grantType' => 'authorization_code', // or 'client_credentials' '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
'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 'hostedDomain' => 'mydomain.com', // Any Google (and optional). Null for MSFT
'serviceAccountName' => 'string', // GoogleAPI service account only. Else null 'projectID' => 'string', // GoogleAPI only. Else null
'impersonate' => 'you@mydomain.com', // Google API service account only. Else null 'serviceAccountName' => 'string', // GoogleAPI service account only. Else null
// (Google Wspace email adddress, not @gmail) 'impersonate' => 'you@mydomain.com', // Google API service account only. Else null
] // (Google Wspace email adddress, not @gmail)
]
); );
$mail->setOAuth($oauthTokenProvider); // Pass OAuthTokenProvider to PHPMailer $mail->setOAuth($oauthTokenProvider); // Pass OAuthTokenProvider to PHPMailer
$mail->Host = 'smtp.office365.com'; // Set SMTP server (smtp.gmail.com for Gmail) $mail->Host = 'smtp.office365.com'; // Set SMTP server (smtp.gmail.com for Gmail)
@ -91,5 +90,5 @@ try {
$mail->send(); $mail->send();
echo 'Message has been sent'; echo 'Message has been sent';
} catch (Exception $e) { } catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}"; echo 'Message could not be sent. Mailer Error: ' . htmlspecialchars($mail->ErrorInfo, ENT_QUOTES);
} }