Exemplo n.º 1
0
 /**
  * Sends an email
  *
  * @param string $recipient
  * @param string $templatePath Path to the twig template
  * @param array $data
  * @param array $blindCopyRecipients Recipients to send bcc
  *
  * @return bool
  */
 public function sendMail($recipient, $templatePath, $data = array(), $blindCopyRecipients = array())
 {
     $tmplData = array_merge($data, array('footerTxt' => $this->templateFooterTxtPath, 'footerHtml' => $this->templateFooterHtmlPath));
     // Load template from twig.
     $template = $this->twig->loadTemplate($templatePath);
     // Merge twig globals so that they also are available in renderBlock.
     $tmplData = $this->twig->mergeGlobals($tmplData);
     // Get subject from block.
     $subject = $template->renderBlock('subject', $tmplData);
     $emailBodyText = $template->renderBlock('body_text', $tmplData);
     $emailBodyHtml = $template->renderBlock('body_html', $tmplData);
     /** @var \Swift_Message $message */
     $message = \Swift_Message::newInstance()->setSubject($subject)->setFrom($this->emailFrom)->setTo($recipient)->setBody($emailBodyText, 'text/plain')->addPart($emailBodyHtml, 'text/html');
     // add blind copy recipients
     foreach ($blindCopyRecipients as $bcc) {
         $message->addBcc($bcc);
     }
     $failedRecipients = array();
     $this->mailer->send($message, $failedRecipients);
     if (count($failedRecipients) > 0) {
         $this->writeLog('Could not send mail to the following recipients: ' . join(', ', $failedRecipients));
         return false;
     }
     return true;
 }
 /**
  * {@inheritdoc}
  */
 public function render(EmailInterface $email, array $data = array())
 {
     if (null !== $email->getTemplate()) {
         $data = $this->twig->mergeGlobals($data);
         /** @var \Twig_Template $template */
         $template = $this->twig->loadTemplate($email->getTemplate());
         if ($template->hasBlock('subject')) {
             $subject = $template->renderBlock('subject', $data);
         } else {
             $twig = new \Twig_Environment(new \Twig_Loader_Array(array()));
             $subjectTemplate = $twig->createTemplate($email->getSubject());
             $subject = $subjectTemplate->render($data);
         }
         $body = $template->renderBlock('body', $data);
     } else {
         $twig = new \Twig_Environment(new \Twig_Loader_Array(array()));
         $subjectTemplate = $twig->createTemplate($email->getSubject());
         $bodyTemplate = $twig->createTemplate($email->getContent());
         $subject = $subjectTemplate->render($data);
         $body = $bodyTemplate->render($data);
     }
     /** @var EmailRenderEvent $event */
     $event = $this->dispatcher->dispatch(SyliusMailerEvents::EMAIL_PRE_RENDER, new EmailRenderEvent(new RenderedEmail($subject, $body)));
     return $event->getRenderedEmail();
 }
 /**
  * {@inheritdoc}
  */
 public function create($templateName, array $context = array())
 {
     if (!isset($this->templateConfigs[$templateName])) {
         throw new TemplateNotExistsException($templateName);
     }
     $templateConfig = $this->templateConfigs[$templateName];
     if (isset($context['utm'])) {
         $templateConfig['utm'] = array_merge($templateConfig['utm'], $context['utm']);
     }
     $context = $this->twig->mergeGlobals($context);
     $template = $this->twig->loadTemplate($templateConfig['template']);
     $subject = $template->renderBlock('subject', $context);
     $htmlBody = $template->renderBlock('body_html', $context);
     $textBody = $template->renderBlock('body_text', $context);
     if (empty($subject)) {
         throw new TemplatePartRequiredException('subject', $templateConfig['template']);
     }
     if (empty($htmlBody)) {
         throw new TemplatePartRequiredException('body_html', $templateConfig['template']);
     }
     $htmlBody = $this->cssToInline($htmlBody);
     $htmlBody = $this->addUtmParams($htmlBody, $templateConfig['host'], $templateConfig['utm']);
     if (empty($textBody) && $templateConfig['generate_text_version']) {
         $textBody = $this->htmlToText($htmlBody);
     }
     $message = \Swift_Message::newInstance()->setSubject($subject)->setBody($htmlBody, 'text/html');
     if (!empty($textBody)) {
         $message->addPart($textBody, 'text/plain');
     }
     return $message;
 }
