Custom mailto: link using Thymeleaf
Asked Answered
B

2

10

I am working with project using Spring and Thymeleaf. I have list of users. I would like to create contact link using mailto:

<a href="mailto:[email protected]">

I am displaying list of users like this:

<tr th:each="users : ${users}">
    <td th:text="${users.id}"></td>
    <td th:text="${users.firstname}"></td>
    <td th:text="${users.lastname}"></td>
    <td th:text="${users.email}"></td>
</tr>

So what I would like to achieve is that mailto suits every new professor and his email without me writing it manually. I am thinking if is possible to write something like this:

<td><a th:href = "mailto:${users.email}">
Booboo answered 15/8, 2017 at 14:31 Comment(0)
L
30

Try the following code:

<td><a th:href="'mailto:' + ${users.email}">Send email </a></td>
Louvain answered 15/8, 2017 at 15:17 Comment(2)
That was it! Thanks :)Booboo
See my answer below how to do this safely using link syntax (with proper escaping done by framework)Takamatsu
T
4

You can do this more safely and with extra parameters using link syntax:

<td><a th:href="@{mailto:{to}(to=${users.email})}">Send email </a></td>

This allows you to also pass CC, Subject and Body:

<td><a th:href="@{mailto:{to}(to=${users.email},subject=${subj},body=${body})}">Send email </a></td>
Takamatsu answered 9/6, 2021 at 21:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.