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 .


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:-


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:-