|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of SwiftMailer. |
|
5
|
|
|
* (c) 2004-2009 Chris Corbyn |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Handles XOAUTH2 authentication. |
|
13
|
|
|
* |
|
14
|
|
|
* Example: |
|
15
|
|
|
* <code> |
|
16
|
|
|
* $transport = (new Swift_SmtpTransport('smtp.gmail.com', 587, 'tls')) |
|
17
|
|
|
* ->setAuthMode('XOAUTH2') |
|
18
|
|
|
* ->setUsername('YOUR_EMAIL_ADDRESS') |
|
19
|
|
|
* ->setPassword('YOUR_ACCESS_TOKEN'); |
|
20
|
|
|
* </code> |
|
21
|
|
|
* |
|
22
|
|
|
* @author xu.li<[email protected]> |
|
23
|
|
|
* |
|
24
|
|
|
* @see https://developers.google.com/google-apps/gmail/xoauth2_protocol |
|
25
|
|
|
*/ |
|
26
|
|
|
class Swift_Transport_Esmtp_Auth_XOAuth2Authenticator implements Swift_Transport_Esmtp_Authenticator |
|
|
|
|
|
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* Get the name of the AUTH mechanism this Authenticator handles. |
|
30
|
|
|
* |
|
31
|
|
|
* @return string |
|
32
|
|
|
*/ |
|
33
|
|
|
public function getAuthKeyword() |
|
34
|
|
|
{ |
|
35
|
|
|
return 'XOAUTH2'; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Try to authenticate the user with $email and $token. |
|
40
|
|
|
* |
|
41
|
|
|
* @param Swift_Transport_SmtpAgent $agent |
|
42
|
|
|
* @param string $email |
|
43
|
|
|
* @param string $token |
|
44
|
|
|
* |
|
45
|
|
|
* @return bool |
|
46
|
|
|
*/ |
|
47
|
|
|
public function authenticate(Swift_Transport_SmtpAgent $agent, $email, $token) |
|
48
|
|
|
{ |
|
49
|
|
|
try { |
|
50
|
|
|
$param = $this->constructXOAuth2Params($email, $token); |
|
51
|
|
|
$agent->executeCommand('AUTH XOAUTH2 ' . $param . "\r\n", array(235)); |
|
52
|
|
|
|
|
53
|
|
|
return true; |
|
54
|
|
|
} catch (Swift_TransportException $e) { |
|
55
|
|
|
$agent->executeCommand("RSET\r\n", array(250)); |
|
56
|
|
|
|
|
57
|
|
|
return false; |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Construct the auth parameter. |
|
63
|
|
|
* |
|
64
|
|
|
* @see https://developers.google.com/google-apps/gmail/xoauth2_protocol#the_sasl_xoauth2_mechanism |
|
65
|
|
|
*/ |
|
66
|
|
|
protected function constructXOAuth2Params($email, $token) |
|
67
|
|
|
{ |
|
68
|
|
|
return base64_encode("user=$email\1auth=Bearer $token\1\1"); |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.