Build e-mail message

@param  TemplateInterface        $template
@param  array                    $headers
@param  ModelInterface           $viewModel
@throws InvalidArgumentException if template is not string nor TemplateInterface
@return Message
192841
public function compose(array $headers, TemplateInterface $template, ModelInterface $viewModel = null)
    {
        if (null == $viewModel) {
            $viewModel = new ViewModel();
        }

        $event = $this->getEvent();
        $event->setTemplate($template);
        $em = $this->getEventManager();

        // 1. Trigger pre event
        $event->setName(ComposerEvent::EVENT_COMPOSE_PRE);
        $em->triggerEvent($event);

        // 2. inject headers
        $event->setName(ComposerEvent::EVENT_HEADERS_PRE);
        $em->triggerEvent($event);
        foreach ($headers as $name => $value) {
            $event->getMessage()->getHeaders()->addHeaderLine($name, $value);
        }
        $event->setName(ComposerEvent::EVENT_HEADERS_POST);
        $em->triggerEvent($event);

        // prepare placeholder for message body
        $body = new MimeMessage();

        // 3. Render plain text template
        if ($template instanceof TextTemplateInterface) {
            $textViewModel = clone $viewModel;
            $textViewModel->setTemplate($template->getTextTemplateName());
            $event->setViewModel($textViewModel);

            $event->setName(ComposerEvent::EVENT_TEXT_BODY_PRE);
            $em->triggerEvent($event);

            $text = new MimePart($this->renderer->render($event->getViewModel()));
            $text->type = 'text/plain';
            $text->charset = $event->getMessage()->getHeaders()->getEncoding();
            $body->addPart($text);

            $event->setName(ComposerEvent::EVENT_TEXT_BODY_POST);
            $em->triggerEvent($event);
        }

        // 4. Render HTML template
        if ($template instanceof HtmlTemplateInterface) {
            $htmlViewModel = clone $viewModel;
            $htmlViewModel->setTemplate($template->getHtmlTemplateName());
            $event->setViewModel($htmlViewModel);

            $event->setName(ComposerEvent::EVENT_HTML_BODY_PRE);
            $em->triggerEvent($event);

            $html = new MimePart($this->renderer->render($event->getViewModel()));
            $html->type = 'text/html';
            $html->charset = $event->getMessage()->getHeaders()->getEncoding();
            $body->addPart($html);

            $event->setName(ComposerEvent::EVENT_HTML_BODY_POST);
            $em->triggerEvent($event);
        }

        // 5. inject body into message
        $event->setBody($body);
        $event->getMessage()->setBody($body);

        // 6. set multipart/alternative when both versions are available
        if ($template instanceof TextTemplateInterface && $template instanceof HtmlTemplateInterface) {
            $event->getMessage()->getHeaders()->get('content-type')->setType('multipart/alternative')
                ->addParameter('boundary', $body->getMime()->boundary());
        }

        $event->setName(ComposerEvent::EVENT_COMPOSE_POST);
        $em->triggerEvent($event);

        return $event->getMessage();
    }
547794
public function formatAndSendEmail($template,$data,$email,$from,
		$replyto='',$ccemail='',$bccemail='',$outtemplate = null,$textemail=false) {
		
		$this->logQ($this->formatoptions['locale'],'formatter');
		
		$formatter = new EmailFormat($this->formatoptions);
		
		$templatedata = $formatter->fetchTemplate($template,$data,$outtemplate);
		
		return $this->sendEmail(
			$email,
			$templatedata['subject'],
			$templatedata['body'],
			$from,
			$replyto,
			$ccemail,
			$bccemail,
			$textemail);
	}
802591
public function parseTemplateText($viewText, &$model) {

        $twigLoader = new \Twig_Loader_Array(array("template" => $viewText));

        $twig = new \Twig_Environment($twigLoader);

        $newModel = SerialisableArrayUtils::convertSerialisableObjectsToAssociativeArrays($model);

        return $twig->render("template", $newModel);

    }
852127
public function render(ViewTemplateInterface $viewTemplate)
    {
        $options = [
            [
                'value'      => 'tl_limit',
                'attributes' => '',
                'content'    => $GLOBALS['TL_LANG']['MSC']['filterRecords']
            ]
        ];

        $optionsPerPage = $this->getItemsPerPage();
        $optionsTotal   = \ceil($this->intTotal / $optionsPerPage);

        for ($i = 0; $i < $optionsTotal; $i++) {
            $first      = ($i * $optionsPerPage);
            $thisLimit  = $first . ',' . $optionsPerPage;
            $upperLimit = ($first + $optionsPerPage);

            if ($upperLimit > $this->intTotal) {
                $upperLimit = $this->intTotal;
            }

            $options[] = [
                'value'      => $thisLimit,
                'attributes' => ($first === $this->getOffset()) ? ' selected' : '',
                'content'    => ($first + 1) . ' - ' . $upperLimit
            ];
        }

        if ($this->intTotal > $optionsPerPage) {
            $options[] = [
                'value'      => 'all',
                'attributes' =>
                    ((0 === $this->getOffset()) && ($this->intTotal === $this->getAmount()))
                        ? 'selected'
                        : '',
                'content'    => $GLOBALS['TL_LANG']['MSC']['filterAll']
            ];
        }

        $viewTemplate->set('options', $options);

        return $this;
    }
628290
public final function withHeader($name, $value): MessageInterface
   {
       $value = $this->parseHeaderValue($value);
       if (!is_string($name) || [] === $value) {
           throw new InvalidArgumentException(
               "Can't get instance for header: invalid " . $name);
       }
       $header = $this->header;
       $header[$this->parseHeaderName($name)] = $value;
       return $this->with("header", $header);
   }
