Skip to content

Commit 83b3869

Browse files
committed
RELEASE 1.5.0. Requires PHP >= 7.3.0. XOAUTH2 authentication has been added.
1 parent 641b36a commit 83b3869

File tree

6 files changed

+210
-12
lines changed

6 files changed

+210
-12
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
1.5.0 - 25-AUG-2022
2+
-------------------
3+
4+
* Requires PHP >= 7.3.0.
5+
* XOAUTH2 authentication has been added for enail accounts hosted by Google, Yahoo, Microsoft.
6+
7+
18
1.4.7 - 27-FEB-2021
29
-------------------
310

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
The MIT License (MIT)
22

3-
Copyright (c) 2012-2021 Ivan Tcholakov
3+
Copyright (c) 2012-2022 Ivan Tcholakov
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy of
66
this software and associated documentation files (the "Software"), to deal in

README.md

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66
A CodeIgniter compatible email-library powered by PHPMailer
77
===========================================================
88

9-
Version: 1.4.7
10-
Author: Ivan Tcholakov <ivantcholakov@gmail.com>, 2012-2021.
9+
Version: 1.5.0
10+
Author: Ivan Tcholakov <ivantcholakov@gmail.com>, 2012-2022.
1111
License: The MIT License (MIT), http://opensource.org/licenses/MIT
1212

13-
This library is compatible with CodeIgniter 3.1.x and PHP >= 5.5.0.
13+
This library is compatible with CodeIgniter 3.1.x and PHP >= 7.3.0.
1414

15-
Tested on CodeIgniter 3.1.11 (September 19th, 2019) and PHPMailer Version 6.2.0 (November 25th, 2020).
15+
Tested on CodeIgniter 3.1.13 (March 3rd, 2022) and PHPMailer Version 6.6.4 (August 22nd, 2022).
1616

1717
Links
1818
-----
@@ -117,7 +117,7 @@ $config['mailpath'] = '/usr/sbin/sendmail';
117117
$config['smtp_host'] = 'smtp.gmail.com';
118118
$config['smtp_auth'] = true; // Whether to use SMTP authentication, boolean TRUE/FALSE. If this option is omited or if it is NULL, then SMTP authentication is used when both $config['smtp_user'] and $config['smtp_pass'] are non-empty strings.
119119
$config['smtp_user'] = 'yourusername@gmail.com';
120-
$config['smtp_pass'] = 'yourpassword';
120+
$config['smtp_pass'] = ''; // Gmail disabled the so-called "Less Secured Applications", your Google password is not to be used directly, XOAUTH2 authentication will be used.
121121
$config['smtp_port'] = 587;
122122
$config['smtp_timeout'] = 30; // (in seconds)
123123
$config['smtp_crypto'] = 'tls'; // '' or 'tls' or 'ssl'
@@ -137,6 +137,20 @@ $config['bcc_batch_mode'] = false;
137137
$config['bcc_batch_size'] = 200;
138138
$config['encoding'] = '8bit'; // The body encoding. For CodeIgniter: '8bit' or '7bit'. For PHPMailer: '8bit', '7bit', 'binary', 'base64', or 'quoted-printable'.
139139

140+
// XOAUTH2 mechanism for authentication.
141+
// See https://github.com/PHPMailer/PHPMailer/wiki/Using-Gmail-with-XOAUTH2
142+
$config['oauth_type'] = 'xoauth2_google'; // XOAUTH2 authentication mechanism:
143+
// '' - disabled;
144+
// 'xoauth2' - custom implementation;
145+
// 'xoauth2_google' - Google provider;
146+
// 'xoauth2_yahoo' - Yahoo provider;
147+
// 'xoauth2_microsoft' - Microsoft provider.
148+
$config['oauth_instance'] = null; // Initialized instance of \PHPMailer\PHPMailer\OAuth (OAuthTokenProvider interface) that contains a custom token provider. Needed for 'xoauth2' custom implementation only.
149+
$config['oauth_user_email'] = ''; // If this option is an empty string or null, $config['smtp_user'] will be used.
150+
$config['oauth_client_id'] = '237644427849-g8d0pnkd1jh3idcjdbopvkse2hvj0tdp.apps.googleusercontent.com';
151+
$config['oauth_client_secret'] = 'mklHhrns6eF-qjwuiLpSB4DL';
152+
$config['oauth_refresh_token'] = '1/7Jt8_RHX86Pk09VTfQd4O_ZqKbmuV7HpMNz-rqJ4KdQMEudVrK5jSpoR30zcRFq6';
153+
140154
// DKIM Signing
141155
$config['dkim_domain'] = ''; // DKIM signing domain name, for exmple 'example.com'.
142156
$config['dkim_private'] = ''; // DKIM private key, set as a file path.

