How to process TXT e-mail template with Thymeleaf?
Asked Answered
S

2

16

I'm trying to send plain text email from Spring application with Thymeleaf.

This is my e-mail service:

@Override
public void sendPasswordToken(Token token) throws ServiceException {
    Assert.notNull(token);

    try {

        Locale locale = Locale.getDefault();

        final Context ctx = new Context(locale);
        ctx.setVariable("url", url(token));

        // Prepare message using a Spring helper
        final MimeMessage mimeMessage = mailSender.createMimeMessage();

        final MimeMessageHelper message = new MimeMessageHelper(
                mimeMessage, false, SpringMailConfig.EMAIL_TEMPLATE_ENCODING
        );

        message.setSubject("Token");
        message.setTo(token.getUser().getUsername());

        final String content = this.textTemplateEngine.process("text/token", ctx);
        message.setText(content, false);

        mailSender.send(mimeMessage);

    } catch (Exception e) {
        throw new ServiceException("Token has not been sent", e);
    }
}

email is sent and delivered into mailbox.

This is my plain text email template:

Token url: ${url}

but in delivered mailbox url variable is not replaced with it's value. Why?

When I use html classic HTML Thymeleaf syntax, variable is replaced:

<span th:text="${url}"></span>

What is proper syntax for e-mail text template?

Suffer answered 11/1, 2017 at 20:16 Comment(2)
Why is it downvoted?Suffer
thymeleaf.org/doc/tutorials/3.0/…Ripleigh
B
25

You can use Thymeleaf in plain text mode as well, like in this example:

Dear [(${customer.name})],

This is the list of our products:
[# th:each="p : ${products}"]
   - [(${p.name})]. Price: [(${#numbers.formatdecimal(p.price,1,2)})] EUR/kg
[/]
Thanks,
  The Thymeleaf Shop

That means you can have a text-file with just this in it:

Token url: [(${url})]

Take a look at the complete documentation of those features here:

https://github.com/thymeleaf/thymeleaf/issues/395

Edit

As mentioned in a comment, make sure to use version >= 3.0 of Thymeleaf:

<properties>
  <thymeleaf.version>3.0.3.RELEASE</thymeleaf.version>
  <thymeleaf-layout-dialect.version>2.1.2</thymeleaf-layout-dialect.version>
</properties>
Bin answered 12/1, 2017 at 21:21 Comment(2)
that is a nice addition of version 3, as states the issue. thanks for posting it.Nuclei
Yes, you are right. I used Thymeleaf starter dependency package from Spring and it added Thymeleaf in version 2.x. When I removed version 2 and upgraded to version 3 the syntax is working and, as you mentioned, it's recommended way.Suffer
N
3

Use an HTML template like this and it will procude plain text.

<html xmlns:th="http://www.thymeleaf.org" th:inline="text" th:remove="tag">
Token url: [[${url}]]
</html>

another way, still using html could be like this

<span th:text="Token url:" th:remove="tag"></span><span th:text="${url}" th:remove="tag"></span>

What you are looking is to produce plain text so thymeleaf will return that for you.

Nuclei answered 11/1, 2017 at 20:37 Comment(2)
this is a proper way to do it. I mean a valid way to do it, im not sure if this is the official or not, I know it works.Nuclei
Note that when using the [[]] syntax the text will be html-escaped: thymeleaf.org/doc/tutorials/3.0/…Gourmet

© 2022 - 2024 — McMap. All rights reserved.