Created
July 22, 2012 13:29
-
-
Save awartoft/3159684 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 | |
/** | |
* @param string $name | |
* @param string $description | |
* @param string $email | |
* @param array $variables | |
* | |
* @return boolean | |
*/ | |
public function send($name, $description, $email, array $variables = array()) | |
{ | |
$template = $this->service->get($name); | |
if (! $template) { | |
$this->service->create($name, $description, $variables); | |
$this->getLogger() | |
->crit( | |
sprintf('Emailer service: a new template was created name: %s, description: %s', $name, $description) | |
); | |
return false; | |
} | |
if (! $template->isValid()) { | |
$this->getLogger() | |
->emerg( | |
sprintf('Emailer service: attempt to send email failed due to invalid template, name: %s', $name), | |
array( | |
'email' => $email, | |
'variables' => $variables | |
) | |
); | |
return false; | |
} | |
// Place the variables in the template | |
$templateString = $template->render($variables); | |
// Create the message | |
$message = $this->getBasicMessage(); | |
// TODO: find out this this is required | |
// If it's not added then the html gets sent as an attachment | |
$text = new MimePart(''); | |
$text->type = "text/plain"; | |
$html = new MimePart($templateString); | |
$html->type = 'text/html'; | |
$body = new MimeMessage(); | |
$body->setParts(array($text, $html)); | |
// Apply the variable stuff | |
$message->setTo($email) | |
->setBody($body) | |
->setSubject($template->getSubject()); | |
// Get the emails | |
$bcc = explode(',', str_replace(' ', '', $template->getBcc())); | |
// Validator | |
$validator = new EmailValidator(); | |
foreach($bcc as $email) | |
{ | |
if (! $validator->isValid($email)) { | |
$this->getLogger() | |
->warn( | |
sprintf('Emailer service: invalid BCC address "%s" specified for template, name: %s', $email, $name) | |
); | |
} | |
$message->addBcc($email); | |
} | |
$this->transport->send($message); | |
return true; | |
} | |
/** | |
* @return \Zend\Mail\Message | |
*/ | |
public function getBasicMessage() | |
{ | |
$message = new MailMessage(); | |
// Set the basic stuff | |
$message->setFrom($this->options->from) | |
->setReplyTo($this->options->reply_to) | |
->setEncoding($this->options->encoding); | |
return $message; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment