Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Render html email template

I have just finished switching the view from twig engine to volt. All works great. However, I have an email rendering component that twig was doing that I can't resolve now

I want to load the html email templates, render 3 variabls, and send it as body. The twig set up was like this:

    $loader = new \Twig_Loader_Filesystem($this->config->application->viewsDir.'/mail');
  $twig = new \Twig_Environment($loader);

  $template = $twig->loadTemplate($name);

  return $template;

The content of the html file is very basic:

Hello {{ user }},

Your password has been reset:

{{ base_url }}/resetPass/{{ newpass }}

Now, I tried so many way to do it using phtml and volt, but all failed. First I tried this, but it returns an empty value

$this->view->getRender($this->config->application->viewsDir . '/mail', $name, $params, function ($view) { $view->setRenderLevel(View::LEVEL_LAYOUT); }); return $this->view->getContent();

I also, tried this, but it returns and unchaged value as string.

    $view = new SimpleView();
  $view->setViewsDir($this->config->application->viewsDir);
  echo $view->render('mail/'.$name,  array(
                      "user"   => $user->firstName . ' ' . $user->lastName,
                      "newpass" => $new_pass,
                      "base_url" => 'https://' . $_SERVER['SERVER_NAME'].':'.$_SERVER['SERVER_PORT']
                  ));

I'm still using phalcon 2, but I'm going to upgrade as soon as I fixed this issue.

Any help, would be appriciated.



107.4k
Accepted
answer

Example from my app:

        /** @var View $view */
        $view = $this->view;
        $host = $this->request->getHttpHost();
        $scheme = $this->request->getScheme();
        $view->setVars(['user' => $user, 'host' => $host, 'scheme' => $scheme]);
        $view->start();
        $view->render('mail', 'forgot');
        $view->finish();
        $html = $view->getContent();
        $message = Swift_Message::newInstance('Przypomnienie hasła')
            ->setFrom('[email protected]', 'Centrum Edukacji Suzuki')
            ->setTo($user->getEmail(), $user->getFirstName()." ".$user->getLastName())
            ->setBody($html, 'text/html')
            ->addPart(strip_tags($html), 'text/plain');

        return $message;

This is much shorter, and acheives the same result:

// Controller context
$html = $this->view->getRender('mail', $name, [
    'user' => 'John Doe',
    'other_var' => 'something'
]);


719
edited Mar '17

I tried it like this, still getting empty body email

$view = $this->view;
$view->setVars(array(
"user"   => $user->firstName . ' ' . $user->lastName,
"newpass" => $newPass,
"base_url" => 'https://' . $_SERVER['SERVER_NAME'].':'.$_SERVER['SERVER_PORT']
));
$view->start();
$view->render('mail', 'newPassrequest');
$view->finish();
$html = $view->getContent();

Example from my app:

```php /** @var View $view */ $view = $this->view; $host = $this->request->getHttpHost(); $scheme = $this->request->getScheme(); $view->setVars(['user' => $user, 'host' => $host, 'scheme' => $scheme]); $view->start(); $view->render('mail', 'forgot'); $view->finish(); $html = $view->getContent(); $message = SwiftMessage::newInstance('Przypomnienie hasła') ->setFrom('[email protected]', 'Centrum Edukacji Suzuki') ->setTo($user->getEmail(), $user->getFirstName()." ".$user->getLastName()) ->setBody($html, 'text/html') ->addPart(striptags($html), 'text/plain');

   return $message;

```



719

Still getting an empty string

This is much shorter, and acheives the same result:

```php // Controller context $html = $this->view->getRender('mail', $name, [ 'user' => 'John Doe', 'other_var' => 'something' ]); ```

edited Mar '17

And you sure that you have in your views directory mail folder and newPassrequest.volt?

For me code above works totally fine.



719

It was solved by renaming the mail templates from html to volt, I thought it would work on html as well.

edited Apr '17

It was solved by renaming the mail templates from html to volt, I thought it would work on html as well.

It could work like that, but only if you register the volt engine with that extension in the service definitions :]



78
edited 5d ago

Hi all ,

I have same requirement but i dont have files, i need to render this from string not a file. i tried to achive that but with no luck ,is this is possible?

compileString function doesn't take params so it dosent help me.

Thanks

I dont think it's possible, behind the scenes phalcon will parse the volt file and create a plain cached php file, which will be used for the final view rendering.

(compileString is the part that creates the php version, that's why it has no params)