Exemplo n.º 4
0
 /**
  * sendNotification.
  *
  * @param string $templateName
  * @param array  $context
  */
 private function sendNotification($templateName, array $context)
 {
     $context = $this->twig->mergeGlobals($context);
     $template = $this->twig->loadTemplate($templateName);
     $message = $template->renderBlock('body', $context);
     $notification = new Notification($message);
     $this->notifier->notify($notification);
 }
Exemplo n.º 5
0
 /**
  * {@inheritdoc}
  */
 public function send($template, array $recipients, array $data = array())
 {
     $data = $this->twig->mergeGlobals($data);
     $template = $this->twig->loadTemplate($template);
     $content = $template->renderBlock('content', $data);
     $originator = $template->renderBlock('originator', $data);
     $this->sender->send($recipients[0], $content, $originator);
 }
Exemplo n.º 6
0
 /**
  * @param EmailInterface $email
  * @param array $data
  *
  * @return RenderedEmail
  */
 private function provideEmailWithTemplate(EmailInterface $email, array $data)
 {
     $data = $this->twig->mergeGlobals($data);
     /** @var \Twig_Template $template */
     $template = $this->twig->loadTemplate($email->getTemplate());
     $subject = $template->renderBlock('subject', $data);
     $body = $template->renderBlock('body', $data);
     return new RenderedEmail($subject, $body);
 }
Exemplo n.º 7
0
 /**
  * Sends the email message.
  *
  * @param  string $templateName The template name
  * @param  array  $context      An array of context to pass to the template
  * @param  mixed $fromEmail     The from email
  * @param  mixed $toEmail       The to email
  */
 protected function sendMessage($templateName, array $context, $fromEmail, $toEmail)
 {
     $context = $this->twig->mergeGlobals($context);
     $template = $this->twig->loadTemplate($templateName);
     $subject = $template->renderBlock('subject', $context);
     $body = $template->renderBlock('body', $context);
     $message = \Swift_Message::newInstance()->setSubject($subject)->setFrom($fromEmail)->setTo($toEmail)->setBody($body, 'text/html');
     $this->mailer->send($message);
 }
Exemplo n.º 8
0
 /**
  * sendNotification.
  *
  * @param string $templateName
  * @param array  $context
  * @param array  $options
  */
 private function sendNotification($templateName, array $context, array $options = [])
 {
     $optionsResolver = new OptionsResolver();
     $optionsResolver->setRequired(['color']);
     $options = $optionsResolver->resolve($options);
     $context = $this->twig->mergeGlobals($context);
     $template = $this->twig->loadTemplate($templateName);
     $message = $template->renderBlock('body', $context);
     $this->notifier->send($message, $options);
 }
Exemplo n.º 9
0
 /**
  * {@inheritdoc}
  */
 public function renderBlock(FormView $view, $resource, $blockName, array $variables = array())
 {
     $cacheKey = $view->vars[self::CACHE_KEY_VAR];
     $context = $this->environment->mergeGlobals($variables);
     ob_start();
     // By contract,This method can only be called after getting the resource
     // (which is passed to the method). Getting a resource for the first time
     // (with an empty cache) is guaranteed to invoke loadResourcesFromTheme(),
     // where the property $template is initialized.
     // We do not call renderBlock here to avoid too many nested level calls
     // (XDebug limits the level to 100 by default)
     $this->template->displayBlock($blockName, $context, $this->resources[$cacheKey]);
     return ob_get_clean();
 }
Exemplo n.º 10
0
 /**
  * Loads the template.
  *
  * @param string $template The template relative path
  * @param array  $vars     The template variables
  *
  * @throws TemplateNotFoundException    When template does not exist
  * @throws UnsupportedTemplateException When template format is not supported
  *
  * @return TemplateInterface
  */
 public function loadTemplate($template, array $vars = [])
 {
     if (!$this->supports($template)) {
         throw new UnsupportedTemplateException(sprintf('Template %s is not supported by this engine.', $template));
     }
     try {
         $reference = $this->twig->resolveTemplate($template);
     } catch (\Twig_Error_Loader $exception) {
         throw new TemplateNotFoundException(sprintf('Template %s does not exist.', $template), $exception);
     } catch (\Exception $exception) {
         throw new UnsupportedTemplateException(sprintf('Invalid template %s provided.', $template), $exception);
     }
     return new Template($reference->getTemplateName(), $this->twig->mergeGlobals($vars));
 }
