/*
* Created on 07-Dec-2005 by Dave S-B
*
* CVS Revision : $Revision$
* Last Updated : $Date$
* Updated By : $Author$
* Modified by Quru (http://www.quru.com) 12June2009
*/
/*package com.lledrsolutions.jspwiki.plugin;*/
package org.jspwiki.plugins; /*12Jun2009*/
import java.util.Date;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import com.ecyrd.jspwiki.WikiContext;
import com.ecyrd.jspwiki.plugin.PluginException;
import com.ecyrd.jspwiki.plugin.WikiPlugin;
/**
* The FormMailerPlugin is designed to be used with WikiForms. It just creates a
* simple email based on the form fields. You need to set up the SMTP server in
* the jspwiki.properties file by adding;
* jspwiki.formMailer.smtpHost = [host_name].
*
* @author Dave S-B
*/
public class FormMailerPlugin implements WikiPlugin
{
@SuppressWarnings("unchecked")
public String execute(WikiContext context, Map params) throws PluginException
{
// Check for submit
if (!params.containsKey("submit"))
return "";
// Construct message
StringBuffer message = new StringBuffer();
Iterator iter = params.keySet().iterator();
while (iter.hasNext())
{
String key = (String) iter.next();
if (!key.startsWith("mailer") && !key.equals("_body") && !key.equals("submit"))
{
message.append(key).append(";").append(System.getProperty("line.separator")).append(params.get(key).toString())
.append(System.getProperty("line.separator")).append(System.getProperty("line.separator"));
}
}
// Send it
String from = safeParam(params, "mailerFrom");
String to = safeParam(params, "mailerTo");
String cc = safeParam(params, "mailerCC");
String bcc = safeParam(params, "mailerBCC");
String subject = safeParam(params, "mailerSubject");
String host = context.getEngine().getVariable(context, "jspwiki.formMailer.smtpHost").toString();
String redirect = safeParam(params, "mailerRedirect");
try
{
this.send(from, to, cc, bcc, subject, message.toString(), host);
}
catch (AddressException e)
{
throw new PluginException("Mail Address Error", e);
}
catch (MessagingException e)
{
throw new PluginException("Messaging Error", e);
}
// Return redirect or thanks message
String html = "";
if (redirect.length() == 0)
html = " Thanks for your message! ";
else
html = "\n" +
"";
return html;
}
/**
* Get a param from the Map - or an empty string if the parameter doesn't
* exist
*
* @param params
* @param param
* @return
*/
private String safeParam(Map params, String param)
{
return params.containsKey(param) ? params.get(param).toString() : "";
}
/**
* Attempts to send an email
*
* @param from
* @param to
* @param cc
* @param bcc
* @param subject
* @param message
* @param smtpServer
* @throws AddressException
* @throws MessagingException
*/
private void send(String from, String to, String cc, String bcc, String subject, String message, String smtpServer)
throws AddressException, MessagingException
{
// Gets the System properties
Properties props = System.getProperties();
// Puts the SMTP server name to properties object
props.put("mail.smtp.host", smtpServer);
// Get the default Session using Properties Object
Session session = Session.getDefaultInstance(props, null);
session.setDebug(true);
// Construct the message
MimeMessage mailMessage = new MimeMessage(session);
mailMessage.setFrom(new InternetAddress(from));
mailMessage.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to, false));
mailMessage.setRecipients(Message.RecipientType.CC, InternetAddress.parse(cc == null ? "" : cc, false));
mailMessage.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(bcc == null ? "" : cc, false));
mailMessage.setSubject(subject);
// Create and fill the first message part
MimeBodyPart mimeBodyPart = new MimeBodyPart();
mimeBodyPart.setText(message);
// Create the Multipart and its parts to it
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(mimeBodyPart);
// Add the Multipart to the message
mailMessage.setContent(multipart);
// Set the Date: header
mailMessage.setSentDate(new Date());
// Send the message
Transport.send(mailMessage);
}
}