327610
public function sendTemplateAsync($templateId, $sendEmail)
    {
        return $this->sendTemplateAsyncWithHttpInfo($templateId, $sendEmail)
            ->then(
                function ($response) {
                    return $response[0];
                }
            );
    }
1338483
protected function addTemplate($templates, $description, $templateId, $formats = array('txt', 'html'))
    {
        $route = $this->router->generate('azine_email_web_preview', array('template' => urlencode($templateId)));

        $template = array('url' => $route,
                            'description' => $description,
                            'formats' => $formats,
                            'templateId' => $templateId,
        );

        $templates[] = $template;

        return $templates;
    }
952334
protected function generatePageTemplate(PageTree $pageTree, ThemeInterface $theme, array $options)
    {
        if ( ! $this->page->getIsPublished()) {
            $this->twigTemplate = "{% extends 'RedKiteLabsThemeEngineBundle:Frontend:unpublished.html.twig' %}";

            return $this;
        }

        $options["filter"] = array('page');
        $this->twigTemplate = sprintf("{%% extends '%s:%s:%s/base/%s.html.twig' %%}" . PHP_EOL, $options["deployBundle"], $options["templatesDir"], $this->language->getLanguageName(), $this->page->getTemplateName());
        $this->twigTemplate .= $this->metatagsSection->generateSection($pageTree, $theme, $options);
        $this->twigTemplate .= $this->assetsSection->generateSection($pageTree, $theme, $options);
        $this->twigTemplate .= $this->contentSection->generateSection($pageTree, $theme, $options);

        return $this;
    }
457698
protected function getHtmlGenerator(array $fields, $modelName, $template)
    {
        if ($this->isCollectiveTemplate($template)) {
            return new LaravelCollectiveHtml($fields, $modelName, $template);
        }

        return new StandardHtml($fields, $modelName, $template);
    }
1212528
private function updateMessageTemplate( $template, $key ) {    
        //invalide message key
        if( !key_exists( $messageKey, $this->templates ) && $key != self::DEFAULT_KEY )
            throw new Exception\MessageTemplateDoesNotExist( "A message template does not exist for key $messageKey" );

        $this->templates[$key] = $template;
    }
23314
private function createFromTemplate($message, $template) {
        ob_start();
        ob_implicit_flush(false);

        if (is_array($message) && !empty($message)) {
            extract($message, EXTR_OVERWRITE);
        }

        $current_module = RouteController::$currentRoute['module'];
        $templatePath = MODULES_DIR . '/' . $current_module . '/Views/' . $template . '.php';
        require $templatePath;
        return ob_get_clean();
    }
580800
public function renderTemplate($filename, $vars = [], $templateDir = '/templates/')
    {
        $filePath = __DIR__ . $templateDir . $filename;
        if (!file_exists($filePath)) {
            // try with '.php' at the end
            $filePath = $filePath . '.php';
            if (!file_exists($filePath)) {
                throw new \InvalidArgumentException(sprintf('Template "%s" not found in "%s" directory', $filename, __DIR__ . $templateDir));
            }
        }
        $template = new PropelTemplate();
        $template->setTemplateFile($filePath);
        $vars = array_merge($vars, ['behavior' => $this]);

        return $template->render($vars);
    }
711852
protected function renderEmail(
        $subject,
        $receiverEmail,
        array $context = []
    ) {
        $message = Swift_Message::newInstance()
            ->setSubject($subject)
            ->setFrom($this->fromEmail)
            ->setTo($receiverEmail)
            ->setContentType('text/html')
            ->setBody(
                $this->templatingEngine->render(
                    $this->template,
                    array_merge(
                        $context,
                        ['layout' => $this->layout]
                    )
                )
            );

        $this
            ->mailer
            ->send($message);

        return $this;
    }
72910
protected function sendEmail($renderedTemplate, $toEmail, $toName, $subject)
    {
        $fromEmail = $this->settingsManager->getParameter('notification.from_email');
        $fromName = $this->settingsManager->getParameter('notification.from_name');

        /** @var \Swift_Mime_Message $message */
        $message = \Swift_Message::newInstance()
            ->setSubject($subject)
            ->setFrom($fromEmail, $fromName)
            ->setTo($toEmail, $toName)
            ->setBody($renderedTemplate, 'text/html')
        ;

        return $this->mailer->send($message);
    }
1207088
public function render(array $templates, Fluent $field): string
    {
        $method = $field->get('method');
        $template = $templates[$method] ?? [$this->presenter, $method];

        if (! \is_callable($template)) {
            throw new InvalidArgumentException("Form template for [{$method}] is not available.");
        }

        return $template($field);
    }
1202182
protected function _outputMessageSafe($template)
    {
        $helpers = ['Form', 'Html'];
        $this->controller->helpers = $helpers;
        $builder = $this->controller->viewBuilder();
        $builder->setHelpers($helpers, false)
            ->setLayoutPath('')
            ->setTemplatePath('Error');
        $view = $this->controller->createView('View');

        $this->controller->response = $this->controller->response
            ->withType('html')
            ->withStringBody($view->render($template, 'error'));

        return $this->controller->response;
    }
1139091
public function setTemplate($templateName)
    {
    	if($templateName instanceof MailTemplate)
    	{
    		$this->template = $templateName;
    	}
    	elseif (!$this->template = $this->mailerManager->getRepository()->findOneBy(array('name' => $templateName)))
    	{
    		$this->template =  $this->mailerManager->createDefault($templateName);
    	}
    
    	return $this;
    }