« 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 添付ファイル
55000 参照数
平均 (4 投票)
平均評価は3.25星中の5です。
コメント
コメント 作成者 日時
Very good! Henrique Simoes de Andrade 2012/06/27 9:20
This is a valid method only if you are working... Emanuele Righetto 2012/09/17 3:40
Thanks, this is pretty much what I was looking... Chris Morris 2015/02/11 4:16
Hey thanks a lot for sharing this precious... Jay Trivedi 2012/12/03 4:08
I have same requirement... Can u plz help me ??... Pradip A Bhatt 2014/03/18 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);
}
投稿日時:12/09/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/03 4:08
I have same requirement... Can u plz help me ??

How can I access template file in portlet?
Jay Trivediへのコメント。投稿日時:14/03/18 23:50
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.
Emanuele Righettoへのコメント。投稿日時:15/02/11 4:16