Skip to content

Instantly share code, notes, and snippets.

@ABM-Dan
Created September 19, 2016 16:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ABM-Dan/0fadc273f6be90e77d9edef98151b237 to your computer and use it in GitHub Desktop.
Save ABM-Dan/0fadc273f6be90e77d9edef98151b237 to your computer and use it in GitHub Desktop.
<?php
namespace CommonBundle\Service;
use Symfony\Component\Templating\EngineInterface;
class SendMail extends \Swift_Message
{
/**
* @var string
*/
protected $text_template = '';
/**
* @var string
*/
protected $html_template = '';
/**
* @var array
*/
protected $template_tags = [];
/**
* @var \Swift_Mailer
*/
protected $mailer;
/**
* @var EngineInterface
*/
protected $templateEngine;
public function init(\Swift_Mailer $mailer, $config, EngineInterface $templateEngine)
{
$this->setFrom([$config['email'] => $config['company_name']]);
$this->templateEngine = $templateEngine;
$this->mailer = $mailer;
}
/**
* Set text template.
*
* @param string $template
*
* @return SendMail
*/
public function setTextTemplate($template)
{
$this->text_template = $template;
return $this;
}
/**
* Get text template.
*
* @return string
*/
public function getTextTemplate()
{
return $this->text_template;
}
/**
* Set html template.
*
* @param string $template
*
* @return SendMail
*/
public function setHtmlTemplate($template)
{
$this->html_template = $template;
return $this;
}
/**
* Get html template.
*
* @return string
*/
public function getHtmlTemplate()
{
return $this->html_template;
}
/**
* Set template tags.
*
* @param string $template_tags
*
* @return SendMail
*/
public function setTemplateTags($template_tags)
{
$this->template_tags = $template_tags;
return $this;
}
/**
* Get template tags.
*
* @return array
*/
public function getTemplateTags()
{
return $this->template_tags;
}
public function addAttachment($data, $filename, $mime)
{
$attachment = \Swift_Attachment::newInstance($data, $filename, $mime);
$this->attach($attachment);
return $this;
}
public function addAttachmentFromPath($data, $filename, $mime)
{
$attachment = \Swift_Attachment::newInstance($data, $mime)->setFilename($filename);
$this->attach($attachment);
return $this;
}
public function send()
{
if ($this->getTextTemplate() !== '' && $this->getHtmlTemplate() !== '') {
// if we have both text and html templates
// set text body and pass tags
$this->setBody($this->templateEngine->render($this->getTextTemplate(), $this->getTemplateTags()));
// add html attachment and set content-type
$this->addPart($this->templateEngine->render($this->getHtmlTemplate(), $this->getTemplateTags()), 'text/html');
} elseif ($this->getTextTemplate()) {
// if we have text template only
// set body
$this->setBody($this->templateEngine->render($this->getTextTemplate(), $this->getTemplateTags()));
} elseif ($this->getHtmlTemplate()) {
// if we have html template only
// set body and pass tags
$this->setBody($this->templateEngine->render($this->getHtmlTemplate(), $this->getTemplateTags()));
// set content-type
$this->setContentType('text/html');
} else {
throw new \LogicException('Not template was set');
}
return $this->mailer->send($this);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment