Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update PHPMailer and extend it to add some functionalities. #4603

Merged
merged 11 commits into from Oct 17, 2016
6 changes: 3 additions & 3 deletions applications/dashboard/models/class.activitymodel.php
Expand Up @@ -888,7 +888,7 @@ public function sendNotification($ActivityID, $Story = '', $Force = false) {
$Emailed = self::SENT_SKIPPED;
}
} catch (phpmailerException $pex) {
if ($pex->getCode() == PHPMailer::STOP_CRITICAL) {
if ($pex->getCode() == PHPMailer::STOP_CRITICAL && !$Email->PhpMailer->isServerError($pex)) {
$Emailed = self::SENT_FAIL;
} else {
$Emailed = self::SENT_ERROR;
Expand Down Expand Up @@ -1022,7 +1022,7 @@ public function email(&$Activity, $NoDelete = false) {
}
}
} catch (phpmailerException $pex) {
if ($pex->getCode() == PHPMailer::STOP_CRITICAL) {
if ($pex->getCode() == PHPMailer::STOP_CRITICAL && !$Email->PhpMailer->isServerError($pex)) {
$Emailed = self::SENT_FAIL;
} else {
$Emailed = self::SENT_ERROR;
Expand Down Expand Up @@ -1163,7 +1163,7 @@ public function sendNotificationQueue() {
$Emailed = self::SENT_SKIPPED;
}
} catch (phpmailerException $pex) {
if ($pex->getCode() == PHPMailer::STOP_CRITICAL) {
if ($pex->getCode() == PHPMailer::STOP_CRITICAL && !$Email->PhpMailer->isServerError($pex)) {
$Emailed = self::SENT_FAIL;
} else {
$Emailed = self::SENT_ERROR;
Expand Down
7 changes: 4 additions & 3 deletions composer.json
Expand Up @@ -32,9 +32,10 @@
"vanilla/htmlawed": "~2.0",
"vanilla/nbbc": "~2.1",
"vanilla/garden-password": "~1.0",
"tburry/pquery": "~1.1",
"container-interop/container-interop": "^1.1",
"vanilla/php-ico": "~1.0"
"vanilla/php-ico": "~1.0",
"tburry/pquery": "~1.1",
"phpmailer/phpmailer": "~5.2",
"container-interop/container-interop": "^1.1"
},
"autoload": {
"classmap": [
Expand Down
64 changes: 62 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

84 changes: 84 additions & 0 deletions library/Vanilla/VanillaMailer.php
@@ -0,0 +1,84 @@
<?php
/**
* @author Alexandre (DaazKu) Chouinard <alexandre.c@vanillaforums.com>
* @copyright 2009-2016 Vanilla Forums Inc.
* @license GPLv2
*/

namespace Vanilla;


use Vanilla\Utility\CamelCaseScheme;

/**
* Shim class used for backward compatibility.
*/
class VanillaMailer extends \PHPMailer {

/**
* Either set or get the value of "throwExceptions".
*
* @param bool $newValue Whether this instance should throw exceptions or not
* @return The current value
*/
public function throwExceptions($newValue = null) {
deprecated('throwExceptions', 'getThrowExceptions/setThrowExceptions');
if ($newValue !== null) {
$this->exceptions = $newValue;
}
return $this->exceptions;
}

/**
* Get throwExceptions value.
*
* @return bool Is this instance set to throw exceptions or not.
*/
public function getThrowExceptions() {
return $this->exceptions;
}

/**
* Set throwExceptions value.
*
* @param bool $newValue The new value to set.
* @return VanillaMailer
*/
public function setThrowExceptions($newValue) {
$this->exceptions = (bool)$newValue;

return $this;
}

/**
* Return the number of recipients.
*
* @return int
*/
public function countRecipients() {
deprecated('countRecipients', 'count($phpMailer->getAllRecipientAddresses())');
return count($this->getAllRecipientAddresses());
}

/**
* Check the phpmailerException message and tell us if the exception should be treated as
* a server error instead of a "critical" error.
* Server error means that we can try to resend the email.
*/
public function isServerError(\phpmailerException $e) {
$serverErrorMessages = [
'connect_host',
'data_not_accepted',
'smtp_connect_failed',
'execute',
];

foreach($serverErrorMessages as $errorMessage) {
if (strpos($e->getMessage(), $this->lang($errorMessage)) !== false) {
return true;
}
}

return false;
}
}
9 changes: 4 additions & 5 deletions library/core/class.email.php
Expand Up @@ -41,10 +41,9 @@ class Gdn_Email extends Gdn_Pluggable {
* Constructor.
*/
function __construct() {
$this->PhpMailer = new PHPMailer();
$this->PhpMailer->CharSet = c('Garden.Charset', 'utf-8');
$this->PhpMailer = new \Vanilla\VanillaMailer();
$this->PhpMailer->CharSet = 'utf-8';
$this->PhpMailer->SingleTo = c('Garden.Email.SingleTo', false);
$this->PhpMailer->PluginDir = combinePaths(array(PATH_LIBRARY, 'vendors/phpmailer/'));
$this->PhpMailer->Hostname = c('Garden.Email.Hostname', '');
$this->PhpMailer->Encoding = 'quoted-printable';
$this->clear();
Expand Down Expand Up @@ -353,12 +352,12 @@ public function send($EventName = '') {
$this->fireEvent('SendMail');
}

if (!empty($this->Skipped) && $this->PhpMailer->countRecipients() == 0) {
if (!empty($this->Skipped) && count($this->PhpMailer->getAllRecipientAddresses()) == 0) {
// We've skipped all recipients.
throw new Exception('No valid email recipients.', self::ERR_SKIPPED);
}

$this->PhpMailer->throwExceptions(true);
$this->PhpMailer->setThrowExceptions(true);
if (!$this->PhpMailer->send()) {
throw new Exception($this->PhpMailer->ErrorInfo);
}
Expand Down