Exemplo n.º 11
0
 /**
  * sendMessage.
  *
  * @param string $templateName
  * @param array  $context
  * @param string $toEmail
  */
 private function sendMessage($templateName, array $context, $toEmail)
 {
     $context = $this->twig->mergeGlobals($context);
     $template = $this->twig->loadTemplate($templateName);
     $subject = $template->renderBlock('subject', $context);
     $textBody = $template->renderBlock('body_text', $context);
     $htmlBody = $template->renderBlock('body_html', $context);
     $message = \Swift_Message::newInstance()->setSubject($subject)->setFrom($this->fromEmail)->setTo($toEmail);
     if (!empty($htmlBody)) {
         $message->setBody($htmlBody, 'text/html')->addPart($textBody, 'text/plain');
     } else {
         $message->setBody($textBody);
     }
     $this->mailer->send($message);
 }
Exemplo n.º 12
0
 /**
  * @param string $templateName
  * @param array  $context
  * @param string $fromEmail
  * @param string $toEmail
  */
 protected function sendMessage($templateName, $context, $toEmail)
 {
     $fromEmail = $this->parameters['from_email'];
     $context = $this->twig->mergeGlobals($context);
     $template = $this->twig->loadTemplate($templateName);
     $subject = $template->renderBlock('subject', $context);
     $textBody = trim($template->renderBlock('text_body', $context));
     $htmlContent = trim($template->renderBlock('content_html_body', $context));
     $htmlBody = trim($template->renderBlock('html_body', $context));
     $message = \Swift_Message::newInstance()->setSubject($subject)->setFrom($fromEmail['address'], $fromEmail['sender_name'])->setTo($toEmail);
     $message->setBody($textBody);
     if (!empty($htmlBody) && !empty($htmlContent)) {
         $message->addPart($htmlBody, 'text/html');
     }
     $this->mailer->send($message);
 }
Exemplo n.º 13
0
 /**
  * @param string $templateName
  * @param array  $context
  * @param string|array $fromEmail
  * @param string|array $toEmail
  */
 protected function sendMessage($templateName, $context, $fromEmail, $toEmail)
 {
     if ($this->noSend) {
         return;
     }
     $context = $this->twig->mergeGlobals($context);
     $template = $this->twig->loadTemplate($templateName);
     $subject = $template->renderBlock('subject', $context);
     $textBody = $template->renderBlock('body_text', $context);
     $htmlBody = $template->renderBlock('body_html', $context);
     /*
     $message = \Swift_Message::newInstance()
         ->setSubject($subject)
         ->setFrom($fromEmail)
         ->setTo($toEmail);
     
     if (!empty($htmlBody)) {
         $message->setBody($htmlBody, 'text/html')
             ->addPart($textBody, 'text/plain');
     } else {
         $message->setBody($textBody);
     }
     
     $this->mailer->send($message);
     */
     mail($toEmail, $subject, $htmlBody, "From: {$fromEmail}" . "\r\n" . 'MIME-Version: 1.0' . "\r\n" . 'Content-type: text/html; charset=iso-8859-1' . "\r\n");
 }
 /**
  * Loads the resources for all blocks in a theme.
  *
  * @param string $cacheKey The cache key for storing the resource.
  * @param mixed  $theme    The theme to load the block from. This parameter
  *                         is passed by reference, because it might be necessary
  *                         to initialize the theme first. Any changes made to
  *                         this variable will be kept and be available upon
  *                         further calls to this method using the same theme.
  */
 protected function loadResourcesFromTheme($cacheKey, &$theme)
 {
     if (!$theme instanceof \Twig_Template) {
         /* @var \Twig_Template $theme */
         $theme = $this->environment->loadTemplate($theme);
     }
     if (null === $this->template) {
         // Store the first \Twig_Template instance that we find so that
         // we can call displayBlock() later on. It doesn't matter *which*
         // template we use for that, since we pass the used blocks manually
         // anyway.
         $this->template = $theme;
     }
     // Use a separate variable for the inheritance traversal, because
     // theme is a reference and we don't want to change it.
     $currentTheme = $theme;
     $context = $this->environment->mergeGlobals(array());
     // The do loop takes care of template inheritance.
     // Add blocks from all templates in the inheritance tree, but avoid
     // overriding blocks already set.
     do {
         foreach ($currentTheme->getBlocks() as $block => $blockData) {
             if (!isset($this->resources[$cacheKey][$block])) {
                 // The resource given back is the key to the bucket that
                 // contains this block.
                 $this->resources[$cacheKey][$block] = $blockData;
             }
         }
     } while (false !== ($currentTheme = $currentTheme->getParent($context)));
 }
