Created
December 1, 2016 21:17
-
-
Save JamesTheHacker/f5bb24fdd81c92f054783f45093d5cab to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace StudentApp\Services\Email; | |
abstract class EmailService | |
{ | |
protected $emailService; | |
protected $template; | |
protected $logger; | |
public function __construct(\Mandrill $emailService, \MongoLog $logger) | |
{ | |
$this->emailService = $emailService; | |
$this->logger = $logger; | |
} | |
public abstract function setHTMLTemplate($templatePath); | |
public function send($subject, $toEmail, $toName, $fromEmail, $fromName) | |
{ | |
$message = [ | |
'html' => $this->template, | |
'subject' => $subject, | |
'from_email' => $fromEmail, | |
'from_name' => $fromName, | |
'to' => [ | |
[ | |
'email' => $toEmail, | |
'name' => $toName, | |
'type' => 'to' | |
] | |
], | |
'important' => true, | |
'auto_text' => true, | |
]; | |
try { | |
$result = $this->emailService->messages->send($message)[0]; | |
} catch(\Mandrill_Error $e) { | |
$this->logger->addCritical("Error sending email in {$e->getFile()}: {$e->getMessage()}"); | |
return false; | |
} catch(\Exception $e) { | |
$this->logger->addCritical("Uncaught exception in {$e->getFile()}: {$e->getMessage()}"); | |
return false; | |
} | |
if(empty($result['status']) || $result['status'] != 'sent') | |
{ | |
$this->logger->addCritical("Mandrill response status empty or not 'sent'"); | |
return false; | |
} | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment