« Back to Templating Languages

Email Velocity Template

Tags: 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 Attachments
55010 Views
Average (4 Votes)
The average rating is 3.25 stars out of 5.
Comments
Threaded Replies Author Date
Very good! Henrique Simoes de Andrade June 27, 2012 9:20 AM
This is a valid method only if you are working... Emanuele Righetto September 17, 2012 3:40 AM
Thanks, this is pretty much what I was looking... Chris Morris February 11, 2015 4:16 AM
Hey thanks a lot for sharing this precious... Jay Trivedi December 3, 2012 4:08 AM
I have same requirement... Can u plz help me ??... Pradip A Bhatt March 18, 2014 11:50 PM

Posted on 6/27/12 9:20 AM.
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);
}
Posted on 9/17/12 3:40 AM.
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
Posted on 12/3/12 4:08 AM.
I have same requirement... Can u plz help me ??

How can I access template file in portlet?
Posted on 3/18/14 11:50 PM in reply to 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.
Posted on 2/11/15 4:16 AM in reply to Emanuele Righetto.