001: /*
002: * Copyright (c) 2002-2003 by OpenSymphony
003: * All rights reserved.
004: */
005: package com.opensymphony.workflow.util;
006:
007: import com.opensymphony.module.propertyset.PropertySet;
008:
009: import com.opensymphony.workflow.FunctionProvider;
010: import com.opensymphony.workflow.config.Configuration;
011:
012: import org.apache.commons.logging.Log;
013: import org.apache.commons.logging.LogFactory;
014:
015: import java.util.*;
016:
017: import javax.mail.*;
018: import javax.mail.internet.InternetAddress;
019: import javax.mail.internet.MimeMessage;
020:
021: /**
022: * Sends an email to a group of users. The following arguments are expected:
023: *
024: * <ul>
025: * <li>to - comma seperated list of email addresses</li>
026: * <li>from - single email address</li>
027: * <li>subject - the message subject</li>
028: * <li>cc - comma seperated list of email addresses (optional)</li>
029: * <li>message - the message body</li>
030: * <li>smtpHost - the SMTP host that will relay the message</li>
031: * <li>parseVariables - if 'true', then variables of the form ${} in subject,
032: * message, to, and cc fields will be parsed</li>
033: * </ul>
034: *
035: * @author <a href="mailto:plightbo@hotmail.com">Pat Lightbody</a>
036: */
037: public class SendEmail implements FunctionProvider {
038: //~ Static fields/initializers /////////////////////////////////////////////
039:
040: private static final Log log = LogFactory.getLog(SendEmail.class);
041:
042: //~ Methods ////////////////////////////////////////////////////////////////
043:
044: public void execute(Map transientVars, Map args, PropertySet ps) {
045: String to = (String) args.get("to");
046: String from = (String) args.get("from");
047: String subject = (String) args.get("subject");
048: String cc = (String) args.get("cc");
049: String m = (String) args.get("message");
050: String smtpHost = (String) args.get("smtpHost");
051: boolean parseVariables = "true".equals(args
052: .get("parseVariables"));
053: Configuration config = (Configuration) transientVars
054: .get("configuration");
055:
056: try {
057: Properties props = new Properties();
058: props.put("mail.smtp.host", smtpHost);
059:
060: Session sendMailSession = Session.getInstance(props, null);
061: Transport transport = sendMailSession.getTransport("smtp");
062: Message message = new MimeMessage(sendMailSession);
063:
064: message.setFrom(new InternetAddress(from));
065:
066: Set toSet = new HashSet();
067: VariableResolver variableResolver = config
068: .getVariableResolver();
069: StringTokenizer st = new StringTokenizer(
070: parseVariables ? variableResolver
071: .translateVariables(to, transientVars, ps)
072: .toString() : to, ", ");
073:
074: while (st.hasMoreTokens()) {
075: String user = st.nextToken();
076: toSet.add(new InternetAddress(user));
077: }
078:
079: message
080: .setRecipients(Message.RecipientType.TO,
081: (InternetAddress[]) toSet
082: .toArray(new InternetAddress[toSet
083: .size()]));
084:
085: Set ccSet = null;
086:
087: if (cc != null) {
088: ccSet = new HashSet();
089:
090: if (parseVariables) {
091: cc = variableResolver.translateVariables(cc,
092: transientVars, ps).toString();
093: }
094:
095: st = new StringTokenizer(cc, ", ");
096:
097: while (st.hasMoreTokens()) {
098: String user = st.nextToken();
099: ccSet.add(new InternetAddress(user));
100: }
101: }
102:
103: if ((ccSet != null) && (ccSet.size() > 0)) {
104: message.setRecipients(Message.RecipientType.CC,
105: (InternetAddress[]) ccSet
106: .toArray(new InternetAddress[ccSet
107: .size()]));
108: }
109:
110: message.setSubject(parseVariables ? variableResolver
111: .translateVariables(subject, transientVars, ps)
112: .toString() : subject);
113: message.setSentDate(new Date());
114: message.setText(parseVariables ? variableResolver
115: .translateVariables(m, transientVars, ps)
116: .toString() : m);
117: message.saveChanges();
118:
119: transport.connect();
120: transport.sendMessage(message, message.getAllRecipients());
121: transport.close();
122: } catch (MessagingException e) {
123: log.error("Error sending email:", e);
124: }
125: }
126: }
|