From 1a09edc379e3ec7e0c8d63d47886484b9ece94a9 Mon Sep 17 00:00:00 2001 From: Chris Lock Date: Mon, 27 Feb 2017 16:08:40 +0000 Subject: [PATCH] Adding smtp debug (#20) * Adding the ability to supply SMTPDebug * Changed send function to be just a check for errors function * fixing code * Another code fix --- code/SmtpMailer.php | 49 ++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 44 insertions(+), 5 deletions(-) diff --git a/code/SmtpMailer.php b/code/SmtpMailer.php index 423ee1c..cdbb233 100644 --- a/code/SmtpMailer.php +++ b/code/SmtpMailer.php @@ -42,7 +42,7 @@ class SmtpMailer extends Mailer protected $tls; /** - * @var int $port - the smpt server port + * @var int $port - the smtp server port */ protected $port; @@ -51,6 +51,11 @@ class SmtpMailer extends Mailer */ protected $encryption; + /** + * @var int $smtpDebug - Debug param that gets passed to PHPMailer + */ + protected $smtpDebug; + /** * creates and configures the mailer @@ -68,7 +73,8 @@ public function __construct( $pass = false, $encryption = 'fallback', $charset = false, - $port = false + $port = false, + $smtpDebug = 0 ) { $cfg = $this->config(); @@ -103,12 +109,16 @@ public function __construct( if ($port === false) { $port = $cfg->port; } + if ($smtpDebug === 0) { + $smtpDebug = $cfg->smtpDebug; + } $this->setHost($host); $this->setCredentials($user, $pass); $this->setEncryption($encryption); $this->setCharset($charset); $this->setPort($port); + $this->setSMTPDebug($smtpDebug); } /** @@ -225,6 +235,16 @@ public function getCharset() return $this->charset; } + /** + * @param int $smtpDebug + * @return $this + */ + public function setSMTPDebug($smtpDebug) + { + $this->smtpDebug = $smtpDebug; + return $this; + } + /** * creates a new phpmailer object */ @@ -242,6 +262,10 @@ protected function initMailer() $mail->SMTPSecure = $this->getEncryption(); + if ($this->smtpDebug) { + $mail->SMTPDebug = $this->smtpDebug; + } + if ($this->port) { $mail->Port = $this->port; } @@ -361,7 +385,7 @@ protected function initEmail($to, $from, $subject, $attachedFiles = false, $cust * @param array|bool $attachedFiles * @param array|bool $customheaders * - * @return bool + * @return bool|array */ public function sendPlain($to, $from, $subject, $plainContent, $attachedFiles = false, $customheaders = false) { @@ -370,9 +394,24 @@ public function sendPlain($to, $from, $subject, $plainContent, $attachedFiles = // set up the body $mail->Body = $plainContent; - // send and return if ($mail->Send()) { return array($to, $subject, $plainContent, $customheaders); + } else { + return $this-checkMailError(); + } + } + + /** + * @return bool + * @throws \Exception if he environment is dev + */ + public function checkMailError() + { + if (Director::isDev()) { + throw new \Exception(sprintf( + 'PHPMailer failed: %s', + $mail->ErrorInfo + )); } else { return false; } @@ -417,7 +456,7 @@ public function sendHTML( if ($mail->Send()) { return array($to, $subject, $htmlContent, $customheaders); } else { - return false; + return $this->checkMailError(); } } }