Skip to content

Instantly share code, notes, and snippets.

Created April 22, 2015 06:53
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 anonymous/f905a6075e87174118a6 to your computer and use it in GitHub Desktop.
Save anonymous/f905a6075e87174118a6 to your computer and use it in GitHub Desktop.
<?php
namespace Emailer;
use Latte;
use Nette;
use Tracy\Debugger;
use WebChemistry;
use Nextras\Application\LinkFactory;
/**
* Class Emailer
* @package Emailer
*/
class Emailer extends Nette\Object
{
/** @var string */
public $templateFile = '';
/** @var array */
public $templateParameters = array();
/** @var Nette\Mail\Message */
private $message;
/** @var EmailAddressContainer */
private $emailAddressContainer;
/** @var EmailerSMTP */
private $mailerSMTP;
/** @var LinkFactory */
private $linkFactory;
/** @var Nette\Application\UI\ITemplateFactory */
private $templateFactory;
public function __construct(EmailAddressContainer $emailAddressContainer, EmailerSMTP $mailerSMTP, Nette\Application\UI\ITemplateFactory $templateFactory)
{
$this->message = new Nette\Mail\Message();
$this->mailerSMTP = $mailerSMTP;
$this->emailAddressContainer = $emailAddressContainer;
$this->templateFactory = $templateFactory;
//$this->templateParameter['link'] = $this->linkFactory;
//$this->templateParameter['basePath'] = $this->linkFactory->link('//Front:Homepage:default');
}
/**
* Send e-mail
*
* @throws \Exception
*/
public function send()
{
$template = $this->templateFactory->createTemplate();
foreach ($this->templateParameters as $key => $value) {
$template->{$key} = $value;
}
$template->setFile($this->templateFile);
$this->message->setHtmlBody($template);
$this->mailerSMTP->send($this->message);
}
/**
* @param $subject
* @return $this
*/
public function setSubject($subject = 'Bez předmětu')
{
$this->templateParameters['subject'] = $subject;
$this->message->setSubject($subject);
return $this;
}
/**
* @param $from
* @return $this
*/
public function setFrom($from)
{
$email = $this->getEmailAddress($from);
if ($email instanceof EmailAddress) {
$this->message->setFrom($email->getEmail(), $email->getName());
} else {
$this->message->setFrom($email);
}
return $this;
}
/**
* @param string|EmailAddress $to
* @return $this
*/
public function addTo($to)
{
$email = $this->getEmailAddress($to);
if ($email instanceof EmailAddress) {
$this->message->addTo($email->getEmail(), $email->getName());
} else {
$this->message->addTo($email);
}
return $this;
}
/**
* @param string|EmailAddress $to
* @return $this
*/
public function addBcc($to)
{
$email = $this->getEmailAddress($to);
if ($email instanceof EmailAddress) {
$this->message->addBcc($email->getEmail(), $email->getName());
} else {
$this->message->addBcc($email);
}
return $this;
}
/**
* Set template parameter
*
* @param $variable
* @param $parameter
* @return $this
*/
public function setTemplateParameters($variable, $parameter)
{
$this->templateParameters[$variable] = $parameter;
return $this;
}
/**
* @param string $path
* @return $this
* @throws Nette\InvalidArgumentException
*/
public function setTemplateFile($path)
{
$this->templateFile = (string)$path;
if (!file_exists($this->templateFile)) {
throw new Nette\InvalidArgumentException('Template file "' . $this->templateFile . '" not found.');
}
return $this;
}
/**
* Get email address
*
* @param $input
* @return mixed
* @throws Nette\InvalidArgumentException
*/
private function getEmailAddress($input)
{
if (!$input instanceof EmailAddress && !is_string($input)) {
throw new Nette\InvalidArgumentException('Invalid email address given. Must be string or instance of Emailer\EmailAddress');
}
if (is_string($input)) {
if (!Nette\Utils\Validators::isEmail($input)) {
$input = $this->emailAddressContainer->getAddress($input);
}
}
return $input;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment