Skip to content

Instantly share code, notes, and snippets.

@JamesTheHacker
Created December 1, 2016 21:17
Show Gist options
  • Save JamesTheHacker/f5bb24fdd81c92f054783f45093d5cab to your computer and use it in GitHub Desktop.
Save JamesTheHacker/f5bb24fdd81c92f054783f45093d5cab to your computer and use it in GitHub Desktop.
<?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