based on sendmail logicsheet documentation http://cocoon.apache.org/2.1/userdocs/xsp/sendmail.html, sample created by StavrosKounis
Problem
Send mail web form
Solution
Requirements
- activation.jar http://java.sun.com/products/javamail/
download and put these jars in WEB-INF\lib
check your cocoon.xconf for sendmail logic sheet
... <builtin-logicsheet> <parameter name="prefix" value="sendmail"/> <parameter name="uri" value="http://apache.org/cocoon/sendmail/1.0"/> <parameter name="href" value="resource://org/apache/cocoon/components/language/markup/xsp/java/sendmail.xsl"/> </builtin-logicsheet> ...
example
create a folder under samples and name it "sendmail" (for example) put in this folser 3 files
- sitemap.xmap
2. sendmailform.xhtml 3. sendmail.xsp
sitemap.xmap
<?xml version="1.0"?> <map:sitemap xmlns:map="http://apache.org/cocoon/sitemap/1.0"> <!-- =========================== Pipelines ================================= --> <map:pipelines> <map:pipeline> <map:match pattern="sendmailform"> <map:read mime-type="text/html" src="sendmailform.xhtml"/> </map:match> <map:match pattern=""> <map:redirect-to uri="sendmailform"/> </map:match> <map:match pattern="send-a-mail"> <map:generate src="sendmail.xsp" type="serverpages"/> <map:serialize/> </map:match> </map:pipeline> </map:pipelines> </map:sitemap>
sendmailform.xhtml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>send mail</title> </head> <body> <form action="send-a-mail" method="post" enctype="multipart/form-data"> <table> <tbody> <tr> <td>subject:</td> <td><input type="text" name="subject" size="56" /></td> </tr> <tr> <td>body:</td> <td><textarea name="messagebody" rows="5" cols="72"></textarea></td> </tr> <tr> <td>file:</td> <td><input type="file" name="attachment" size="56" /></td> </tr> </tbody> </table> <br /> <button type="submit">send . . . </button> </form> </body> </html>
sendmail.xsp
<?xml version="1.0" encoding="ISO-8859-1"?> <xsp:page language="java" xmlns:xsp="http://apache.org/xsp" xmlns:xsp-request="http://apache.org/xsp/request/2.0" xmlns:sendmail="http://apache.org/cocoon/sendmail/1.0"> <email> <xsp:logic> StringBuffer body = new StringBuffer(); body.append("\n"); body.append("Web form message:\n\n"); body.append(<xsp-request:get-parameter name="messagebody"/>); body.append("\n\n"); </xsp:logic> <sendmail:send-mail> <!-- change sender and recipient appropriately --> <sendmail:from>cocoon@apache.org</sendmail:from> <sendmail:to>test@127.0.0.1</sendmail:to> <sendmail:subject><xsp-request:get-parameter name="subject"/></sendmail:subject> <!-- change the IP to point to your send mail server --> <sendmail:smtphost>127.0.0.1</sendmail:smtphost> <sendmail:body><xsp:expr>body.toString()</xsp:expr></sendmail:body> <sendmail:attachment><sendmail:param name="object"><xsp:expr>request.get("attachment")</xsp:expr></sendmail:param></sendmail:attachment> <sendmail:on-success> <p>Email successfully sent.</p> </sendmail:on-success> <sendmail:on-error> <p style="color:red;">An error occurred: <sendmail:error-message/></p> </sendmail:on-error> </sendmail:send-mail> </email> </xsp:page>
ask for http://...cocoon.../samples/sendmail/
ONE COMMON PROBLEM
maybe developers patch sendmail logicsheet in a future release
be carefull to NOT leave spaces or tabs in:
<sendmail:body><xsp:expr>body.toString()</xsp:expr></sendmail:body>
this one will generate errors:
<sendmail:body> <xsp:expr>body.toString()</xsp:expr> </sendmail:body>
the same error will occur in:
<sendmail:attachment><sendmail:param name="object"><xsp:expr>request.get("attachment")</xsp:expr></sendmail:param></sendmail:attachment>
this one will generate errors:
<sendmail:attachment> <sendmail:param name="object"> <xsp:expr>request.get("attachment")</xsp:expr> </sendmail:param> </sendmail:attachment>
This should be fixed in Cocoon 2.1.5, would be nice if someone can confirm it. (JoergHeinicke)
''I have test it in 2.1.6 and it's not fixed here is the error message (StavrosKounis):
Error compiling sendmail_xsp: ERROR 1 (org\apache\cocoon\www\samples\sendmail\sendmail_xsp.java): ... _sendmail_mms.setBody(String.valueOf( "" // start error (lines 174-174) "String literal is not properly closed by a double-quote" + " // end error " + ... Line 174, column 0: String literal is not properly closed by a double-quote
In cocoon 2.1.8 I had to remove geronimo-spec-javamail-1.3.1-rc5.jar and geronimo-spec-activation-1.0.2-rc4.jar from WEB-INF/lib I still had error with attachment.
Attachment: sendmail.zip