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 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.
*/
// 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
// error_reporting(E_ALL);
// ini_set("display_errors", 1);
// Load Composer's autoloader
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
date_default_timezone_set('Etc/UTC');
@ -39,8 +37,8 @@ $mail = new PHPMailer(true);
try {
// Server settings
$mail->SMTPDebug = SMTP::DEBUG_OFF; // Set DEBUG_LOWLEVEL for SMTP diagnostics
$mail->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
@ -57,7 +55,8 @@ try {
*/
$oauthTokenProvider = new SendOauth2B(
['mail' => $mail, // PHPMailer instance
[
'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
@ -91,5 +90,5 @@ try {
$mail->send();
echo 'Message has been sent';
} 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);
}