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

Layout for mail template

I use Phalcon\Mailer for sending mails. Can I use the layout for the letter template?

Sure you can make your own layout/template to use in all your messages



13.8k

It works basically as any other template. Here's an example how i use/solved it.

# controller - genrate content for e-mail
$content = $this->view->getRender('emails', 'reset-password', ['token' => $uuid, 'url' => $this->config->application->domainUri]);
# views/emails
<html>
    <body>
        <p>We received a request to reset your password.</p>
        <p>Use the following link to reset your password: <a href="{{ url }}{{ url("auth/reset/" ~ token) }}" >{{ url }}{{ url("auth/reset/" ~ token) }}</a></p>
    </body>
</html>


1.5k

If I want to send a mail in a model? How then do I prepare the content?

It works basically as any other template. Here's an example how i use/solved it.

# controller - genrate content for e-mail
$content = $this->view->getRender('emails', 'reset-password', ['token' => $uuid, 'url' => $this->config->application->domainUri]);
# views/emails
<html>
   <body>
      <p>We received a request to reset your password.</p>
       <p>Use the following link to reset your password: <a href="{{ url }}{{ url("auth/reset/" ~ token) }}" >{{ url }}{{ url("auth/reset/" ~ token) }}</a></p>
   </body>
</html>


1.5k

What do you say about this way? Template:

<?php ob_start();?>

<!-- Template mail -->

<?php $content = ob_get_contents();

ob_end_clean();

$this->render(DIR.'/../layouts/main.phtml', ['content' => $content]);

Layout:

<html>

<head></head>

<body><?=$content?></body>

</html>

Send mail:

$mail = $this->getDI()->getMail();

$message = $mail->createMessageFromView('user/activate', $params)->to($this->email, $this->username)

->subject('Hello world!')

->send();