« 返回到 Templating Languages

Email Velocity Template

标签: 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 附件
55008 查看
平均 (4 票)
满分为 5,平均得分为 3.25。
评论
讨论主题回复 作者 日期
Very good! Henrique Simoes de Andrade 2012年6月27日 上午9:20
This is a valid method only if you are working... Emanuele Righetto 2012年9月17日 上午3:40
Thanks, this is pretty much what I was looking... Chris Morris 2015年2月11日 上午4:16
Hey thanks a lot for sharing this precious... Jay Trivedi 2012年12月3日 上午4:08
I have same requirement... Can u plz help me ??... Pradip A Bhatt 2014年3月18日 下午11: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);
}
在 12-9-17 上午3: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
在 12-12-3 上午4:08 发帖。
I have same requirement... Can u plz help me ??

How can I access template file in portlet?
在 14-3-18 下午11:50 发帖以回复 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.
在 15-2-11 上午4:16 发帖以回复 Emanuele Righetto