Skip to content

Commit

Permalink
RELEASE 1.5.0. Requires PHP >= 7.3.0. XOAUTH2 authentication has been…
Browse files Browse the repository at this point in the history
… added.
  • Loading branch information
ivantcholakov committed Aug 25, 2022
1 parent 641b36a commit 83b3869
Show file tree
Hide file tree
Showing 6 changed files with 210 additions and 12 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
1.5.0 - 25-AUG-2022
-------------------

* Requires PHP >= 7.3.0.
* XOAUTH2 authentication has been added for enail accounts hosted by Google, Yahoo, Microsoft.


1.4.7 - 27-FEB-2021
-------------------

Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2012-2021 Ivan Tcholakov
Copyright (c) 2012-2022 Ivan Tcholakov

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
Expand Down
24 changes: 19 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
A CodeIgniter compatible email-library powered by PHPMailer
===========================================================

Version: 1.4.7
Author: Ivan Tcholakov <[email protected]>, 2012-2021.
Version: 1.5.0
Author: Ivan Tcholakov <[email protected]>, 2012-2022.
License: The MIT License (MIT), http://opensource.org/licenses/MIT

This library is compatible with CodeIgniter 3.1.x and PHP >= 5.5.0.
This library is compatible with CodeIgniter 3.1.x and PHP >= 7.3.0.

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

Links
-----
Expand Down Expand Up @@ -117,7 +117,7 @@ $config['mailpath'] = '/usr/sbin/sendmail';
$config['smtp_host'] = 'smtp.gmail.com';
$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.
$config['smtp_user'] = '[email protected]';
$config['smtp_pass'] = 'yourpassword';
$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.
$config['smtp_port'] = 587;
$config['smtp_timeout'] = 30; // (in seconds)
$config['smtp_crypto'] = 'tls'; // '' or 'tls' or 'ssl'
Expand All @@ -137,6 +137,20 @@ $config['bcc_batch_mode'] = false;
$config['bcc_batch_size'] = 200;
$config['encoding'] = '8bit'; // The body encoding. For CodeIgniter: '8bit' or '7bit'. For PHPMailer: '8bit', '7bit', 'binary', 'base64', or 'quoted-printable'.

// XOAUTH2 mechanism for authentication.
// See https://github.com/PHPMailer/PHPMailer/wiki/Using-Gmail-with-XOAUTH2
$config['oauth_type'] = 'xoauth2_google'; // XOAUTH2 authentication mechanism:
// '' - disabled;
// 'xoauth2' - custom implementation;
// 'xoauth2_google' - Google provider;
// 'xoauth2_yahoo' - Yahoo provider;
// 'xoauth2_microsoft' - Microsoft provider.
$config['oauth_instance'] = null; // Initialized instance of \PHPMailer\PHPMailer\OAuth (OAuthTokenProvider interface) that contains a custom token provider. Needed for 'xoauth2' custom implementation only.
$config['oauth_user_email'] = ''; // If this option is an empty string or null, $config['smtp_user'] will be used.
$config['oauth_client_id'] = '237644427849-g8d0pnkd1jh3idcjdbopvkse2hvj0tdp.apps.googleusercontent.com';
$config['oauth_client_secret'] = 'mklHhrns6eF-qjwuiLpSB4DL';
$config['oauth_refresh_token'] = '1/7Jt8_RHX86Pk09VTfQd4O_ZqKbmuV7HpMNz-rqJ4KdQMEudVrK5jSpoR30zcRFq6';

// DKIM Signing
$config['dkim_domain'] = ''; // DKIM signing domain name, for exmple 'example.com'.
$config['dkim_private'] = ''; // DKIM private key, set as a file path.
Expand Down
7 changes: 5 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@
}
],
"require": {
"php": ">=5.5.0",
"phpmailer/phpmailer": "^6.2.0"
"php": ">=7.3.0",
"phpmailer/phpmailer": "^6.6.4",
"league/oauth2-google": "^4.0.0",
"hayageek/oauth2-yahoo": "^2.0.5",
"stevenmaguire/oauth2-microsoft": "^2.2.0"
}
}
14 changes: 14 additions & 0 deletions config/email.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,20 @@
$config['bcc_batch_size'] = 200;
$config['encoding'] = '8bit'; // The body encoding. For CodeIgniter: '8bit' or '7bit'. For PHPMailer: '8bit', '7bit', 'binary', 'base64', or 'quoted-printable'.

// XOAUTH2 mechanism for authentication.
// See https://github.com/PHPMailer/PHPMailer/wiki/Using-Gmail-with-XOAUTH2
$config['oauth_type'] = ''; // XOAUTH2 authentication mechanism:
// '' - disabled;
// 'xoauth2' - custom implementation;
// 'xoauth2_google' - Google provider;
// 'xoauth2_yahoo' - Yahoo provider;
// 'xoauth2_microsoft' - Microsoft provider.
$config['oauth_instance'] = null; // Initialized instance of \PHPMailer\PHPMailer\OAuth (OAuthTokenProvider interface) that contains a custom token provider. Needed for 'xoauth2' custom implementation only.
$config['oauth_user_email'] = ''; // If this option is an empty string or null, $config['smtp_user'] will be used.
$config['oauth_client_id'] = '';
$config['oauth_client_secret'] = '';
$config['oauth_refresh_token'] = '';

// DKIM Signing
// See https://yomotherboard.com/how-to-setup-email-server-dkim-keys/
// See http://stackoverflow.com/questions/24463425/send-mail-in-phpmailer-using-dkim-keys
Expand Down
168 changes: 164 additions & 4 deletions libraries/MY_Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

