We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

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.



145.0k
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'
]);


3.6k
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:

       /** @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;


3.6k

Still getting an empty string

This is much shorter, and acheives the same result:

// 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.



3.6k

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 Oct '17

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)