Exemplo n.º 15
0
 /**
  * @param EmailInterface $email
  * @param array $data
  *
  * @return RenderedEmail
  */
 private function getRenderedEmail(EmailInterface $email, array $data)
 {
     if (null !== $email->getTemplate()) {
         $data = $this->twig->mergeGlobals($data);
         /** @var \Twig_Template $template */
         $template = $this->twig->loadTemplate($email->getTemplate());
         $subject = $template->renderBlock('subject', $data);
         $body = $template->renderBlock('body', $data);
         return new RenderedEmail($subject, $body);
     }
     $twig = new \Twig_Environment(new \Twig_Loader_Array([]));
     $subjectTemplate = $twig->createTemplate($email->getSubject());
     $bodyTemplate = $twig->createTemplate($email->getContent());
     $subject = $subjectTemplate->render($data);
     $body = $bodyTemplate->render($data);
     return new RenderedEmail($subject, $body);
 }
Exemplo n.º 16
0
 /**
  * @param $block
  * @param array $args
  * @param array $options
  * @return string
  */
 public function displayBlock($block, array $args = [], array $options = [])
 {
     $vars = array_merge($this->extension->getGlobalVars(), compact('args'));
     if (isset($options['vars'])) {
         $vars = array_merge($vars, $options['vars']);
     }
     if (isset($options['args'])) {
         $min = min([count($args), count($options['args'])]);
         for ($i = 0; $i < $min; $i++) {
             if (isset($options['args'][$i]) && isset($args[$i])) {
                 $vars[$options['args'][$i]] = $args[$i];
             }
         }
     }
     $theme = $vars['theme'] = isset($vars['theme']) ? $vars['theme'] : $this->theme ?: $this->extension->getTemplate();
     $context = $this->environment->mergeGlobals($vars);
     $context = $this->extension->beforeRender($block, $context);
     ob_start();
     $this->_getTemplateResource($theme)->displayBlock($block, $context);
     return ob_get_clean();
 }
Exemplo n.º 17
0
 function it_renders_an_email(\Twig_Environment $twig, \Twig_Template $template, EmailInterface $email, EmailRenderEvent $event, EventDispatcherInterface $dispatcher, RenderedEmail $renderedEmail)
 {
     $this->setEventDispatcher($dispatcher);
     $twig->mergeGlobals([])->shouldBeCalled()->willReturn([]);
     $email->getTemplate()->shouldBeCalled()->willReturn('MyTemplate');
     $twig->loadTemplate('MyTemplate')->shouldBeCalled()->willReturn($template);
     $template->renderBlock('subject', [])->shouldBeCalled();
     $template->renderBlock('body', [])->shouldBeCalled();
     $dispatcher->dispatch(SyliusMailerEvents::EMAIL_PRE_RENDER, Argument::type(EmailRenderEvent::class))->shouldBeCalled()->willReturn($event);
     $event->getRenderedEmail()->shouldBeCalled()->willReturn($renderedEmail);
     $this->render($email, [])->shouldReturn($renderedEmail);
 }
Exemplo n.º 18
0
 /**
  * @param \FSi\Bundle\AdminSecurityBundle\Model\UserPasswordResetInterface $user
  * @param \Twig_Environment $twig
  * @param \Twig_Template $template
  * @param \Swift_Mailer $mailer
  * @param \Symfony\Component\HttpFoundation\RequestStack $requestStack
  */
 function it_should_render_template($user, $twig, $template, $mailer, $requestStack)
 {
     $request = new Request(array(), array(), array(), array(), array(), array('HTTP_USER_AGENT' => 'user agent', 'REMOTE_ADDR' => '192.168.99.99'));
     $requestStack->getMasterRequest()->willReturn($request);
     $templateParameters = array('user' => $user, 'ip' => '192.168.99.99', 'user_agent' => 'user agent');
     $twig->mergeGlobals($templateParameters)->willReturn($templateParameters);
     $twig->loadTemplate('mailer-template.html.twig')->willReturn($template);
     $template->renderBlock('subject', $templateParameters)->willReturn('subject string');
     $template->renderBlock('body_html', $templateParameters)->willReturn('body string');
     $user->getEmail()->willReturn('*****@*****.**');
     $mailer->send(Argument::allOf(Argument::type('\\Swift_Message'), Argument::which('getSubject', 'subject string'), Argument::which('getTo', array('*****@*****.**' => null)), Argument::which('getFrom', array('*****@*****.**' => null)), Argument::which('getReplyTo', array('*****@*****.**' => null)), Argument::which('getBody', 'body string')))->willReturn(1);
     $this->sendPasswordResetMail($user)->shouldReturn(1);
 }
