Monday, November 23, 2015

Email forms using Liferay Web Content

Liferay have mail API to send emails in portlets and liferay also have web forms portlet to create email forms.

If we want create custom email form we need to develop custom portlet and need to design email form as for our requirement and its needed some development effort.

If we use web form portlet then we cannot make much look and feel design to forms and its separate portlet and we cannot use much convenient way in web content combination.

Out of box we can send email using web content so that we design email forms with good look and feel and we can send emails.

Here we have some custom portlet that will take care the email sending functionality we will use Ajax based mechanism to send email on behalf of our mail sender portlet through web content.

We will design email form in Liferay web content and we use AUI Ajax call to send emails.

The following are the steps you need to follow.

Step: 1

Download portlet from following location and deploy in your portal server and it should be successfully deployed.

Step: 2

Add following property in your file and restart Liferay portal server.



The above property should in single line in the properties file

Step: 3

Drag and Drop Liferay web content Display portlet in your desired page.

Create Liferay web content using following html content and publish it.

<style type="text/css">.mail-container{
<div class="mail-container">
<div id="successMessage">&nbsp;</div>

<div id="errorMessage">&nbsp;</div>

<form action="#" enctype="text/plain" method="post">Receiver Name:<br />
<input id="receiverName" name="receiverName" type="text" value="" /><br />
Receiver Email:<br />
<input id="receiverEmail" name="receiverEmail" type="text" value="" /><br />
Sender Name:<br />
<input id="senderName" name="senderName" type="text" value="" /><br />
Sendw Email:<br />
<input id="senderMail" name="senderMail" type="text" value="" /><br />
Subject<br />
<input id="mailSubject" name="mailSubject" type="text" value="" /><br />
Mail Body<br />
<textarea cols="50" id="mailBody" name="mailBody" rows="4"></textarea><br />
<br />
<input id="mailSend" type="button" value="mailSend" /> <input type="reset" value="Reset" />&nbsp;</form>
YUI().use('aui-base','aui-io-request', function(A){"#mailSend").on('click',function(){"#errorMessage").set("innerHTML","");"#successMessage").set("innerHTML","");
//alert("This click Event from AUI");
//aui ajax call to get updated content'http://localhost:8080/?p_p_id=AjaxMailSender_WAR_AjaxMailSenderportlet&p_p_lifecycle=2',{
dataType: 'json',
method: 'GET',
data: {
on: {
success: function() {
// response data will be received here
var data = this.get('responseData');

Finally you can see email form in web content display portlet as follows

You can see success message after send email

You can see error message if some get failed

The following portlet Java class we can customize the way we required.

package com.iamsuraj.mailsender;
import javax.mail.internet.InternetAddress;
import javax.portlet.PortletException;
import javax.portlet.ResourceRequest;
import javax.portlet.ResourceResponse;
import com.liferay.mail.service.MailServiceUtil;
import com.liferay.portal.kernel.json.JSONFactoryUtil;
import com.liferay.portal.kernel.json.JSONObject;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.mail.MailMessage;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.util.bridges.mvc.MVCPortlet;
public class AJaxMailSenderPortlet extends MVCPortlet {
public void serveResource(ResourceRequest resourceRequest,ResourceResponse resourceResponse)throws  IOException, PortletException {

String receiverName=ParamUtil.getString(resourceRequest,"receiverName");
String receiverEmail=ParamUtil.getString(resourceRequest,"receiverEmail");
String senderName=ParamUtil.getString(resourceRequest,"senderName");
String senderMail=ParamUtil.getString(resourceRequest,"senderMail");
String mailSubject=ParamUtil.getString(resourceRequest,"mailSubject");
String mailBody=ParamUtil.getString(resourceRequest,"mailBody");"receiverName:"+receiverName);"receiverEmail:"+receiverEmail);"senderName:"+senderName);"senderMail:"+senderMail);"mailSubject:"+mailSubject);"mailBody:"+mailBody);
JSONObject responseOject=JSONFactoryUtil.createJSONObject();
boolean success=false;
try {
MailMessage mailMessage=new MailMessage();
mailMessage.setFrom(new InternetAddress(senderMail,senderName));
mailMessage.setTo(new InternetAddress(receiverEmail,receiverName));
}catch (Exception e) {

responseOject.put("errorMessage","One of Email Adresess is not valid");
responseOject.put("successMessage","Your mail has been send successfully");
PrintWriter out=resourceResponse.getWriter();;
private static final Log logger = LogFactoryUtil.getLog(AJaxMailSenderPortlet.class);

Important Points

 This is simple example portlet and we can develop like the way we wanted and you can also handle server side validation in the code.

The web content form is a simple form not much style and we can design with good look and feel with validations.

Web content I have hard coded the host name(localhost) if your host name is changed then please change the host name in the web content ajax URL.

To send email in the portal we must configure mail server in the Liferay portal and you can find mail configuration in the following post.


Saturday, November 21, 2015

Liferay User Profile Input Fields Disable/Enable

Liferay have given way to enable/disable user my account input fields through the properties and this is good way to enable/disable fields rather than create some JSP hooks.

Generally when we want change some User Interface in Liferay Portal Portlets we will create JSP hooks or JSP Adopter hooks to handle these kind of scenarios.

But Liferay have given some properties so that we can enable or disable user my account/my profile input fields.

Assume some scenario some time we don’t want give edit option to change email address or screen name of user in the my account page and this scenarios we can handle it from properties.

Assume some time we import all accounts from LDAP to Liferay database but we don’t want give some edit permission to some important fields like screen name, email address.

Liferay used some kind of logic that based on domain names in email address so that it can control the input fields edit/no-editable permissions through defined properties.

Generally create account page as follows and most of the fields are editable.

Scenario: 1

Assume all email addresses with give editable option other email address ends with any other domain we should not give edit options in my account input fields.

Scenario: 2

Only email address not editable in the account page.

Scenario: 3

Assume email address input only editable if it ends with and all other email address it should not be editable and other fields are editable.

All these scenarios we can achieve just change the properties so that we can achieve.
You can add following properties in the file then restart server so that you see result in the user my account page.

Scenario: 1

Assume all email addresses with give editable option other email address ends with any other domain we should not give edit options in my account input fields.


You can pass multiple domain names as COMMA separated values so that input fields editable to particulate domain email address users input fields.

Fields Editable for email address ends with

All fields non editable if the email address not ends with

 Scenario: 2

Only email address not editable in the account page

The following case email input filed not editable for and but other all input fields are editable.


The following account email address ends with only email input is not editable all other are editable

The following account email address ends with only email input is not editable all other are editable

Scenario: 3

Assume email address input only editable if it ends with and all other email address it should not be editable and other fields are editable.


The following account email address ends with so all input are editable including email input

The following account email address not ends with so email input is not editable and other input fields are editable


The properties you have to place in file and this file available in Liferay Home Directory/Portal Server Parent Directory

We can also use “” to place the properties and this file available in Liferay Home Directory/Portal Server Parent Directory

All the cases administrator have power to edit their fields even we disable.

The following is the description about field.editable.user.types

Input a list of comma delimited user types. Users that match one of these user types have permission to edit all of their own fields. Valid user types are "user-with-mx" and "user-without-mx". A value of "user-with-mx" is a user who has an email address that matches the company mail suffix, and "user-without-mx" is a user who does not have an email address that matches the company mail suffix.



Popular Posts

Recent Posts

Recent Posts Widget