Forums

Home » Liferay Portal » English » 3. Development

Combination View Flat View Tree View
Threads [ Previous | Next ]
toggle
Vaibhav Mittal
Mail through WCM (template's velocity)
December 7, 2012 8:37 AM
Answer

Vaibhav Mittal

Rank: Junior Member

Posts: 54

Join Date: July 16, 2012

Recent Posts

Hi All,

I have a structure and a template (applied to structure). Now when i add a web content and chage there structure and template from default to my own st. and tem. I can see the data is coming from the structure the way i have defined in my vm template.
Now my requirement is to add a form in template with two fields,one textbox and a submit button which i can fulfill using form tag in velocity. But how can i add action url so that when i click on submit button an email comes to the specified address ( generating through a loop defined in template which is calling data from structure) and form data gets saved in the database.
I can't use form portlet as i want that functionality in my template's velocity to access email id dynamically.

Regards,
Vaibhav Mittal
Vaibhav Mittal
RE: Mail through WCM (template's velocity)
December 7, 2012 8:39 AM
Answer

Vaibhav Mittal

Rank: Junior Member

Posts: 54

Join Date: July 16, 2012

Recent Posts

1 out of two issues i have solved. What i did z add the below code into my template

Name: <input id="form" type="text" name="name"/><br/>
<input id="button" type="button" value="Send"/>

then send a ajax call (written in template) on jsp page on click of above button

<script>
$(document).ready(function(){
$("#button").click(function(){
var x=document.getElementById("form").value;
$.ajax({type: "POST",
url:"/your theme name/jsp/form.jsp",
data: {a:x} ,
success:function(result){$("#div1").html(result);}
});
});
});
</script>

and in jsp i write my code for sending mail after configuring smtp server.

<%@ page import="com.liferay.mail.service.MailServiceUtil" %>
<%@ page import="com.liferay.portal.kernel.mail.MailMessage" %>
<%@ page import="com.liferay.util.mail.MailEngine" %>
<%@ page import="com.liferay.util.mail.MailEngineException" %>
<%@ page import="javax.mail.internet.InternetAddress" %>

<% String str = request.getParameter("a"); %>
<%= str %>

<% try{
InternetAddress to = new InternetAddress("abc@gmail.com");
InternetAddress from = new InternetAddress("xyz@gmail.com");
String sub = new String("test");
String body = new String("test-mail");
MailMessage msg = new MailMessage();
msg.setTo(to);
msg.setFrom(from);
msg.setBody(body);
msg.setSubject(sub);
MailServiceUtil.sendEmail(msg);
MailEngine.send(from, to, sub, body);
}
catch (Exception e) {
out.println("can't send mail");
}
%>

Don't forget tocopy the commons-lang.jar-File into your portlet\theme in the following folder: $\docroot\WEB-INF\lib\ from the folder: $\webapps\ROOT\WEB-INF\lib\ or add dependency if you are using maven.