composer.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@
1313
}
1414
],
1515
"require": {
16-
"php": ">=5.5.0",
17-
"phpmailer/phpmailer": "^6.2.0"
16+
"php": ">=7.3.0",
17+
"phpmailer/phpmailer": "^6.6.4",
18+
"league/oauth2-google": "^4.0.0",
19+
"hayageek/oauth2-yahoo": "^2.0.5",
20+
"stevenmaguire/oauth2-microsoft": "^2.2.0"
1821
}
1922
}

config/email.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,20 @@
2626
$config['bcc_batch_size'] = 200;
2727
$config['encoding'] = '8bit'; // The body encoding. For CodeIgniter: '8bit' or '7bit'. For PHPMailer: '8bit', '7bit', 'binary', 'base64', or 'quoted-printable'.
2828

29+
// XOAUTH2 mechanism for authentication.
30+
// See https://github.com/PHPMailer/PHPMailer/wiki/Using-Gmail-with-XOAUTH2
31+
$config['oauth_type'] = ''; // XOAUTH2 authentication mechanism:
32+
// '' - disabled;
33+
// 'xoauth2' - custom implementation;
34+
// 'xoauth2_google' - Google provider;
35+
// 'xoauth2_yahoo' - Yahoo provider;
36+
// 'xoauth2_microsoft' - Microsoft provider.
37+
$config['oauth_instance'] = null; // Initialized instance of \PHPMailer\PHPMailer\OAuth (OAuthTokenProvider interface) that contains a custom token provider. Needed for 'xoauth2' custom implementation only.
38+
$config['oauth_user_email'] = ''; // If this option is an empty string or null, $config['smtp_user'] will be used.
39+
$config['oauth_client_id'] = '';
40+
$config['oauth_client_secret'] = '';
41+
$config['oauth_refresh_token'] = '';
42+
2943
// DKIM Signing
3044
// See https://yomotherboard.com/how-to-setup-email-server-dkim-keys/
3145
// See http://stackoverflow.com/questions/24463425/send-mail-in-phpmailer-using-dkim-keys

libraries/MY_Email.php

Lines changed: 164 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
/**
44
* 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.
77
* @license The MIT License (MIT), http://opensource.org/licenses/MIT
88
* @link https://github.com/ivantcholakov/codeigniter-phpmailer
99
*
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).
1212
*/
1313

1414
class MY_Email extends CI_Email {
@@ -46,6 +46,12 @@ class MY_Email extends CI_Email {
4646
'encoding' => '8bit',
4747
'smtp_auto_tls' => true,
4848
'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' => '',
4955
'dkim_domain' => '',
5056
'dkim_private' => '',
5157
'dkim_private_string' => '',
@@ -556,6 +562,100 @@ public function send($auto_clear = true) {
556562
//
557563
}
558564

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+
559659
$result = (bool) $this->phpmailer->send();
560660

561661
if ($result) {
@@ -640,6 +740,7 @@ public function set_mailpath($value) {
640740

641741
public function set_protocol($protocol = 'mail') {
642742

743+
$protocol = (string) $protocol;
643744
$protocol = in_array($protocol, self::$protocols, TRUE) ? strtolower($protocol) : 'mail';
644745

645746
$this->properties['protocol'] = $protocol;
@@ -1059,6 +1160,65 @@ public function set_smtp_conn_options($value) {
10591160
return $this;
10601161
}
10611162

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+
10621222
// DKIM signing, see https://github.com/ivantcholakov/codeigniter-phpmailer/issues/11
10631223

10641224
// PHPMailer: DKIM signing domain name, for exmple 'example.com'.

0 commit comments

Comments
 (0)