« Voltar para Templating Languages

Email Velocity Template

Tag: velocity

Table of Contents [-]

Introduction#

You may need to send e-mails to users for many reasons, for example a cart checkout confirmation or a registration notice. While sending an email is very easy, coding the body in your java is a no no. The proper way to construct the subject/body of an email in Liferay is to use a velocity template and fill in the variables.

Method #

Here is an example of how this works in Liferay:

Create a new velocity file, lets call ours

 checkout.vm

We will be storing ours in

 ext-impl/src/com/company/portal/cart/dependancies/

The content of the vm is this:

 You have purchased $cart.getItemName() for $cart.getCost()

You now need to create a template for the from address, subject, and from name. You don't absolutely have to do this, as you may want to use the velocity template to generate the subject for you and you may want the from info to change based on what type of item they purchase or something.

I created

 mail.properties

in

 ext-impl/src/com/company/portal/cart/dependancies/

with the content

 fromAddress=test@liferay.com
 fromName=Joe Bloggs
 subject=Thank you for your purchase

Now, inside our java where we build the email (for myself, it's CartLocalServiceImpl)

At this point I assume we have a Cart object being passed in.

 User user = UserLocalServiceUtil.getUserById(cart.getUserId());
 String template = ContentUtil.get("com/company/portal/cart/dependancies/checkout.vm");
 Map<String, Object> variables = new HashMap<String, Object>();
 variables.put("cart", cart);
 Properties props = new Properties();
 ClassLoader classLoader = getClass().getClassLoader();
 InputStream is = classLoader.getResourceAsStream("com/company/portal/cart/dependancies/mail.properties");
 props.load(is);
 String body = VelocityUtil.evaluate(template, variables);
 InternetAddress from = new InternetAddress(
 props.getProperty("fromAddress"),
 props.getProperty("fromName"));
 InternetAddress to = new InternetAddress(
 user.getEmailAddress(),
 user.getFirstName() + StringPool.SPACE +
 user.getLastName());
 MailMessage message = new MailMessage(from, to, props.getProperty("subject"), body, true);
 MailServiceUtil.sendEmail(message);
0 Anexos
55018 Visualizações
Média (4 Votos)
A média da avaliação é 3.25 estrelas de 5.
Comentários
Respostas do tópico Autor Data
Very good! Henrique Simoes de Andrade 27 de Junho de 2012 09:20
This is a valid method only if you are working... Emanuele Righetto 17 de Setembro de 2012 03:40
Thanks, this is pretty much what I was looking... Chris Morris 11 de Fevereiro de 2015 04:16
Hey thanks a lot for sharing this precious... Jay Trivedi 3 de Dezembro de 2012 04:08
I have same requirement... Can u plz help me ??... Pradip A Bhatt 18 de Março de 2014 23:50

Postado em 27/06/12 09:20.
This is a valid method only if you are working on ext-plugin.
The class is into portal-impl.jar and as far as i know we cannot import it into custom portlets, right?
I've writed this snipped, is that way correct?

Map<String, Object> model = new HashMap<String, Object>();
model.put("varName", value);
model.put(........);
String template = ContentUtil.get("it/packageName/notificationMail.vm");
try {
UnsyncStringWriter writer = new UnsyncStringWriter();
VelocityEngineUtil.mergeTemplate(String.valueOf(Math.­random()), template, VelocityEngineUtil.getStandardToolsContext(), writer);
InternetAddress from = new InternetAddress(FROM);
InternetAddress to = new InternetAddress(TO);
MailMessage message = new MailMessage(from, to, NOTIFICATION_MAIL_TITLE, writer.toString(), true);
MailServiceUtil.sendEmail(message);
} catch (Exception e) {
_log.error(e.getMessage());
_log.debug(e.getMessage(), e);
}
Postado em 17/09/12 03:40.
Hey thanks a lot for sharing this precious code!.
What if i have my own vm file and i want my mail body to get attached into it. I tried a lot but it gives me lot of erros. I dropped my vm file in docroot/html/<packagename>/mailtemplate.vm
Now i want to call this file from action class and used it in body of my outgoing mail. Can you help me out.

Thanks in Advance. emoticon Jay
Postado em 03/12/12 04:08.
I have same requirement... Can u plz help me ??

How can I access template file in portlet?
Postado em 18/03/14 23:50 em resposta a Jay Trivedi.
Thanks, this is pretty much what I was looking for.

I've made a slight change and instead of using VelocityEngineUtil.getStandardToolsContext() I used VelocityEngineUtil.getEmptyContext() so I could add custom variables. Using the VelocityEngineUtil.getStandardToolsContext() was returning the same context for all users.
Postado em 11/02/15 04:16 em resposta a Emanuele Righetto.