-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
/
sendoauth2.phps
94 lines (78 loc) · 4.91 KB
/
sendoauth2.phps
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?php
/**
* The SendOauth2 wrapper supports OAuth2 and Basic authorization/authentication for
* Microsoft 365 Exchange email and Google Gmail. Both TheLeague's Google provider + client
* and Google's 'official' GoogleAPI client are supported. The wrapper supports any authentication
* mechanism provided by these systems: authorization_code grant and client_credentials grant
* (aka Google 'service accounts'), client secrets and X.509 certificates, $_SESSION 'state'
* and PKCE code exchanges, and creation on the fly of GoogleAPI's .json credentials files.
* Appropriate scopes (client permissions) and 'provider' overrides are added automatically.
*
* The wrapper is installed with Composer from the decomplexity/SendOauth2 repo; see its README.
*
* 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';
// Set timezone for SMTP
date_default_timezone_set('Etc/UTC');
// Create an instance; passing `true` enables exceptions
$mail = new PHPMailer(true);
try {
// Server settings
$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
$mail->AuthType = 'XOAUTH2'; // Set AuthType to use XOAUTH2 ('LOGIN' for Basic auth)
// Sender and recipients
$mail->setFrom('[email protected]', 'Mailer'); // 'Header' From address with optional sender name
$mail->addAddress('[email protected]', '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' => '[email protected]', // 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' => '[email protected]', // Google API service account only. Else null
// (Google Wspace email adddress, not @gmail)
]
);
$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 <b>in bold!</b>';
$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);
}