Exemplo n.º 19
0
 /**
  * Sends an email. Expects twig blocks `body_html`, `body_plain` and `subject`.
  *
  * @return boolean
  * @author Marcel Eschmann
  **/
 public function send($template, $context, $from, $to, $bcc = null)
 {
     // load template and enable globals
     $context = $this->twig->mergeGlobals($context);
     $template = $this->twig->loadTemplate($template);
     // render email parts
     $subject = $template->renderBlock('subject', $context);
     $plain = $template->renderBlock('body_plain', $context);
     $html = $template->renderBlock('body_html', $context);
     // create message
     $message = \Swift_Message::newInstance()->setSubject($subject)->setFrom($from)->setTo($to)->setBody($plain, 'text/plain')->addPart($html, 'text/html');
     if ($bcc) {
         $message->setBcc($bcc);
     }
     // send email via swiftmailer
     try {
         $this->mailer->send($message);
     } catch (\Exception $e) {
         return false;
     }
     return true;
 }
 /**
  * @param DataSourceViewInterface $view
  * @param array $contextVars
  * @param $availableBlocks
  * @return string
  */
 private function renderTheme(DataSourceViewInterface $view, array $contextVars = array(), $availableBlocks = array())
 {
     $templates = $this->getTemplates($view);
     $contextVars = $this->environment->mergeGlobals($contextVars);
     ob_start();
     foreach ($availableBlocks as $blockName) {
         foreach ($templates as $template) {
             if (false !== ($template = $this->findTemplateWithBlock($template, $blockName))) {
                 $template->displayBlock($blockName, $contextVars);
                 return ob_get_clean();
             }
         }
     }
     return ob_get_clean();
 }
Exemplo n.º 21
0
 public function renderBlock(\Twig_Environment $twig, $block, array $parameters = array())
 {
     $template = $twig->loadTemplate('blocks.html.twig');
     $parameters = $twig->mergeGlobals($parameters);
     $level = ob_get_level();
     ob_start();
     try {
         $rendered = $template->renderBlock($block, $parameters);
         ob_end_clean();
         return $rendered;
     } catch (\Exception $e) {
         while (ob_get_level() > $level) {
             ob_end_clean();
         }
         throw $e;
     }
 }
 /**
  * Render the theme block for the datagrid.
  *
  * The resolved template and block are cached for future reference.
  *
  * @param DatagridView $datagridView
  * @param array        $contextVars
  * @param array        $availableBlocks
  * @param string|null  $cacheKey
  *
  * @throws \Exception
  * @throws \Twig_Error
  * @throws \Twig_Error_Runtime
  *
  * @return string
  */
 private function renderTheme(DatagridView $datagridView, array $contextVars = [], $availableBlocks = [], $cacheKey = null)
 {
     $contextVars = $this->environment->mergeGlobals($contextVars);
     if ($cacheKey && isset($this->blocksCache[$cacheKey])) {
         ob_start();
         $this->blocksCache[$cacheKey][0]->displayBlock($this->blocksCache[$cacheKey][1], $contextVars);
         return ob_get_clean();
     }
     $templates = $this->getTemplates($datagridView);
     ob_start();
     foreach ($availableBlocks as $blockName) {
         foreach ($templates as $template) {
             if (false !== ($template = $this->findTemplateWithBlock($template, $blockName))) {
                 $template->displayBlock($blockName, $contextVars);
                 if ($cacheKey) {
                     $this->blocksCache[$cacheKey] = [$template, $blockName];
                 }
                 return ob_get_clean();
             }
         }
     }
     return ob_get_clean();
 }
Exemplo n.º 23
0
 /**
  * Internal method for block rendering.
  */
 private function renderTemplateBlock(\Twig_Environment $env, \Twig_Template $template, $block, array $context = array())
 {
     $context = $env->mergeGlobals($context);
     $level = ob_get_level();
     ob_start();
     try {
         $rendered = $template->renderBlock($block, $context);
         ob_end_clean();
         return $rendered;
     } catch (\Exception $e) {
         while (ob_get_level() > $level) {
             ob_end_clean();
         }
         throw $e;
     }
 }