« Zurück zu Templating Languages

Email Velocity Template

Asset-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 Anhänge
55009 Angesehen
Durchschnitt (4 Stimmen)
Die durchschnittliche Bewertung ist 3.25 von max. 5 Sternen.
Kommentare
Antworten im Thread Autor Datum
Very good! Henrique Simoes de Andrade 27. Juni 2012 09:20
This is a valid method only if you are working... Emanuele Righetto 17. September 2012 03:40
Thanks, this is pretty much what I was looking... Chris Morris 11. Februar 2015 04:16
Hey thanks a lot for sharing this precious... Jay Trivedi 3. Dezember 2012 04:08
I have same requirement... Can u plz help me ??... Pradip A Bhatt 18. März 2014 23:50

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);
}
Gepostet am 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
Gepostet am 03.12.12 04:08.
I have same requirement... Can u plz help me ??

How can I access template file in portlet?
Gepostet am 18.03.14 23:50 als Antwort auf 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.
Gepostet am 11.02.15 04:16 als Antwort auf Emanuele Righetto.