logo

MMBase tips

A form to send mail

tip #6 / Thu 10 Dec 2009 / André / 0 comments

In my previous post I showed how easy it is to create an email node and send it. This time I'll make it into a complete example which includes a simple form.

If you care to validate your user input you can use the <mm:form /> tag which was mainly designed for that purpose, but here I'll focus on creating a node with a standard html form. In the action attribute of the following form I have put an empty ${mm:link('')} tag that generates a link to the page itself thereby ensuring that we submit to the same page as we are woking on.

<form action="${mm:link('')}" method="post">
<input type="text" name="your_email" />
<textarea name="your_message"></textarea />
<input type="submit" name="submit" value="Send" />
</form>

Put it somewhere in the middle of your jsp. Above it place code to import the submitted values:

<mm:import externid="your_email" />
<mm:import externid="your_message" />
<mm:import externid="submit" />

And some code to handle the variables that were created when someone clicks 'Send'. Here we use the <mm:present /> that is often used in mmbase but standard JSTL with <c:if test="${!empty submit}">...</c:if> works as well. Between a lot of mmbase taglibrary tags I find it often even easier tot read.
Right after the check with <mm:present /> I include a cloud tag to open a cloud in which we can create e-mail nodes. In the body of the cloud tag comes the code from the previous tip.

<mm:present referid="submit">
<mm:cloud username="foo" password="bar" method="pagelogon">
<!-- here the code that handles e-mail creation and sending -->
</mm:cloud>
</mm:present>

Below you will find the complete example. Copy and paste it, but be sure to change the e-mail address 'you@somewhere.org' in that of your own. And change the username 'foo' and password 'bar' in the second <mm:cloud /> tag if you have already changed the default security settings of your application.

<%@ page language="java" contentType="text/html" 
%><%@ taglib uri="http://www.mmbase.org/mmbase-taglib-2.0" prefix="mm"
%><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<mm:cloud>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="nl">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>E-mail example</title>
</head>
<body>

<mm:import externid="your_email" />
<mm:import externid="your_message" />
<mm:import externid="submit" />

<mm:present referid="submit">
<%-- pagelogin to enable visitors to create email nodes --%>
<mm:cloud username="foo" password="bar" method="pagelogon">

<%-- create an email node --%>
<mm:createnode id="new_email_node" type="email">
<mm:setfield name="from">${your_email}</mm:setfield>
<mm:setfield name="to"> you@somewhere.org </mm:setfield>
<mm:setfield name="subject">A test mail</mm:setfield>
<mm:setfield name="body">${your_message}</mm:setfield>
</mm:createnode>

<%-- the function 'mail' shoots it off into the world --%>
<mm:node referid="new_email_node">
<mm:functioncontainer>
<mm:param name="type">oneshotkeep</mm:param>
<mm:function name="mail" />
</mm:functioncontainer>
</mm:node>

<%-- let the visitor know what happened with it --%>
<mm:node referid="new_email_node">
<mm:field name="mailstatus">
<mm:compare value="1">
<p>Thanks. Your mail has been send.</p>
</mm:compare>
<mm:compare value="2">
<p>Some error occured!</p>
</mm:compare>
</mm:field>
</mm:node>

</mm:cloud>
</mm:present>

<form action="${mm:link('')}" method="post">
<fieldset>
<legend>Send an email</legend>
<input type="text" name="your_email" size="42" />
<textarea name="your_message" cols="42"></textarea />
<input type="submit" name="submit" value="Send" />
</fieldset>
</form>

</body>
</html>
</mm:cloud>

0 comments

RSS feed

Tweet this article

Most mentioned links:

MMBase documentation
MMBase API
MMBase SVN
Bugtracker
MMBase Taglib reference
The reference for all tags mm