3 * PHPMailer - PHP email creation and transport class.
6 * @link https://github.com/PHPMailer/PHPMailer/ The PHPMailer GitHub project
7 * @author Marcus Bointon (Synchro/coolbru) <phpmailer@synchromedia.co.uk>
8 * @author Jim Jagielski (jimjag) <jimjag@gmail.com>
9 * @author Andy Prevost (codeworxtech) <codeworxtech@users.sourceforge.net>
10 * @author Brent R. Matzelle (original founder)
11 * @copyright 2012 - 2014 Marcus Bointon
12 * @copyright 2010 - 2012 Jim Jagielski
13 * @copyright 2004 - 2009 Andy Prevost
14 * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
15 * @note This program is distributed in the hope that it will be useful - WITHOUT
16 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 * FITNESS FOR A PARTICULAR PURPOSE.
21 * PHPMailerOAuth - PHPMailer subclass adding OAuth support.
23 * @author @sherryl4george
24 * @author Marcus Bointon (@Synchro) <phpmailer@synchromedia.co.uk>
26 class PHPMailerOAuth
extends PHPMailer
29 * The OAuth user's email address
32 public $oauthUserEmail = '';
35 * The OAuth refresh token
38 public $oauthRefreshToken = '';
44 public $oauthClientId = '';
47 * The OAuth client secret
50 public $oauthClientSecret = '';
53 * An instance of the PHPMailerOAuthGoogle class.
54 * @var PHPMailerOAuthGoogle
57 protected $oauth = null;
60 * Get a PHPMailerOAuthGoogle instance to use.
61 * @return PHPMailerOAuthGoogle
63 public function getOAUTHInstance()
65 if (!is_object($this->oauth
)) {
66 $this->oauth
= new PHPMailerOAuthGoogle(
67 $this->oauthUserEmail
,
68 $this->oauthClientSecret
,
70 $this->oauthRefreshToken
77 * Initiate a connection to an SMTP server.
78 * Overrides the original smtpConnect method to add support for OAuth.
79 * @param array $options An array of options compatible with stream_context_create()
84 public function smtpConnect($options = array())
86 if (is_null($this->smtp
)) {
87 $this->smtp
= $this->getSMTPInstance();
90 if (is_null($this->oauth
)) {
91 $this->oauth
= $this->getOAUTHInstance();
95 if ($this->smtp
->connected()) {
99 $this->smtp
->setTimeout($this->Timeout
);
100 $this->smtp
->setDebugLevel($this->SMTPDebug
);
101 $this->smtp
->setDebugOutput($this->Debugoutput
);
102 $this->smtp
->setVerp($this->do_verp
);
103 $hosts = explode(';', $this->Host
);
104 $lastexception = null;
106 foreach ($hosts as $hostentry) {
108 if (!preg_match('/^((ssl|tls):\/\/)*([a-zA-Z0-9\.-]*):?([0-9]*)$/', trim($hostentry), $hostinfo)) {
109 // Not a valid host entry
112 // $hostinfo[2]: optional ssl or tls prefix
113 // $hostinfo[3]: the hostname
114 // $hostinfo[4]: optional port number
115 // The host string prefix can temporarily override the current setting for SMTPSecure
116 // If it's not specified, the default value is used
118 $secure = $this->SMTPSecure
;
119 $tls = ($this->SMTPSecure
== 'tls');
120 if ('ssl' == $hostinfo[2] or ('' == $hostinfo[2] and 'ssl' == $this->SMTPSecure
)) {
122 $tls = false; // Can't have SSL and TLS at the same time
124 } elseif ($hostinfo[2] == 'tls') {
126 // tls doesn't use a prefix
129 //Do we need the OpenSSL extension?
130 $sslext = defined('OPENSSL_ALGO_SHA1');
131 if ('tls' === $secure or 'ssl' === $secure) {
132 //Check for an OpenSSL constant rather than using extension_loaded, which is sometimes disabled
134 throw new phpmailerException($this->lang('extension_missing').'openssl', self
::STOP_CRITICAL
);
137 $host = $hostinfo[3];
139 $tport = (integer)$hostinfo[4];
140 if ($tport > 0 and $tport < 65536) {
143 if ($this->smtp
->connect($prefix . $host, $port, $this->Timeout
, $options)) {
146 $hello = $this->Helo
;
148 $hello = $this->serverHostname();
150 $this->smtp
->hello($hello);
151 //Automatically enable TLS encryption if:
152 // * it's not disabled
153 // * we have openssl extension
154 // * we are not already using SSL
155 // * the server offers STARTTLS
156 if ($this->SMTPAutoTLS
and $sslext and $secure != 'ssl' and $this->smtp
->getServerExt('STARTTLS')) {
160 if (!$this->smtp
->startTLS()) {
161 throw new phpmailerException($this->lang('connect_host'));
163 // We must resend HELO after tls negotiation
164 $this->smtp
->hello($hello);
166 if ($this->SMTPAuth
) {
167 if (!$this->smtp
->authenticate(
176 throw new phpmailerException($this->lang('authenticate'));
180 } catch (phpmailerException
$exc) {
181 $lastexception = $exc;
182 $this->edebug($exc->getMessage());
183 // We must have connected, but then failed TLS or Auth, so close connection nicely
188 // If we get here, all connection attempts have failed, so close connection hard
189 $this->smtp
->close();
190 // As we've caught all exceptions, just report whatever the last one was
191 if ($this->exceptions
and !is_null($lastexception)) {
192 throw $lastexception;