Tuesday, September 22, 2015

Sending Mail in Liferay Using Templates(tmpl) Files


In the Previous blog we see how to we Configure mail in Liferay  and how to send a mail programmatically. When we send a mail we have to provide content in setBody() method either in plain text or in the HTML format. But what happen if we want to send a mail with dynamic content in body part and in proper HTML format.In this case we create templates with HTML tags and just call the tmpl files and replace the variables declared inside tmpl files with actual data.

So lets start this step by step:-


Step 1:-Create Liferay Project and Portlet
Create a Liferay Project in eclipse and then create a portlet in the Project.For theses you may refer Step 1 and Step 2 of my previous blog Here .

Step 2:-Create Template in the Project
Create a folder content inside src .After that inside this create a file with name exam.tmpl and paste this content .

<h3>Dear [$NAME$],</h3>
<br/>
<h2><font color=red>[$RESULT$]</font></h2>
<br/>
<h3>You got [$PERCENTAGE$] in the [$EXAM$]</h3>
<br/>
<h3>Thanks for Appearing in the Exam</h3>
<br/>
<h3>
Best Regards,<br/>
Liferay is Easy
</h3>
view raw exam.tmpl hosted with ❤ by GitHub

Explanation:-
Here in this file we create a complete HTML structure and declare 3 variables NAME , RESULT ,PERCENTAGE and EXAM which we can set programmatically.




Step 3:-Use the Template 
Open your Controller ie java file and Paste this:-

package com.test;
import java.io.IOException;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.portlet.PortletException;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import com.liferay.mail.service.MailServiceUtil;
import com.liferay.portal.kernel.mail.MailMessage;
import com.liferay.portal.kernel.util.StringUtil;
import com.liferay.util.ContentUtil;
import com.liferay.util.bridges.mvc.MVCPortlet;
public class Demo extends MVCPortlet {
@Override
public void doView(RenderRequest renderRequest,RenderResponse renderResponse) throws IOException, PortletException {
sendMailUsingTemplate();
super.doView(renderRequest, renderResponse);
}
public void sendMailUsingTemplate() {
InternetAddress fromAddress = null;
InternetAddress toAddress = null;
String body = ContentUtil.get("/content/exam.tmpl", true);
body = StringUtil.replace(body, new String[] { "[$NAME$]","[$RESULT$]","[$PERCENTAGE$]","[$EXAM$]" }, new String[] { "Ravi", "CONGRATULATION" ,"80%" , "CCLP"});
try {
fromAddress = new InternetAddress("aa665845@gmail.com");
toAddress = new InternetAddress("sendersmail@gmail.com");
MailMessage mailMessage = new MailMessage();
mailMessage.setTo(toAddress);
mailMessage.setFrom(fromAddress);
mailMessage.setSubject("Send mail by Using Tempelate");
mailMessage.setBody(body);
mailMessage.setHTMLFormat(true);
MailServiceUtil.sendEmail(mailMessage);
System.out.println("Send mail by Using Tempelate");
} catch (AddressException e) {
e.printStackTrace();
}
}
}
view raw Demo.java hosted with ❤ by GitHub

Explanation:-
Inside sendMailUsingTemplate method we fetch the tmpl file using ContentUtil Class and then replace the 4 variables with our values.

Step 4:-Check Output 
As sendMailUsingTemplate method is called in doView method just deploy the portlet and add to page and check your inbox .You will receive a mail as:-



Project Structure:-







Hope this will Help....

You can Download Source code from  Sending Mail Using Templates.

Related Post:-

No comments:

Post a Comment

Total Pageviews

1042053

Number Of Unique Visitor

Free counters!