More cleanup

This commit is contained in:
Synchro 2016-01-28 13:47:36 +01:00
parent 9e0bbaa1ae
commit 8cf057efb7
7 changed files with 36 additions and 425 deletions

View File

@ -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)

View File

@ -1,166 +1,30 @@
<?php
/**
* This example shows settings to use when sending via Google's Gmail servers using XOAUTH2.
*/
namespace PHPMailer\PHPMailer;
//SMTP needs accurate times, and the PHP time zone MUST be set
//This should be done in your php.ini, but this is how to do it if you don't have access to that
date_default_timezone_set('Etc/UTC');
require '../vendor/autoload.php';
//Create a new PHPMailer instance
$mail = new PHPMailerOAuth;
//Tell PHPMailer to use SMTP
$mail = new PHPMailer;
$mail->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]://<yourdomain>/<folder>/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!";
}

View File

@ -1,96 +1,48 @@
<?php
/**
* This example shows settings to use when sending via Outlook/Microsoft Live servers using XOAUTH2.
*/
namespace PHPMailer\PHPMailer;
date_default_timezone_set('Etc/UTC');
require '../vendor/autoload.php';
//Load dependencies from composer
//If this causes an error, run 'composer install'
require '../vendor/autoload.php';
//Create a new Microsoft-specific PHPMailer instance
$mail = new PHPMailerOAuthMicrosoft;
$mail = new PHPMailer;
$mail->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!";
}

View File

@ -1,94 +1,45 @@
<?php
/**
* This example shows settings to use when sending via Yahoo servers using XOAUTH2.
*/
namespace PHPMailer\PHPMailer;
date_default_timezone_set('Etc/UTC');
require '../vendor/autoload.php';
//Load dependencies from composer
//If this causes an error, run 'composer install'
require '../vendor/autoload.php';
//Create a new Yahoo-specific PHPMailer instance
$mail = new PHPMailerOAuthYahoo;
$mail = new PHPMailer;
$mail->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!';
}

View File

@ -28,9 +28,12 @@ use League\OAuth2\Client\Provider\Google as LeagueGoogle;
* @author Marcus Bointon (@Synchro) <phpmailer@synchromedia.co.uk>
* @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)) {

View File

@ -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;
}
}

View File

@ -1,171 +0,0 @@
<?php
/**
* PHPMailer - PHP email creation and transport class.
* PHP Version 5.4
* @package PHPMailer
* @link https://github.com/PHPMailer/PHPMailer/ The PHPMailer GitHub project
* @author Marcus Bointon (Synchro/coolbru) <phpmailer@synchromedia.co.uk>
* @author Jim Jagielski (jimjag) <jimjag@gmail.com>
* @author Andy Prevost (codeworxtech) <codeworxtech@users.sourceforge.net>
* @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) <phpmailer@synchromedia.co.uk>
*/
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;
}
}