Skip to content

Commit

Permalink
Adding smtp debug (#20)
Browse files Browse the repository at this point in the history
* Adding the ability to supply SMTPDebug

* Changed send function to be just a check for errors function

* fixing code

* Another code fix
  • Loading branch information
catharsisjelly authored and markguinn committed Feb 27, 2017
1 parent 827faad commit 1a09edc
Showing 1 changed file with 44 additions and 5 deletions.
49 changes: 44 additions & 5 deletions code/SmtpMailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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
Expand All @@ -68,7 +73,8 @@ public function __construct(
$pass = false,
$encryption = 'fallback',
$charset = false,
$port = false
$port = false,
$smtpDebug = 0
) {
$cfg = $this->config();

Expand Down Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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
*/
Expand All @@ -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;
}
Expand Down Expand Up @@ -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)
{
Expand All @@ -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;
}
Expand Down Expand Up @@ -417,7 +456,7 @@ public function sendHTML(
if ($mail->Send()) {
return array($to, $subject, $htmlContent, $customheaders);
} else {
return false;
return $this->checkMailError();
}
}
}

0 comments on commit 1a09edc

Please sign in to comment.