From 8cf057efb7adb4695170ecf2ff78301a843f7867 Mon Sep 17 00:00:00 2001 From: Synchro Date: Thu, 28 Jan 2016 13:47:36 +0100 Subject: [PATCH] More cleanup --- changelog.md | 4 +- examples/xoauth2_gmail.phps | 142 +------------------------- examples/xoauth2_microsoft.phps | 50 +--------- examples/xoauth2_yahoo.phps | 51 +--------- src/OAuthProvider/Google.php | 5 +- src/PHPMailer.php | 38 ++++--- src/PHPMailerOAuth.php | 171 -------------------------------- 7 files changed, 36 insertions(+), 425 deletions(-) delete mode 100644 src/PHPMailerOAuth.php diff --git a/changelog.md b/changelog.md index c7ddafde..57d24f3a 100644 --- a/changelog.md +++ b/changelog.md @@ -21,10 +21,12 @@ This is a major update that breaks backwards compatibility. To emphasise that th * `POP3->Version` * All elements previously marked as deprecated have been removed (e.g. `ReturnPath`) * NTLM authentication removed - never worked anyway! + * `PHPMailer->Workstation` + * `PHPMailer->Realm` * `SMTP::authenticate` method signature changed * `parseAddresses()` is now static * `validateAddress()` is now called statically from `parseAddresses()` -* Extensive reworking of XOAUTH2, adding support for Google, Yahoo and Microsoft providers, thanks to @sherryl4george +* Extensive reworking of XOAUTH2, adding support for Google, Yahoo and Microsoft providers in the standard PHPMailer class, thanks to @sherryl4george * Fix extra line break in getSentMIMEMessage() ## Version 5.2.14 (Nov 1st 2015) diff --git a/examples/xoauth2_gmail.phps b/examples/xoauth2_gmail.phps index ae8c2155..5361b00e 100644 --- a/examples/xoauth2_gmail.phps +++ b/examples/xoauth2_gmail.phps @@ -1,166 +1,30 @@ isSMTP(); - - - -//Enable SMTP debugging - -// 0 = off (for production use) - -// 1 = client messages - -// 2 = client and server messages - $mail->SMTPDebug = 2; - - - -//Ask for HTML-friendly debug output - -//$mail->Debugoutput = 'html'; - - - -//Set the hostname of the mail server - $mail->Host = 'smtp.gmail.com'; - - - -//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission - $mail->Port = 587; - - - -//Set the encryption system to use - ssl (deprecated) or tls - $mail->SMTPSecure = 'tls'; - - - -//Whether to use SMTP authentication - $mail->SMTPAuth = true; - - - -//Set AuthTYpe - -$mail->AuthType = 'XOAUTH2'; - - - -//UserEmail to use for SMTP authentication - Use the same Email used in Google Developer Console - -$mail->oauthUserEmail = "marcus.bointon@gmail.com"; - - - -//Obtained From Google Developer Console - -$mail->oauthClientId = "237644427849-g8d0pnkd1jh3idcjdbopvkse2hvj0tdp.apps.googleusercontent.com"; - - - -//Obtained From Google Developer Console - -$mail->oauthClientSecret = "mklHhrns6eF-qjwuiLpSB4DL"; - - - -//Obtained By running get_oauth_token.php after setting up APP in Google Developer Console. - -//Set Redirect URI in Developer Console as [https/http]:////get_oauth_token.php - -// eg: http://localhost/phpmail/get_oauth_token.php - -$mail->oauthRefreshToken = "1/7Jt8_RHX86Pk09VTfQd4O_ZqKbmuV7HpMNz-rqJ4KdQMEudVrK5jSpoR30zcRFq6"; - - - -$mail->SMTPOptions = [ - - 'ssl' => [ - - 'verify_peer' => false, - - 'verify_peer_name' => false, - - 'allow_self_signed' => true - - ] - -]; - - - -//Set who the message is to be sent from - -$mail->setFrom('marcus.bointon@gmail.com', 'First Last'); - - - -//Set who the message is to be sent to - -$mail->addAddress('marcus@synchromedia.co.uk', 'John Doe'); - - - -//Set the subject line - $mail->Subject = 'PHPMailer GMail SMTP test'; - - - -//Read an HTML message body from an external file, convert referenced images to embedded, - -//convert HTML into a basic plain-text alternative body - $mail->msgHTML(file_get_contents('contents.html'), dirname(__FILE__)); - - +$mail->setFrom('sender@gmail.com', 'Test'); +$mail->addAddress('receiver@hotmail.com', 'Test'); //send the message, check for errors if (!$mail->send()) { - echo "Mailer Error: " . $mail->ErrorInfo; - } else { - echo "Message sent!"; - } - diff --git a/examples/xoauth2_microsoft.phps b/examples/xoauth2_microsoft.phps index 33ba1f86..ced46ed0 100644 --- a/examples/xoauth2_microsoft.phps +++ b/examples/xoauth2_microsoft.phps @@ -1,96 +1,48 @@ isSMTP(); - $mail->SMTPDebug = 2; - $mail->Host = 'smtp-mail.outlook.com'; - $mail->Port = 587; - $mail->SMTPSecure = 'tls'; - $mail->SMTPAuth = true; - - //Set AuthType - $mail->AuthType = 'XOAUTH2'; - - //User Email to use for SMTP authentication - Who authorised to access Outlook mail - $mail->oauthUserEmail = "sender@hotmail.com"; - - //Obtained From https://account.live.com/developers/applications/index - $mail->oauthClientId = "{YOUR_CLIENT_ID}"; - - //Obtained From https://account.live.com/developers/applications/index - $mail->oauthClientSecret = "{YOUR_CLIENT_SECRET}"; - - //Obtained By running get_oauth_token.php - $mail->oauthRefreshToken = "{OAUTH_REFRESH_TOKEN}"; - - $mail->setFrom('sender@hotmail.com', 'Test'); - $mail->addAddress('receiver@gmail.com', 'Test'); - $mail->Subject = 'PHPMailer hotmail XOAUTH2 SMTP test'; - $mail->msgHTML(file_get_contents('contents.html'), dirname(__FILE__)); - if (!$mail->send()) { - echo "Mailer Error: " . $mail->ErrorInfo; - } else { - echo "Message sent!"; - } - diff --git a/examples/xoauth2_yahoo.phps b/examples/xoauth2_yahoo.phps index 0918ca8c..eb142efa 100644 --- a/examples/xoauth2_yahoo.phps +++ b/examples/xoauth2_yahoo.phps @@ -1,94 +1,45 @@ isSMTP(); - $mail->SMTPDebug = 2; - $mail->Host = 'smtp.mail.yahoo.com'; - $mail->Port = 587; - $mail->SMTPSecure = 'tls'; - $mail->SMTPAuth = true; - - //Set AuthType - $mail->AuthType = 'XOAUTH2'; - - //User Email to use for SMTP authentication - Who authorised to send Yahoo mail - $mail->oauthUserEmail = 'sender@yahoo.com'; - - //Obtained From https://developer.yahoo.com/apps/ - $mail->oauthClientId = '{YAHOO_CLIENT_ID}'; - - //Obtained From https://developer.yahoo.com/apps/ - $mail->oauthClientSecret = '{CLIENT_SECRET}''; - - // eg: http://localhost/phpmail/get_oauth_token.php - $mail->oauthRefreshToken = '{REFRESH_TOKEN}'; - - $mail->setFrom('sender@yahoo.com', 'test'); - $mail->addAddress('receiver@gmail.com', 'test'); - $mail->Subject = 'PHPMailer Yahoo XOAUTH2 SMTP test'; - $mail->msgHTML(file_get_contents('contents.html'), dirname(__FILE__)); - if (!$mail->send()) { - echo 'Mailer Error: ' . $mail->ErrorInfo; - } else { - echo 'Message sent!'; - } - diff --git a/src/OAuthProvider/Google.php b/src/OAuthProvider/Google.php index 6952f0a5..874c47bb 100644 --- a/src/OAuthProvider/Google.php +++ b/src/OAuthProvider/Google.php @@ -28,9 +28,12 @@ use League\OAuth2\Client\Provider\Google as LeagueGoogle; * @author Marcus Bointon (@Synchro) * @link https://github.com/thephpleague/oauth2-client */ - class Google extends Base { + /** + * Return the OAuth provider implementation for this adaptor. + * @return League\OAuth2\Client\Provider\AbstractProvider + */ public function getProvider() { if (is_null($this->provider)) { diff --git a/src/PHPMailer.php b/src/PHPMailer.php index fc7113a7..94997a41 100644 --- a/src/PHPMailer.php +++ b/src/PHPMailer.php @@ -263,24 +263,17 @@ class PHPMailer /** * SMTP auth type. - * Options are LOGIN (default), PLAIN, NTLM, CRAM-MD5 + * Options are LOGIN (default), PLAIN, NTLM, CRAM-MD5, XOAUTH2 * @var string */ public $AuthType = ''; /** - * SMTP realm. - * Used for NTLM auth - * @var string + * An instance of an OAuthProvider\Base derivative class. + * @var OAuthProvider\Base + * @access protected */ - public $Realm = ''; - - /** - * SMTP workstation. - * Used for NTLM auth - * @var string - */ - public $Workstation = ''; + protected $oauth = null; /** * The SMTP server timeout in seconds. @@ -1578,8 +1571,7 @@ class PHPMailer $this->Username, $this->Password, $this->AuthType, - $this->Realm, - $this->Workstation + $this->oauth ) ) { throw new Exception($this->lang('authenticate')); @@ -3803,4 +3795,22 @@ class PHPMailer call_user_func_array($this->action_function, $params); } } + + /** + * Get the OAuthProvider instance. + * @return OAuthProvider\Base + */ + public function getOAuth() + { + return $this->oauth; + } + + /** + * Set an OAuthProvider instance. + * @param OAuthProvider\Base $oauth + */ + public function setOAuth(OAuthProvider\Base $oauth) + { + $this->oauth = $oauth; + } } diff --git a/src/PHPMailerOAuth.php b/src/PHPMailerOAuth.php deleted file mode 100644 index 374bae69..00000000 --- a/src/PHPMailerOAuth.php +++ /dev/null @@ -1,171 +0,0 @@ - - * @author Jim Jagielski (jimjag) - * @author Andy Prevost (codeworxtech) - * @author Brent R. Matzelle (original founder) - * @copyright 2012 - 2015 Marcus Bointon - * @copyright 2010 - 2012 Jim Jagielski - * @copyright 2004 - 2009 Andy Prevost - * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License - * @note This program is distributed in the hope that it will be useful - WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. - */ - -namespace PHPMailer\PHPMailer; - -/** - * PHPMailerOAuth - PHPMailer subclass adding OAuth support. - * @package PHPMailer - * @author @sherryl4george - * @author Marcus Bointon (@Synchro) - */ -class PHPMailerOAuth extends PHPMailer -{ - /** - * An instance of an OAuthProvider\Base derivative class. - * @var OAuthProvider\Base - * @access protected - */ - protected $oauth = null; - - /** - * Get an OAuthProvider instance to use. - * @return OAuthProvider\Base - */ - public function getOAuth() - { - return $this->oauth; - } - - public function setOAuth(OAuthProvider\Base $oauth) - { - $this->oauth = $oauth; - } - - /** - * Initiate a connection to an SMTP server. - * Overrides the original smtpConnect method to add support for OAuth. - * @param array $options An array of options compatible with stream_context_create() - * @return bool - * @throws null - * @throws Exception - * @uses SMTP - * @access public - */ - public function smtpConnect($options = []) - { - if (is_null($this->smtp)) { - $this->smtp = $this->getSMTPInstance(); - } - - if (is_null($this->oauth)) { - $this->oauth = $this->getOAuth(); - } - - // Already connected? - if ($this->smtp->connected()) { - return true; - } - - $this->smtp->setTimeout($this->Timeout); - $this->smtp->setDebugLevel($this->SMTPDebug); - $this->smtp->setDebugOutput($this->Debugoutput); - $this->smtp->setVerp($this->do_verp); - $hosts = explode(';', $this->Host); - $lastexception = null; - - foreach ($hosts as $hostentry) { - $hostinfo = []; - if (!preg_match('/^((ssl|tls):\/\/)*([a-zA-Z0-9\.-]*):?([0-9]*)$/', trim($hostentry), $hostinfo)) { - // Not a valid host entry - continue; - } - // $hostinfo[2]: optional ssl or tls prefix - // $hostinfo[3]: the hostname - // $hostinfo[4]: optional port number - // The host string prefix can temporarily override the current setting for SMTPSecure - // If it's not specified, the default value is used - $prefix = ''; - $secure = $this->SMTPSecure; - $tls = ($this->SMTPSecure == 'tls'); - if ('ssl' == $hostinfo[2] or ('' == $hostinfo[2] and 'ssl' == $this->SMTPSecure)) { - $prefix = 'ssl://'; - $tls = false; // Can't have SSL and TLS at the same time - $secure = 'ssl'; - } elseif ($hostinfo[2] == 'tls') { - $tls = true; - // tls doesn't use a prefix - $secure = 'tls'; - } - //Do we need the OpenSSL extension? - $sslext = defined('OPENSSL_ALGO_SHA1'); - if ('tls' === $secure or 'ssl' === $secure) { - //Check for an OpenSSL constant rather than using extension_loaded, which is sometimes disabled - if (!$sslext) { - throw new Exception($this->lang('extension_missing').'openssl', self::STOP_CRITICAL); - } - } - $host = $hostinfo[3]; - $port = $this->Port; - $tport = (integer)$hostinfo[4]; - if ($tport > 0 and $tport < 65536) { - $port = $tport; - } - if ($this->smtp->connect($prefix . $host, $port, $this->Timeout, $options)) { - try { - if ($this->Helo) { - $hello = $this->Helo; - } else { - $hello = $this->serverHostname(); - } - $this->smtp->hello($hello); - //Automatically enable TLS encryption if: - // * it's not disabled - // * we have openssl extension - // * we are not already using SSL - // * the server offers STARTTLS - if ($this->SMTPAutoTLS and $sslext and 'ssl' != $secure and $this->smtp->getServerExt('STARTTLS')) { - $tls = true; - } - if ($tls) { - if (!$this->smtp->startTLS()) { - throw new Exception($this->lang('connect_host')); - } - // We must resend HELO after tls negotiation - $this->smtp->hello($hello); - } - if ($this->SMTPAuth) { - if (!$this->smtp->authenticate( - $this->Username, - $this->Password, - $this->AuthType, - $this->oauth - ) - ) { - throw new Exception($this->lang('authenticate')); - } - } - return true; - } catch (Exception $exc) { - $lastexception = $exc; - $this->edebug($exc->getMessage()); - // We must have connected, but then failed TLS or Auth, so close connection nicely - $this->smtp->quit(); - } - } - } - // If we get here, all connection attempts have failed, so close connection hard - $this->smtp->close(); - // As we've caught all exceptions, just report whatever the last one was - if ($this->exceptions and !is_null($lastexception)) { - throw $lastexception; - } - return false; - } -}