/**
* CodeIgniter compatible email-library powered by PHPMailer.
* Version: 1.4.6
* @author Ivan Tcholakov <[email protected]>, 2012-2020.
* Version: 1.5.0
* @author Ivan Tcholakov <[email protected]>, 2012-2022.
* @license The MIT License (MIT), http://opensource.org/licenses/MIT
* @link https://github.com/ivantcholakov/codeigniter-phpmailer
*
* Tested on CodeIgniter 3.1.11 (September 19th, 2019) and
* PHPMailer Version 6.2.0 (November 25th, 2020).
* Tested on CodeIgniter 3.1.13 (March 3rd, 2022) and
* PHPMailer Version 6.6.4 (August 22nd, 2022).
*/

class MY_Email extends CI_Email {
Expand Down Expand Up @@ -46,6 +46,12 @@ class MY_Email extends CI_Email {
'encoding' => '8bit',
'smtp_auto_tls' => true,
'smtp_conn_options' => array(),
'oauth_type' => '',
'oauth_instance' => null,
'oauth_user_email' => '',
'oauth_client_id' => '',
'oauth_client_secret' => '',
'oauth_refresh_token' => '',
'dkim_domain' => '',
'dkim_private' => '',
'dkim_private_string' => '',
Expand Down Expand Up @@ -556,6 +562,100 @@ public function send($auto_clear = true) {
//
}

switch ($this->oauth_type) {

case 'xoauth2':

$this->phpmailer->AuthType = 'XOAUTH2';

$this->phpmailer->setOAuth($this->oauth_instance);

break;

case 'xoauth2_google':

$this->phpmailer->AuthType = 'XOAUTH2';

$provider = new \League\OAuth2\Client\Provider\Google(
array(
'clientId' => $this->oauth_client_id,
'clientSecret' => $this->oauth_client_secret,
)
);

$this->phpmailer->setOAuth(new \PHPMailer\PHPMailer\OAuth(
array(
'provider' => $provider,
'clientId' => $this->oauth_client_id,
'clientSecret' => $this->oauth_client_secret,
'refreshToken' => $this->oauth_refresh_token,
'userName' => $this->oauth_user_email != '' ? $this->oauth_user_email : $this->smtp_user,
)
)
);

break;

case 'xoauth2_yahoo':

$this->phpmailer->AuthType = 'XOAUTH2';

$provider = new \Hayageek\OAuth2\Client\Provider\Yahoo(
array(
'clientId' => $this->oauth_client_id,
'clientSecret' => $this->oauth_client_secret,
)
);

$this->phpmailer->setOAuth(new \PHPMailer\PHPMailer\OAuth(
array(
'provider' => $provider,
'clientId' => $this->oauth_client_id,
'clientSecret' => $this->oauth_client_secret,
'refreshToken' => $this->oauth_refresh_token,
'userName' => $this->oauth_user_email != '' ? $this->oauth_user_email : $this->smtp_user,
)
)
);

break;

case 'xoauth2_microsoft':

$this->phpmailer->AuthType = 'XOAUTH2';

$provider = new \Stevenmaguire\OAuth2\Client\Provider\Microsoft(
array(
'clientId' => $this->oauth_client_id,
'clientSecret' => $this->oauth_client_secret,
)
);

$this->phpmailer->setOAuth(new \PHPMailer\PHPMailer\OAuth(
array(
'provider' => $provider,
'clientId' => $this->oauth_client_id,
'clientSecret' => $this->oauth_client_secret,
'refreshToken' => $this->oauth_refresh_token,
'userName' => $this->oauth_user_email != '' ? $this->oauth_user_email : $this->smtp_user,
)
)
);

break;

default:

$this->phpmailer->AuthType = '';

$reflection = new \ReflectionClass($this->phpmailer);
$property = $reflection->getProperty('oauth');
$property->setAccessible(true);
$property->setValue($this->phpmailer, null);

break;
}

$result = (bool) $this->phpmailer->send();

if ($result) {
Expand Down Expand Up @@ -640,6 +740,7 @@ public function set_mailpath($value) {

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

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

$this->properties['protocol'] = $protocol;
Expand Down Expand Up @@ -1059,6 +1160,65 @@ public function set_smtp_conn_options($value) {
return $this;
}

// XOAUTH2 settings.

public function set_oauth_type($value) {

$value = strtolower(trim((string) $value));

if ($value != '' && strpos($value, 'xoauth2') === false) {
$value = 'xoauth2';
}

$this->properties['oauth_type'] = $value;

return $this;
}

public function set_oauth_instance($value) {

// An object that implements PHPMailer OAuthTokenProvider interface or null is expected here.
$this->properties['oauth_instance'] = $value;

return $this;
}

public function set_oauth_user_email($value) {

$value = (string) $value;

$this->properties['oauth_user_email'] = $value;

return $this;
}

public function set_oauth_client_id($value) {

$value = (string) $value;

$this->properties['oauth_client_id'] = $value;

return $this;
}

public function set_oauth_client_secret($value) {

$value = (string) $value;

$this->properties['oauth_client_secret'] = $value;

return $this;
}

public function set_oauth_refresh_token($value) {

$value = (string) $value;

$this->properties['oauth_refresh_token'] = $value;

return $this;
}

// DKIM signing, see https://github.com/ivantcholakov/codeigniter-phpmailer/issues/11

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

1 comment on commit 83b3869

@arabcash001
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WhatsApp Image 2022-09-12 at 1 56 45 PM
please i need a mailer like this someone help

Please sign in to comment.