Merge branch 'master' of ssh://github.com/PHPMailer/PHPMailer
This commit is contained in:
commit
14ace3da7b
|
|
@ -4,3 +4,4 @@ test/testbootstrap.php
|
|||
test/*.pem
|
||||
.idea
|
||||
build/
|
||||
vendor/
|
||||
|
|
|
|||
|
|
@ -16,7 +16,7 @@ Build status: [](h
|
|||
- Send emails with multiple TOs, CCs, BCCs and REPLY-TOs
|
||||
- Multipart/alternative emails for mail clients that do not read HTML email
|
||||
- Support for UTF-8 content and 8bit, base64, binary, and quoted-printable encodings
|
||||
- SMTP authentication with LOGIN, PLAIN, NTLM and CRAM-MD5 mechanisms over SSL and TLS transports
|
||||
- SMTP authentication with LOGIN, PLAIN, NTLM, CRAM-MD5 and Google's XOAUTH2 mechanisms over SSL and TLS transports
|
||||
- Error messages in 47 languages!
|
||||
- DKIM and S/MIME signing support
|
||||
- Compatible with PHP 5.0 and later
|
||||
|
|
@ -50,15 +50,19 @@ or
|
|||
composer require phpmailer/phpmailer
|
||||
```
|
||||
|
||||
If you want to use the Gmail XOAUTH2 authentication class, you will also need to add a dependency on the `league/oauth2-client` package.
|
||||
|
||||
Alternatively, copy the contents of the PHPMailer folder into somewhere that's in your PHP `include_path` setting. If you don't speak git or just want a tarball, click the 'zip' button at the top of the page in GitHub.
|
||||
|
||||
If you're not using composer's autoloader, PHPMailer provides an SPL-compatible autoloader, and that is the preferred way of loading the library - just `require '/path/to/PHPMailerAutoload.php';` and everything should work. The autoloader does not throw errors if it can't find classes so it prepends itself to the SPL list, allowing your own (or your framework's) autoloader to catch errors. SPL autoloading was introduced in PHP 5.1.0, so if you are using a version older than that you will need to require/include each class manually.
|
||||
|
||||
PHPMailer does *not* declare a namespace because namespaces were only introduced in PHP 5.3.
|
||||
|
||||
If you want to use Google's XOAUTH2 authentication mechanism, you need to be running at least PHP 5.4, and load the dependencies listed in `composer.json`.
|
||||
|
||||
### Minimal installation
|
||||
|
||||
While installing the entire package manually or with composer is simple, convenient and reliable, you may want to include only vital files in your project. At the very least you will need [class.phpmailer.php](class.phpmailer.php). If you're using SMTP, you'll need [class.smtp.php](class.smtp.php), and if you're using POP-before SMTP, you'll need [class.pop3.php](class.pop3.php). For all of these, we recommend you use [the autoloader](PHPMailerAutoload.php) too as otherwise you will either have to `require` all classes manually or use some other autoloader. You can skip the [language](language/) folder if you're not showing errors to users and can make do with English-only errors. You may need the additional classes in the [extras](extras/) folder if you are using those features, including NTLM authentication and ics generation.
|
||||
While installing the entire package manually or with composer is simple, convenient and reliable, you may want to include only vital files in your project. At the very least you will need [class.phpmailer.php](class.phpmailer.php). If you're using SMTP, you'll need [class.smtp.php](class.smtp.php), and if you're using POP-before SMTP, you'll need [class.pop3.php](class.pop3.php). For all of these, we recommend you use [the autoloader](PHPMailerAutoload.php) too as otherwise you will either have to `require` all classes manually or use some other autoloader. You can skip the [language](language/) folder if you're not showing errors to users and can make do with English-only errors. You may need the additional classes in the [extras](extras/) folder if you are using those features, including NTLM authentication and ics generation. If you're using Google XOAUTH2 you will need `class.phpmaileroauth.php` and `class.oauth.php` classes too, as well as the composer dependencies.
|
||||
|
||||
## A Simple Example
|
||||
|
||||
|
|
|
|||
|
|
@ -1,11 +1,18 @@
|
|||
# ChangeLog
|
||||
|
||||
## Version 5.2.12 (Sep 1st 2015)
|
||||
* Fix incorrect composer package dependencies
|
||||
* Skip existing embedded image `cid`s in `msgHTML`
|
||||
|
||||
## Version 5.2.11 (Aug 31st 2015)
|
||||
* Don't switch to quoted-printable for long lines if already using base64
|
||||
* Fixed Travis-CI config when run on PHP 7
|
||||
* Added Google XOAUTH2 authentication mechanism, thanks to @sherryl4george
|
||||
* Add address parser for RFC822-format addresses
|
||||
* Update MS Office MIME types
|
||||
* Don't convert line breaks when using quoted-printable encoding
|
||||
* Handle MS Exchange returning an invalid empty AUTH-type list in EHLO
|
||||
* Don't set name or filename properties on MIME parts that don't have one
|
||||
|
||||
## Version 5.2.10 (May 4th 2015)
|
||||
* Add custom header getter
|
||||
|
|
|
|||
|
|
@ -0,0 +1,45 @@
|
|||
<?php
|
||||
class OAuth
|
||||
{
|
||||
private $oauthUserEmail = '';
|
||||
private $oauthRefreshToken = '';
|
||||
private $oauthClientId = '';
|
||||
private $oauthClientSecret = '';
|
||||
|
||||
public function __construct(
|
||||
$UserEmail,
|
||||
$ClientSecret,
|
||||
$ClientId,
|
||||
$RefreshToken
|
||||
) {
|
||||
$this->oauthClientId = $ClientId;
|
||||
$this->oauthClientSecret = $ClientSecret;
|
||||
$this->oauthRefreshToken = $RefreshToken;
|
||||
$this->oauthUserEmail = $UserEmail;
|
||||
}
|
||||
|
||||
private function getProvider() {
|
||||
return new League\OAuth2\Client\Provider\Google([
|
||||
'clientId' => $this->oauthClientId,
|
||||
'clientSecret' => $this->oauthClientSecret
|
||||
]);
|
||||
}
|
||||
|
||||
private function getGrant()
|
||||
{
|
||||
return new \League\OAuth2\Client\Grant\RefreshToken();
|
||||
}
|
||||
|
||||
private function getToken()
|
||||
{
|
||||
$provider = $this->getProvider();
|
||||
$grant = $this->getGrant();
|
||||
return $provider->getAccessToken($grant, ['refresh_token' => $this->oauthRefreshToken]);
|
||||
}
|
||||
|
||||
public function getOauth64()
|
||||
{
|
||||
$token = $this->getToken();
|
||||
return base64_encode("user=" . $this->oauthUserEmail . "\001auth=Bearer " . $token . "\001\001");
|
||||
}
|
||||
}
|
||||
|
|
@ -31,7 +31,7 @@ class PHPMailer
|
|||
* The PHPMailer Version number.
|
||||
* @type string
|
||||
*/
|
||||
public $Version = '5.2.10';
|
||||
public $Version = '5.2.12';
|
||||
|
||||
/**
|
||||
* Email priority.
|
||||
|
|
@ -444,7 +444,18 @@ class PHPMailer
|
|||
* @type string
|
||||
*/
|
||||
public $XMailer = '';
|
||||
|
||||
|
||||
/**
|
||||
* Only For XOAUTH - Google
|
||||
* Options: An empty string for PHPMailer default, Enter the email used to get access token
|
||||
* @type string
|
||||
*/
|
||||
// public $UserEmail = '';
|
||||
// public $RefreshToken = '';
|
||||
// public $ClientId = '';
|
||||
// public $ClientSecret = '';
|
||||
|
||||
|
||||
/**
|
||||
* An instance of the SMTP sender class.
|
||||
* @type SMTP
|
||||
|
|
@ -1373,7 +1384,7 @@ class PHPMailer
|
|||
if (is_null($this->smtp)) {
|
||||
$this->smtp = $this->getSMTPInstance();
|
||||
}
|
||||
|
||||
|
||||
// Already connected?
|
||||
if ($this->smtp->connected()) {
|
||||
return true;
|
||||
|
|
@ -1448,10 +1459,10 @@ class PHPMailer
|
|||
}
|
||||
if ($this->SMTPAuth) {
|
||||
if (!$this->smtp->authenticate(
|
||||
$this->Username,
|
||||
$this->Password,
|
||||
$this->AuthType,
|
||||
$this->Realm,
|
||||
$this->Username,
|
||||
$this->Password,
|
||||
$this->AuthType,
|
||||
$this->Realm,
|
||||
$this->Workstation
|
||||
)
|
||||
) {
|
||||
|
|
@ -2330,12 +2341,21 @@ class PHPMailer
|
|||
$cidUniq[$cid] = true;
|
||||
|
||||
$mime[] = sprintf('--%s%s', $boundary, $this->LE);
|
||||
$mime[] = sprintf(
|
||||
'Content-Type: %s; name="%s"%s',
|
||||
$type,
|
||||
$this->encodeHeader($this->secureHeader($name)),
|
||||
$this->LE
|
||||
);
|
||||
//Only include a filename property if we have one
|
||||
if (!empty($name)) {
|
||||
$mime[] = sprintf(
|
||||
'Content-Type: %s; name="%s"%s',
|
||||
$type,
|
||||
$this->encodeHeader($this->secureHeader($name)),
|
||||
$this->LE
|
||||
);
|
||||
} else {
|
||||
$mime[] = sprintf(
|
||||
'Content-Type: %s%s',
|
||||
$type,
|
||||
$this->LE
|
||||
);
|
||||
}
|
||||
// RFC1341 part 5 says 7bit is assumed if not specified
|
||||
if ($encoding != '7bit') {
|
||||
$mime[] = sprintf('Content-Transfer-Encoding: %s%s', $encoding, $this->LE);
|
||||
|
|
@ -2359,12 +2379,20 @@ class PHPMailer
|
|||
$this->LE . $this->LE
|
||||
);
|
||||
} else {
|
||||
$mime[] = sprintf(
|
||||
'Content-Disposition: %s; filename=%s%s',
|
||||
$disposition,
|
||||
$encoded_name,
|
||||
$this->LE . $this->LE
|
||||
);
|
||||
if (!empty($encoded_name)) {
|
||||
$mime[] = sprintf(
|
||||
'Content-Disposition: %s; filename=%s%s',
|
||||
$disposition,
|
||||
$encoded_name,
|
||||
$this->LE . $this->LE
|
||||
);
|
||||
} else {
|
||||
$mime[] = sprintf(
|
||||
'Content-Disposition: %s%s',
|
||||
$disposition,
|
||||
$this->LE . $this->LE
|
||||
);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$mime[] = $this->LE;
|
||||
|
|
@ -2801,7 +2829,7 @@ class PHPMailer
|
|||
$disposition = 'inline'
|
||||
) {
|
||||
// If a MIME type is not specified, try to work it out from the name
|
||||
if ($type == '') {
|
||||
if ($type == '' and !empty($name)) {
|
||||
$type = self::filenameToType($name);
|
||||
}
|
||||
|
||||
|
|
@ -3103,15 +3131,16 @@ class PHPMailer
|
|||
$data = rawurldecode($data);
|
||||
}
|
||||
$cid = md5($url) . '@phpmailer.0'; // RFC2392 S 2
|
||||
if ($this->addStringEmbeddedImage($data, $cid, '', 'base64', $match[1])) {
|
||||
if ($this->addStringEmbeddedImage($data, $cid, 'embed' . $imgindex, 'base64', $match[1])) {
|
||||
$message = str_replace(
|
||||
$images[0][$imgindex],
|
||||
$images[1][$imgindex] . '="cid:' . $cid . '"',
|
||||
$message
|
||||
);
|
||||
}
|
||||
} elseif (!preg_match('#^[A-z]+://#', $url)) {
|
||||
} elseif (substr($url, 0, 4) !== 'cid:' && !preg_match('#^[A-z]+://#', $url)) {
|
||||
// Do not change urls for absolute images (thanks to corvuscorax)
|
||||
// Do not change urls that are already inline images
|
||||
$filename = basename($url);
|
||||
$directory = dirname($url);
|
||||
if ($directory == '.') {
|
||||
|
|
|
|||
|
|
@ -0,0 +1,197 @@
|
|||
<?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 - 2014 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.
|
||||
*/
|
||||
|
||||
/**
|
||||
* PHPMailerOAuth - PHPMailer subclass adding OAuth support.
|
||||
* @package PHPMailer
|
||||
* @author @sherryl4george
|
||||
* @author Marcus Bointon (@Synchro) <phpmailer@synchromedia.co.uk>
|
||||
*/
|
||||
class PHPMailerOAuth extends PHPMailer
|
||||
{
|
||||
/**
|
||||
* The OAuth user's email address
|
||||
* @type string
|
||||
*/
|
||||
public $oauthUserEmail = '';
|
||||
|
||||
/**
|
||||
* The OAuth refresh token
|
||||
* @type string
|
||||
*/
|
||||
public $oauthRefreshToken = '';
|
||||
|
||||
/**
|
||||
* The OAuth client ID
|
||||
* @type string
|
||||
*/
|
||||
public $oauthClientId = '';
|
||||
|
||||
/**
|
||||
* The OAuth client secret
|
||||
* @type string
|
||||
*/
|
||||
public $oauthClientSecret = '';
|
||||
|
||||
/**
|
||||
* An instance of the OAuth class.
|
||||
* @type OAuth
|
||||
* @access protected
|
||||
*/
|
||||
protected $oauth = null;
|
||||
|
||||
/**
|
||||
* Get an OAuth instance to use.
|
||||
* @return OAuth
|
||||
*/
|
||||
public function getOAUTHInstance()
|
||||
{
|
||||
if (!is_object($this->oauth)) {
|
||||
$this->oauth = new OAuth(
|
||||
$this->oauthUserEmail,
|
||||
$this->oauthClientSecret,
|
||||
$this->oauthClientId,
|
||||
$this->oauthRefreshToken
|
||||
);
|
||||
}
|
||||
return $this->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()
|
||||
* @uses SMTP
|
||||
* @access public
|
||||
* @throws phpmailerException
|
||||
* @return boolean
|
||||
*/
|
||||
public function smtpConnect($options = array())
|
||||
{
|
||||
if (is_null($this->smtp)) {
|
||||
$this->smtp = $this->getSMTPInstance();
|
||||
}
|
||||
|
||||
if (is_null($this->oauth)) {
|
||||
$this->oauth = $this->getOAUTHInstance();
|
||||
}
|
||||
|
||||
// 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 = array();
|
||||
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 phpmailerException($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 $secure != 'ssl' and $this->smtp->getServerExt('STARTTLS')) {
|
||||
$tls = true;
|
||||
}
|
||||
if ($tls) {
|
||||
if (!$this->smtp->startTLS()) {
|
||||
throw new phpmailerException($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->Realm,
|
||||
$this->Workstation,
|
||||
$this->oauth
|
||||
)
|
||||
) {
|
||||
throw new phpmailerException($this->lang('authenticate'));
|
||||
}
|
||||
}
|
||||
return true;
|
||||
} catch (phpmailerException $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;
|
||||
}
|
||||
}
|
||||
|
|
@ -34,7 +34,7 @@ class POP3
|
|||
* @type string
|
||||
* @access public
|
||||
*/
|
||||
public $Version = '5.2.10';
|
||||
public $Version = '5.2.12';
|
||||
|
||||
/**
|
||||
* Default POP3 port number.
|
||||
|
|
|
|||
|
|
@ -30,7 +30,7 @@ class SMTP
|
|||
* The PHPMailer SMTP version number.
|
||||
* @type string
|
||||
*/
|
||||
const VERSION = '5.2.10';
|
||||
const VERSION = '5.2.12';
|
||||
|
||||
/**
|
||||
* SMTP line break constant.
|
||||
|
|
@ -81,7 +81,7 @@ class SMTP
|
|||
* @deprecated Use the `VERSION` constant instead
|
||||
* @see SMTP::VERSION
|
||||
*/
|
||||
public $Version = '5.2.10';
|
||||
public $Version = '5.2.12';
|
||||
|
||||
/**
|
||||
* SMTP server port number.
|
||||
|
|
@ -351,20 +351,21 @@ class SMTP
|
|||
* Perform SMTP authentication.
|
||||
* Must be run after hello().
|
||||
* @see hello()
|
||||
* @param string $username The user name
|
||||
* @param string $password The password
|
||||
* @param string $authtype The auth type (PLAIN, LOGIN, NTLM, CRAM-MD5)
|
||||
* @param string $realm The auth realm for NTLM
|
||||
* @param string $username The user name
|
||||
* @param string $password The password
|
||||
* @param string $authtype The auth type (PLAIN, LOGIN, NTLM, CRAM-MD5, XOAUTH2)
|
||||
* @param string $realm The auth realm for NTLM
|
||||
* @param string $workstation The auth workstation for NTLM
|
||||
* @access public
|
||||
* @return boolean True if successfully authenticated.
|
||||
* @param null|OAuth $OAuth An optional OAuth instance (@see PHPMailerOAuth)
|
||||
* @return bool True if successfully authenticated.* @access public
|
||||
*/
|
||||
public function authenticate(
|
||||
$username,
|
||||
$password,
|
||||
$authtype = null,
|
||||
$realm = '',
|
||||
$workstation = ''
|
||||
$workstation = '',
|
||||
$OAuth = null
|
||||
) {
|
||||
if (!$this->server_caps) {
|
||||
$this->setError('Authentication is not allowed before HELO/EHLO');
|
||||
|
|
@ -388,7 +389,7 @@ class SMTP
|
|||
);
|
||||
|
||||
if (empty($authtype)) {
|
||||
foreach (array('LOGIN', 'CRAM-MD5', 'NTLM', 'PLAIN') as $method) {
|
||||
foreach (array('LOGIN', 'CRAM-MD5', 'NTLM', 'PLAIN', 'XOAUTH2') as $method) {
|
||||
if (in_array($method, $this->server_caps['AUTH'])) {
|
||||
$authtype = $method;
|
||||
break;
|
||||
|
|
@ -436,6 +437,19 @@ class SMTP
|
|||
return false;
|
||||
}
|
||||
break;
|
||||
case 'XOAUTH2':
|
||||
//If the OAuth Instance is not set. Can be a case when PHPMailer is used
|
||||
//instead of PHPMailerOAuth
|
||||
if (is_null($OAuth)) {
|
||||
return false;
|
||||
}
|
||||
$oauth = $OAuth->getOauth64();
|
||||
|
||||
// Start authentication
|
||||
if (!$this->sendCommand('AUTH', 'AUTH XOAUTH2 ' . $oauth, 235)) {
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case 'NTLM':
|
||||
/*
|
||||
* ntlm_sasl_client.php
|
||||
|
|
|
|||
|
|
@ -26,9 +26,14 @@
|
|||
"phpdocumentor/phpdocumentor": "*",
|
||||
"phpunit/phpunit": "4.7.*"
|
||||
},
|
||||
"suggest": {
|
||||
"league/oauth2-client": "Needed for Gmail's XOAUTH2 authentication system"
|
||||
},
|
||||
"autoload": {
|
||||
"classmap": [
|
||||
"class.phpmailer.php",
|
||||
"class.phpmaileroauth.php",
|
||||
"class.oauth.php",
|
||||
"class.smtp.php",
|
||||
"class.pop3.php",
|
||||
"extras/EasyPeasyICS.php",
|
||||
|
|
@ -36,4 +41,4 @@
|
|||
]
|
||||
},
|
||||
"license": "LGPL-2.1"
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,85 @@
|
|||
<?php
|
||||
/**
|
||||
* This example shows settings to use when sending via Google's Gmail servers.
|
||||
*/
|
||||
|
||||
//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 '../PHPMailerAutoload.php';
|
||||
|
||||
//Load dependnecies from composer
|
||||
//If this causes an error, run 'composer install'
|
||||
require '../vendor/autoload.php';
|
||||
|
||||
//Create a new PHPMailer instance
|
||||
$mail = new PHPMailerOAuth;
|
||||
|
||||
//Tell PHPMailer to use SMTP
|
||||
$mail->isSMTP();
|
||||
|
||||
//Enable SMTP debugging
|
||||
// 0 = off (for production use)
|
||||
// 1 = client messages
|
||||
// 2 = client and server messages
|
||||
$mail->SMTPDebug = 0;
|
||||
|
||||
//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';
|
||||
|
||||
//User Email to use for SMTP authentication - Use the same Email used in Google Developer Console
|
||||
$mail->oauthUserEmail = "someone@gmail.com";
|
||||
|
||||
//Obtained From Google Developer Console
|
||||
$mail->oauthClientId = "RANDOMCHARS-----duv1n2.apps.googleusercontent.com";
|
||||
|
||||
//Obtained From Google Developer Console
|
||||
$mail->oauthClientSecret = "RANDOMCHARS-----lGyjPcRtvP";
|
||||
|
||||
//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 = "RANDOMCHARS-----DWxgOvPT003r-yFUV49TQYag7_Aod7y0";
|
||||
|
||||
//Set who the message is to be sent from
|
||||
//For gmail, this generally needs to be the same as the user you logged in as
|
||||
$mail->setFrom('from@example.com', 'First Last');
|
||||
|
||||
//Set who the message is to be sent to
|
||||
$mail->addAddress('whoto@example.com', '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__));
|
||||
|
||||
//Replace the plain text body with one created manually
|
||||
$mail->AltBody = 'This is a plain-text message body';
|
||||
|
||||
//Attach an image file
|
||||
$mail->addAttachment('images/phpmailer_mini.png');
|
||||
|
||||
//send the message, check for errors
|
||||
if (!$mail->send()) {
|
||||
echo "Mailer Error: " . $mail->ErrorInfo;
|
||||
} else {
|
||||
echo "Message sent!";
|
||||
}
|
||||
|
|
@ -0,0 +1,82 @@
|
|||
<?php
|
||||
/**
|
||||
* This example shows signing a message and then sending it via the mail() function of PHP.
|
||||
*
|
||||
* Before you can sign the mail certificates are needed.
|
||||
*
|
||||
*
|
||||
* STEP 1 - Creating a certificate:
|
||||
* You can either use a self signed certificate, pay for a signed one or use free alternatives such as StartSSL/Comodo etc.
|
||||
* Check out this link for more providers: http://kb.mozillazine.org/Getting_an_SMIME_certificate
|
||||
* In this example I am using Comodo.
|
||||
* The form is directly available via https://secure.comodo.com/products/frontpage?area=SecureEmailCertificate
|
||||
* Fill it out and you'll get an email with a link to download your certificate.
|
||||
* Usually the certificate will be directly installed into your browser (FireFox/Chrome).
|
||||
*
|
||||
*
|
||||
* STEP 2 - Exporting the certificate
|
||||
* This is specific to your browser, however, most browsers will give you the option to export your recently added certificate in PKCS12 (.pfx)
|
||||
* Include your private key if you are asked for it.
|
||||
* Set up a password to protect your exported file.
|
||||
*
|
||||
* STEP 3 - Splitting the .pfx into a private key and the certificate.
|
||||
* I use openssl for this. You only need two commands. In my case the certificate file is called 'exported-cert.pfx'
|
||||
* To create the private key do the following:
|
||||
*
|
||||
* openssl pkcs12 -in exported-cert.pfx -nocerts -out cert.key
|
||||
*
|
||||
* Of course the way you name your file (-out) is up to you.
|
||||
* You will be asked for a password for the Import password. This is the password you just set while exporting the certificate into the pfx file.
|
||||
* Afterwards, you can password protect your private key (recommended)
|
||||
* Also make sure to set the permissions to a minimum level and suitable for your application.
|
||||
* To create the certificate file use the following command:
|
||||
*
|
||||
* openssl pkcs12 -in exported-cert.pfx -clcerts -nokeys -out cert.crt
|
||||
*
|
||||
* Again, the way you name your certificate is up to you. You will be also asked for the Import Password.
|
||||
*
|
||||
*
|
||||
* STEP 3 - Code (most of the code is copied from the mail.phps example)
|
||||
*/
|
||||
|
||||
require '../PHPMailerAutoload.php';
|
||||
|
||||
//Create a new PHPMailer instance
|
||||
$mail = new PHPMailer();
|
||||
//Set who the message is to be sent from
|
||||
//IMPORTANT: This must match the email address of your certificate.
|
||||
//Although the certificate will be valid, an error will be thrown since it cannot be verified that the sender and the signer are the same person.
|
||||
$mail->setFrom('from@example.com', 'First Last');
|
||||
//Set an alternative reply-to address
|
||||
$mail->addReplyTo('replyto@example.com', 'First Last');
|
||||
//Set who the message is to be sent to
|
||||
$mail->addAddress('whoto@example.com', 'John Doe');
|
||||
//Set the subject line
|
||||
$mail->Subject = 'PHPMailer mail() 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__));
|
||||
//Replace the plain text body with one created manually
|
||||
$mail->AltBody = 'This is a plain-text message body';
|
||||
//Attach an image file
|
||||
$mail->addAttachment('images/phpmailer_mini.png');
|
||||
|
||||
//signing the email
|
||||
$mail->sign('/path/to/cert.crt', //the location of your certificate file
|
||||
'/path/to/cert.key', //the location of your private key file
|
||||
'yourSecretPrivateKeyPassword'); //the password you protected your private key with (may be empty but parameter can not mit omitted!)
|
||||
//!!!! This is not the Import Password !!!!
|
||||
|
||||
//send the message, check for errors
|
||||
if (!$mail->send()) {
|
||||
echo "Mailer Error: " . $mail->ErrorInfo;
|
||||
} else {
|
||||
echo "Message sent!";
|
||||
}
|
||||
|
||||
/**
|
||||
* REMARKS:
|
||||
* If your email client does not support S/MIME it will most likely just show an attachment smime.p7s which is the signature contained in the email.
|
||||
* Other clients, such as Thunderbird support S/MIME natively and will validate the signature automatically and report the result in some way.
|
||||
*/
|
||||
?>
|
||||
|
|
@ -0,0 +1,64 @@
|
|||
<?php
|
||||
/**
|
||||
* Get an OAuth2 token from Google.
|
||||
* * Install this script on your server so that it's accessible
|
||||
* as [https/http]://<yourdomain>/<folder>/get_oauth_token.php
|
||||
* e.g.: http://localhost/phpmail/get_oauth_token.php
|
||||
* * Ensure dependencies are installed with 'composer install'
|
||||
* * Set up an app in your Google developer console
|
||||
* * Set the script address as the app's redirect URL
|
||||
* If no refresh token is obtained when running this file, revoke access to your app
|
||||
* using link: https://accounts.google.com/b/0/IssuedAuthSubTokens and run the script again.
|
||||
* This script requires PHP 5.4 or later
|
||||
*/
|
||||
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
session_start();
|
||||
|
||||
//If this automatic URL doesn't work, set it yourself manually
|
||||
$redirectUri = isset($_SERVER['HTTPS']) ? 'https://' : 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
|
||||
//$redirectUri = 'http://localhost/phpmailer/get_oauth_token.php';
|
||||
$clientId = 'RANDOMCHARS-----duv1n2.apps.googleusercontent.com';
|
||||
$clientSecret = 'RANDOMCHARS-----lGyjPcRtvP';
|
||||
|
||||
//All details obtained by setting up app in Google developer console.
|
||||
//Set Redirect URI in Developer Console as [https/http]://<yourdomain>/<folder>/get_oauth_token.php
|
||||
$provider = new League\OAuth2\Client\Provider\Google (
|
||||
[
|
||||
'clientId' => $clientId,
|
||||
'clientSecret' => $clientSecret,
|
||||
'redirectUri' => $redirectUri,
|
||||
'scopes' => ['https://mail.google.com/'],
|
||||
'accessType' => 'offline'
|
||||
]
|
||||
);
|
||||
|
||||
if (!isset($_GET['code'])) {
|
||||
// If we don't have an authorization code then get one
|
||||
$authUrl = $provider->getAuthorizationUrl();
|
||||
$_SESSION['oauth2state'] = $provider->state;
|
||||
header('Location: ' . $authUrl);
|
||||
exit;
|
||||
// Check given state against previously stored one to mitigate CSRF attack
|
||||
} elseif (empty($_GET['state']) || ($_GET['state'] !== $_SESSION['oauth2state'])) {
|
||||
unset($_SESSION['oauth2state']);
|
||||
exit('Invalid state');
|
||||
} else {
|
||||
$provider->accessType = 'offline';
|
||||
// Try to get an access token (using the authorization code grant)
|
||||
$token = $provider->getAccessToken(
|
||||
'authorization_code',
|
||||
[
|
||||
'code' => $_GET['code']
|
||||
]
|
||||
);
|
||||
// Use this to interact with an API on the users behalf
|
||||
// echo $token->accessToken.'<br>';
|
||||
|
||||
// Use this to get a new access token if the old one expires
|
||||
echo 'Refresh Token: ' . $token->refreshToken;
|
||||
|
||||
// Unix timestamp of when the token will expire, and need refreshing
|
||||
// echo $token->expires;
|
||||
}
|
||||
|
|
@ -975,6 +975,31 @@ EOT;
|
|||
$this->assertTrue($this->Mail->send(), $this->Mail->ErrorInfo);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test embedded image without a name
|
||||
*/
|
||||
public function testHTMLStringEmbedNoName()
|
||||
{
|
||||
$this->Mail->Body = 'This is the <strong>HTML</strong> part of the email.';
|
||||
$this->Mail->Subject .= ': HTML + unnamed embedded image';
|
||||
$this->Mail->isHTML(true);
|
||||
|
||||
if (!$this->Mail->addStringEmbeddedImage(
|
||||
file_get_contents('../examples/images/phpmailer_mini.png'),
|
||||
md5('phpmailer_mini.png').'@phpmailer.0',
|
||||
'', //intentionally empty name
|
||||
'base64',
|
||||
'image/png',
|
||||
'inline')
|
||||
) {
|
||||
$this->assertTrue(false, $this->Mail->ErrorInfo);
|
||||
return;
|
||||
}
|
||||
|
||||
$this->buildBody();
|
||||
$this->assertTrue($this->Mail->send(), $this->Mail->ErrorInfo);
|
||||
}
|
||||
|
||||
/**
|
||||
* Simple HTML and multiple attachment test
|
||||
*/
|
||||
|
|
|
|||
Loading…
Reference in New Issue