|
2 | 2 |
|
3 | 3 | /**
|
4 | 4 | * CodeIgniter compatible email-library powered by PHPMailer.
|
5 |
| - * Version: 1.4.6 |
6 |
| - * @author Ivan Tcholakov <ivantcholakov@gmail.com>, 2012-2020. |
| 5 | + * Version: 1.5.0 |
| 6 | + * @author Ivan Tcholakov <ivantcholakov@gmail.com>, 2012-2022. |
7 | 7 | * @license The MIT License (MIT), http://opensource.org/licenses/MIT
|
8 | 8 | * @link https://github.com/ivantcholakov/codeigniter-phpmailer
|
9 | 9 | *
|
10 |
| - * Tested on CodeIgniter 3.1.11 (September 19th, 2019) and |
11 |
| - * PHPMailer Version 6.2.0 (November 25th, 2020). |
| 10 | + * Tested on CodeIgniter 3.1.13 (March 3rd, 2022) and |
| 11 | + * PHPMailer Version 6.6.4 (August 22nd, 2022). |
12 | 12 | */
|
13 | 13 |
|
14 | 14 | class MY_Email extends CI_Email {
|
@@ -46,6 +46,12 @@ class MY_Email extends CI_Email {
|
46 | 46 | 'encoding' => '8bit',
|
47 | 47 | 'smtp_auto_tls' => true,
|
48 | 48 | 'smtp_conn_options' => array(),
|
| 49 | + 'oauth_type' => '', |
| 50 | + 'oauth_instance' => null, |
| 51 | + 'oauth_user_email' => '', |
| 52 | + 'oauth_client_id' => '', |
| 53 | + 'oauth_client_secret' => '', |
| 54 | + 'oauth_refresh_token' => '', |
49 | 55 | 'dkim_domain' => '',
|
50 | 56 | 'dkim_private' => '',
|
51 | 57 | 'dkim_private_string' => '',
|
@@ -556,6 +562,100 @@ public function send($auto_clear = true) {
|
556 | 562 | //
|
557 | 563 | }
|
558 | 564 |
|
| 565 | + switch ($this->oauth_type) { |
| 566 | + |
| 567 | + case 'xoauth2': |
| 568 | + |
| 569 | + $this->phpmailer->AuthType = 'XOAUTH2'; |
| 570 | + |
| 571 | + $this->phpmailer->setOAuth($this->oauth_instance); |
| 572 | + |
| 573 | + break; |
| 574 | + |
| 575 | + case 'xoauth2_google': |
| 576 | + |
| 577 | + $this->phpmailer->AuthType = 'XOAUTH2'; |
| 578 | + |
| 579 | + $provider = new \League\OAuth2\Client\Provider\Google( |
| 580 | + array( |
| 581 | + 'clientId' => $this->oauth_client_id, |
| 582 | + 'clientSecret' => $this->oauth_client_secret, |
| 583 | + ) |
| 584 | + ); |
| 585 | + |
| 586 | + $this->phpmailer->setOAuth(new \PHPMailer\PHPMailer\OAuth( |
| 587 | + array( |
| 588 | + 'provider' => $provider, |
| 589 | + 'clientId' => $this->oauth_client_id, |
| 590 | + 'clientSecret' => $this->oauth_client_secret, |
| 591 | + 'refreshToken' => $this->oauth_refresh_token, |
| 592 | + 'userName' => $this->oauth_user_email != '' ? $this->oauth_user_email : $this->smtp_user, |
| 593 | + ) |
| 594 | + ) |
| 595 | + ); |
| 596 | + |
| 597 | + break; |
| 598 | + |
| 599 | + case 'xoauth2_yahoo': |
| 600 | + |
| 601 | + $this->phpmailer->AuthType = 'XOAUTH2'; |
| 602 | + |
| 603 | + $provider = new \Hayageek\OAuth2\Client\Provider\Yahoo( |
| 604 | + array( |
| 605 | + 'clientId' => $this->oauth_client_id, |
| 606 | + 'clientSecret' => $this->oauth_client_secret, |
| 607 | + ) |
| 608 | + ); |
| 609 | + |
| 610 | + $this->phpmailer->setOAuth(new \PHPMailer\PHPMailer\OAuth( |
| 611 | + array( |
| 612 | + 'provider' => $provider, |
| 613 | + 'clientId' => $this->oauth_client_id, |
| 614 | + 'clientSecret' => $this->oauth_client_secret, |
| 615 | + 'refreshToken' => $this->oauth_refresh_token, |
| 616 | + 'userName' => $this->oauth_user_email != '' ? $this->oauth_user_email : $this->smtp_user, |
| 617 | + ) |
| 618 | + ) |
| 619 | + ); |
| 620 | + |
| 621 | + break; |
| 622 | + |
| 623 | + case 'xoauth2_microsoft': |
| 624 | + |
| 625 | + $this->phpmailer->AuthType = 'XOAUTH2'; |
| 626 | + |
| 627 | + $provider = new \Stevenmaguire\OAuth2\Client\Provider\Microsoft( |
| 628 | + array( |
| 629 | + 'clientId' => $this->oauth_client_id, |
| 630 | + 'clientSecret' => $this->oauth_client_secret, |
| 631 | + ) |
| 632 | + ); |
| 633 | + |
| 634 | + $this->phpmailer->setOAuth(new \PHPMailer\PHPMailer\OAuth( |
| 635 | + array( |
| 636 | + 'provider' => $provider, |
| 637 | + 'clientId' => $this->oauth_client_id, |
| 638 | + 'clientSecret' => $this->oauth_client_secret, |
| 639 | + 'refreshToken' => $this->oauth_refresh_token, |
| 640 | + 'userName' => $this->oauth_user_email != '' ? $this->oauth_user_email : $this->smtp_user, |
| 641 | + ) |
| 642 | + ) |
| 643 | + ); |
| 644 | + |
| 645 | + break; |
| 646 | + |
| 647 | + default: |
| 648 | + |
| 649 | + $this->phpmailer->AuthType = ''; |
| 650 | + |
| 651 | + $reflection = new \ReflectionClass($this->phpmailer); |
| 652 | + $property = $reflection->getProperty('oauth'); |
| 653 | + $property->setAccessible(true); |
| 654 | + $property->setValue($this->phpmailer, null); |
| 655 | + |
| 656 | + break; |
| 657 | + } |
| 658 | + |
559 | 659 | $result = (bool) $this->phpmailer->send();
|
560 | 660 |
|
561 | 661 | if ($result) {
|
@@ -640,6 +740,7 @@ public function set_mailpath($value) {
|
640 | 740 |
|
641 | 741 | public function set_protocol($protocol = 'mail') {
|
642 | 742 |
|
| 743 | + $protocol = (string) $protocol; |
643 | 744 | $protocol = in_array($protocol, self::$protocols, TRUE) ? strtolower($protocol) : 'mail';
|
644 | 745 |
|
645 | 746 | $this->properties['protocol'] = $protocol;
|
@@ -1059,6 +1160,65 @@ public function set_smtp_conn_options($value) {
|
1059 | 1160 | return $this;
|
1060 | 1161 | }
|
1061 | 1162 |
|
| 1163 | + // XOAUTH2 settings. |
| 1164 | + |
| 1165 | + public function set_oauth_type($value) { |
| 1166 | + |
| 1167 | + $value = strtolower(trim((string) $value)); |
| 1168 | + |
| 1169 | + if ($value != '' && strpos($value, 'xoauth2') === false) { |
| 1170 | + $value = 'xoauth2'; |
| 1171 | + } |
| 1172 | + |
| 1173 | + $this->properties['oauth_type'] = $value; |
| 1174 | + |
| 1175 | + return $this; |
| 1176 | + } |
| 1177 | + |
| 1178 | + public function set_oauth_instance($value) { |
| 1179 | + |
| 1180 | + // An object that implements PHPMailer OAuthTokenProvider interface or null is expected here. |
| 1181 | + $this->properties['oauth_instance'] = $value; |
| 1182 | + |
| 1183 | + return $this; |
| 1184 | + } |
| 1185 | + |
| 1186 | + public function set_oauth_user_email($value) { |
| 1187 | + |
| 1188 | + $value = (string) $value; |
| 1189 | + |
| 1190 | + $this->properties['oauth_user_email'] = $value; |
| 1191 | + |
| 1192 | + return $this; |
| 1193 | + } |
| 1194 | + |
| 1195 | + public function set_oauth_client_id($value) { |
| 1196 | + |
| 1197 | + $value = (string) $value; |
| 1198 | + |
| 1199 | + $this->properties['oauth_client_id'] = $value; |
| 1200 | + |
| 1201 | + return $this; |
| 1202 | + } |
| 1203 | + |
| 1204 | + public function set_oauth_client_secret($value) { |
| 1205 | + |
| 1206 | + $value = (string) $value; |
| 1207 | + |
| 1208 | + $this->properties['oauth_client_secret'] = $value; |
| 1209 | + |
| 1210 | + return $this; |
| 1211 | + } |
| 1212 | + |
| 1213 | + public function set_oauth_refresh_token($value) { |
| 1214 | + |
| 1215 | + $value = (string) $value; |
| 1216 | + |
| 1217 | + $this->properties['oauth_refresh_token'] = $value; |
| 1218 | + |
| 1219 | + return $this; |
| 1220 | + } |
| 1221 | + |
1062 | 1222 | // DKIM signing, see https://github.com/ivantcholakov/codeigniter-phpmailer/issues/11
|
1063 | 1223 |
|
1064 | 1224 | // PHPMailer: DKIM signing domain name, for exmple 'example.com'.
|
|
0 commit comments