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 portal-ext.properties file and restart Liferay portal server.


portlet.add.default.resource.check.whitelist=3,56_INSTANCE_0000,
58,82,86,87,88,103,113,145,164,166,170,177,
AjaxMailSender_WAR_AjaxMailSenderportlet


Note:

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{
margin-left:10px;
}
#successMessage{
color:green;
}
#errorMessage{
color:red;
}
</style>
<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>
</div>
<script>
YUI().use('aui-base','aui-io-request', function(A){
A.one("#mailSend").on('click',function(){
A.one("#errorMessage").set("innerHTML","");
A.one("#successMessage").set("innerHTML","");
var receiverName=A.one("#receiverName").get("value");
var receiverEmail=A.one("#receiverEmail").get("value");
var senderName=A.one("#senderName").get("value");
var senderMail=A.one("#senderMail").get("value");
var mailSubject=A.one("#mailSubject").get("value");
var mailBody=A.one("#mailBody").get("value");
//alert("This click Event from AUI");
//aui ajax call to get updated content
A.io.request('http://localhost:8080/?p_p_id=AjaxMailSender_WAR_AjaxMailSenderportlet&p_p_lifecycle=2',{
dataType: 'json',
method: 'GET',
data: {
receiverName:receiverName,
receiverEmail:receiverEmail,
senderName:senderName,
senderMail:senderMail,
mailSubject:mailSubject,
mailBody:mailBody
},
on: {
success: function() {
// response data will be received here
var data = this.get('responseData');
if(data.errorMessage){
A.one("#errorMessage").set("innerHTML",data.errorMessage);
}
if(data.successMessage){
A.one("#successMessage").set("innerHTML",data.successMessage);
}
//console.log(data.successMessage,"data");
}
}
});
});
});
</script>


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 java.io.IOException;
import java.io.PrintWriter;
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 {
@Override
public void serveResource(ResourceRequest resourceRequest,ResourceResponse resourceResponse)throws  IOException, PortletException {
System.out.println("====serveResource========");

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");
logger.info("receiverName:"+receiverName);
logger.info("receiverEmail:"+receiverEmail);
logger.info("senderName:"+senderName);
logger.info("senderMail:"+senderMail);
logger.info("mailSubject:"+mailSubject);
logger.info("mailBody:"+mailBody);
JSONObject responseOject=JSONFactoryUtil.createJSONObject();
boolean success=false;
if(Validator.isEmailAddress(receiverEmail)&&Validator.isEmailAddress(senderMail)){
try {
MailMessage mailMessage=new MailMessage();
mailMessage.setBody(mailBody);
mailMessage.setSubject(mailSubject);
mailMessage.setFrom(new InternetAddress(senderMail,senderName));
mailMessage.setTo(new InternetAddress(receiverEmail,receiverName));
MailServiceUtil.sendEmail(mailMessage);
success=true;
}catch (Exception e) {
e.printStackTrace();
responseOject.put("errorMessage",e.getLocalizedMessage());
success=false;
}

}else{
responseOject.put("errorMessage","One of Email Adresess is not valid");
success=false;
}
if(success){
responseOject.put("successMessage","Your mail has been send successfully");
}
PrintWriter out=resourceResponse.getWriter();
logger.info(responseOject.toString());
out.print(responseOject.toString());
}
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.




Author

22 comments :

  1. Wow, What an Outstanding post. I found this too much informatics. It is what I was seeking for. I would like to recommend you that please keep sharing such type of info.If possible, Thanks. https://zetamatic.com/downloads/woocommerce-mailchimp-newsletter-discount/

    ReplyDelete
  2. Hey what a brilliant post I have come across and believe me I have been searching out for this similar kind of post for past a week and hardly came across this. Thank you very much and will look for more postings from you. Zendable

    ReplyDelete
  3. When your website or blog goes live for the first time, it is exciting. That is until you realize no one but you and your. hostgator black friday sale

    ReplyDelete
  4. It should be noted that whilst ordering papers for sale at paper writing service, you can get unkind attitude. In case you feel that the bureau is trying to cheat you, don't buy term paper from it. webmaster en méxico

    ReplyDelete
  5. Cool you structure, the data is all through stunning and overwhelming, I'll give you a help with trip my site. dark0de market

    ReplyDelete
  6. For getting admirers freed from this string I will address is a free on the web! dark0de market url

    ReplyDelete
  7. I should demand scarcely that its hanging! The blog is edifying correspondingly endlessly make frustrating entitys. dark0de market url

    ReplyDelete
  8. It assists putting with increasing the introductions giving definite data about the organization, its items, and administrations. junk mail senders

    ReplyDelete
  9. The other serious mix-up specialists make with email is that they are exhausting and don't have a clue how to draw in with possibilities. With inferior open rates and despicable commitment it's no big surprise why realtors have gotten away from email advertising. gmx net

    ReplyDelete
  10. Most of the time I don’t make comments on websites, but I'd like to say that this article really forced me to do so. Really nice post! digital marketing agency

    ReplyDelete
  11. Thanks for taking the time to discuss this, I feel strongly that love and read more on this topic. If possible, such as gain knowledge, would you mind updating your blog with additional information? It is very useful for me. denver airport shuttle

    ReplyDelete
  12. Very nice post. I just stumbled upon your weblog and wanted to say that I have truly enjoyed browsing your blog posts. After all I’ll be subscribing to your feed and I hope you write again soon! health and wellness content writer

    ReplyDelete
  13. A great designer is going to know that design and SEO go hand-in-hand. They are being snatched by agencies and large projects. They are creative people that need to think out of the box. A superb web designer ought to be in a position to comprehend the emerging trends in the market, web design dubai

    ReplyDelete
  14. I think this is an informative post and it is very useful and knowledgeable. therefore, I would like to thank you for the efforts you have made in writing this article. Web Design Melbourne

    ReplyDelete
  15. Handling pre-approved offers in the Outlook account using a day by day basis can frequently get quite annoying if the user receives a good amount of spam. People receive lots of mails on their own junk folder, which can include spam, adult content mails, scam messages and thus on. bulk hotmail accounts

    ReplyDelete
  16. Really looking forward to read more. Keep writing.
    WordPress Agency London

    ReplyDelete
  17. a web hosting supplier is an organization that holds or stores your records such that they are accessible web-based https://onohosting.com/

    ReplyDelete
  18. In the event that you have perused our past article, Begin Your Work-From-Home Site With WordPress, you'll definitely know how to introduce WordPress, track down an extraordinary subject and make a scope of pages and posts.
    WP CTA PRO

    ReplyDelete
  19. Past email, online entertainment marketing and web search tool marketing, you can wander into a large group of other digital marketing endeavors. Portable publicizing, radio, TV, electronic bulletins and considerably more are accessible as marketing outlets. christopherross.ca

    ReplyDelete
  20. Writing prompts can be a fun way to collaborate with other writers and share your work. GPT prompt generator for Midjourney

    ReplyDelete
  21. This is really a nice and informative, containing all information and also has a great impact on the new technology. Check it out here buy email lists

    ReplyDelete
  22. This comment has been removed by the author.

    ReplyDelete

Recent Posts

Recent Posts Widget

Popular Posts