Created
July 22, 2013 23:00
-
-
Save rafaelhdr/6058469 to your computer and use it in GitHub Desktop.
ZF2 Mail Service
It is possible to embed a layout and template for the e-mail
This file contains 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 | |
/* | |
* Original: https://gist.github.com/devmatheus/38cd250c3bb46f0ad2c8 | |
* | |
* Changes: | |
* - It is not necessary define page on __construct | |
* - Set layout for embed template | |
* | |
* Problem: | |
* - It is necessary keep de layout and page in the same Module | |
*/ | |
namespace MyModule\Mail; | |
use Zend\Mail\Transport\Smtp as SmtpTransport; | |
use Zend\Mail\Message; | |
use Zend\View\Model\ViewModel; | |
use Zend\View\Renderer\PhpRenderer; | |
use Zend\Mime\Message as MimeMessage; | |
use Zend\Mime\Part as MimePart; | |
class Mail | |
{ | |
protected $transport; | |
protected $view; | |
protected $body; | |
protected $message; | |
protected $subject; | |
protected $from; | |
protected $to; | |
protected $data; | |
protected $page; | |
protected $layout; | |
public function __construct(SmtpTransport $transport, $view) | |
{ | |
$this->transport = $transport; | |
$this->view = $view; | |
} | |
public function setPage($page) | |
{ | |
$this->page = $page; | |
return $this; | |
} | |
public function setLayout($layout) | |
{ | |
$this->layout = $layout; | |
return $this; | |
} | |
public function setSubject($subject) | |
{ | |
$this->subject = $subject; | |
return $this; | |
} | |
public function setFrom($from) | |
{ | |
$this->from = $from; | |
return $this; | |
} | |
public function setTo($to) | |
{ | |
$this->to = $to; | |
return $this; | |
} | |
public function setData($data) | |
{ | |
$this->data = $data; | |
return $this; | |
} | |
public function renderView($page, array $data) | |
{ | |
$model = new ViewModel; | |
$model->setTemplate("mailer/{$page}.phtml"); | |
$model->setOption('has_parent',true); | |
$model->setVariables($data); | |
$view = new \Zend\View\Renderer\PhpRenderer(); | |
$resolver = new \Zend\View\Resolver\TemplateMapResolver(); | |
$mapper = array( | |
'mailTemplate' => __DIR__ . "/../../../view/mailer/{$page}.phtml", | |
); | |
$resolver->setMap($mapper); | |
$view->setResolver($resolver); | |
$viewModel = new \Zend\View\Model\ViewModel(); | |
$viewModel->setTemplate('mailTemplate') | |
->setVariables($data); | |
$content = $view->render($viewModel); | |
if ($this->layout) { | |
$mapper['mailLayout'] = __DIR__ . "/../../../view/layout/{$this->layout}.phtml"; | |
$resolver->setMap($mapper); | |
$viewLayout = new \Zend\View\Model\ViewModel(); | |
$variables = array_merge( | |
array('content' => $content), | |
$data | |
); | |
$viewLayout->setTemplate('mailLayout') | |
->setVariables($variables); | |
$content = $view->render($viewLayout); | |
} | |
return $content; | |
} | |
public function prepare() | |
{ | |
$html = new MimePart($this->renderView($this->page, $this->data)); | |
$html->type = "text/html"; | |
$body = new MimeMessage(); | |
$body->setParts(array($html)); | |
$this->body = $body; | |
$this->message = new Message; | |
$this->message->addFrom($this->from) | |
->addTo($this->to) | |
->setSubject($this->subject) | |
->setBody($this->body); | |
return $this; | |
} | |
public function send() | |
{ | |
$this->transport->send($this->message); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment