Update of /var/cvs/applications/email/src/org/mmbase/applications/email
In directory james.mmbase.org:/tmp/cvs-serv26560
Modified Files:
EmailHandler.java
Log Message:
added support for MessageFormat replacement in the mail before sending (which
can typically be used in conjuction with mail 'templates'). Also dropped
support for '<NOMAIL>' and 'fakemail' which were a bit silly.
See also:
http://cvs.mmbase.org/viewcvs/applications/email/src/org/mmbase/applications/email
Index: EmailHandler.java
===================================================================
RCS file:
/var/cvs/applications/email/src/org/mmbase/applications/email/EmailHandler.java,v
retrieving revision 1.32
retrieving revision 1.33
diff -u -b -r1.32 -r1.33
--- EmailHandler.java 14 Oct 2008 11:04:32 -0000 1.32
+++ EmailHandler.java 28 Nov 2008 14:28:59 -0000 1.33
@@ -29,7 +29,7 @@
* @author Daniel Ockeloen
* @author Michiel Meeuwissen
* @author Simon Groenewolt
- * @version $Id: EmailHandler.java,v 1.32 2008/10/14 11:04:32 michiel Exp $
+ * @version $Id: EmailHandler.java,v 1.33 2008/11/28 14:28:59 michiel Exp $
* @since MMBase-1.7
*/
class EmailHandler {
@@ -42,7 +42,7 @@
* then parse the content for subject and body
* lastly mail it using the sendmail module
*/
- public static Node sendMailNode(final Node node) throws MessagingException
{
+ public static Node sendMailNode(final Node node, Object...
messageFormatArguments) throws MessagingException {
// get the sendmail module
SendMail sendmail = EmailBuilder.getSendMail();
if (sendmail == null) {
@@ -66,7 +66,7 @@
boolean success = true;
// loop all the users we need to mail
for (NodeRecipient to : toUsers) {
- if (! sendMail(node, from, to, body, headers)) {
+ if (! sendMail(node, from, to, body, headers,
messageFormatArguments)) {
success = false;
}
@@ -77,7 +77,7 @@
} else {
// one simple mail
NodeRecipient to = new NodeRecipient(-1,
node.getStringValue("to"));
- sendMail(node, from, to, body, headers);
+ sendMail(node, from, to, body, headers, messageFormatArguments);
}
// set the new mailedtime, that can be used by admins
// to see when it was mailed vs the requested mail
@@ -85,12 +85,20 @@
node.setValue("mailedtime", (int)(System.currentTimeMillis()/1000));
// commit the changes to the cloud
- if (node.getNumber() > 0) {
+ if (! node.isNew()) {
node.commit();
}
return node;
}
+ protected static String applyMessageFormat(String pattern, Object...
messageFormatArguments) {
+ if (messageFormatArguments == null) {
+ return null;
+ } else {
+ return java.text.MessageFormat.format(pattern,
messageFormatArguments);
+ }
+ }
+
/**
* Reads some fields from the given node and returns it as a Map with
mail-headers.
* The considered fields are replyto, cc, bcc and subject.
@@ -109,7 +117,7 @@
if (email.hasField("bcc")) {
headers.put("BCC", unemptyString(node.getStringValue("bcc")));
}
- // subject field is obligotary
+ // subject field is obligatory
headers.put("Subject", unemptyString(node.getStringValue("subject")));
headers.put("X-mmbase-node", node.getNodeManager().getName() + "/" +
node.getNumber());
@@ -130,10 +138,13 @@
* get the To header if its not set directly
* try to obtain it from related objects.
*/
- private static Set<NodeRecipient> getTo(Node node) {
+ private static Set<NodeRecipient> getTo(Node node, Object...
messageFormatArguments) {
Set<NodeRecipient> toUsers = new LinkedHashSet<NodeRecipient>();
String to = node.getStringValue("to");
if (to != null && !to.equals("")) {
+ if (messageFormatArguments != null) {
+ to = applyMessageFormat(to, messageFormatArguments);
+ }
toUsers.add(new NodeRecipient(-1, to));
}
return toUsers;
@@ -194,19 +205,9 @@
// If a Content-Type header is present: get the encoding from there
if (contentType != null) {
-
- StringTokenizer tokenizer = new StringTokenizer(contentType, ";
");
- while (tokenizer.hasMoreTokens()) {
- String value = tokenizer.nextToken();
- if (value.startsWith("charset=")) {
- // charset overrides the defaults for the mimetypes
- encoding = value.substring(8);
- } else if (value.equals("text/xml")) {
- // default encoding for text/xml
- encoding = "utf-8";
- }
- // default encoding for text/html en text/plain is
ISO-8859-1
-
+ String e =
org.mmbase.util.GenericResponseWrapper.getEncoding(contentType);
+ if (e != null) {
+ encoding = e;
}
}
BufferedReader in = new BufferedReader(new InputStreamReader
(connection.getInputStream(), encoding));
@@ -247,56 +248,43 @@
* @return whether successful
*/
- private static boolean sendMail(Node node, String from, NodeRecipient to,
String body, Map<String, String> headers) throws MessagingException {
- String obody = body;
-
- // WTF!
-
+ private static boolean sendMail(Node node, String from, NodeRecipient to,
String body, Map<String, String> headers, Object[] messageFormatArguments)
throws MessagingException {
// if the body starts with a url call that url
- if (obody.indexOf("http://") == 0) {
- body = getUrlExtern(obody, "", "" + to.nodeNumber);
- // convert html to plain text unless a the html tag is found
- if (body.indexOf("<html>") == -1 && body.indexOf("<HTML>") == -1) {
- //body=html2plain(body);
+ if (body.startsWith("http://")) {
+ body = getUrlExtern(body, "", "" + to.nodeNumber);
}
- }
-
- String osubject = headers.get("Subject");
+ {
+ String subject = headers.get("Subject");
// if the subject starts with a url call that url
- if (osubject != null && osubject.indexOf("http://") == 0) {
- String subject = getUrlExtern(osubject, "" , "" + to.nodeNumber);
+ if (subject != null && subject.startsWith("http://")) {
+ subject = getUrlExtern(subject, "" , "" + to.nodeNumber);
subject = stripToOneLine(subject);
headers.put("Subject", subject);
}
+ }
// little trick if it seems valid html page set
// Follow horrible hacks
- // the headers for html mail
+ // the headers for html mail This is stupid.
if (body.indexOf("<HTML>") != -1 && body.indexOf("</HTML>")!=-1) {
headers.put("Mime-Version", "1.0");
headers.put("Content-Type", "text/html; charset=\"ISO-8859-1\"");
// oh no!
}
- // is the don't mail tag set ? this allows
- // a generated body to signal it doesn't
- // want to be mailed since for some reason
- // invalid (for example there is no news for
- // you
- if (body.indexOf("<DONTMAIL>") == -1) {
- // if the subject contains 'fakemail'
- // perform all actions butt don't really
- // mail. This is done for testing
- String subject = headers.get("Subject");
- if (subject != null && subject.indexOf("fakemail")!=-1) {
- // add one to the sendmail counter
- // refix numberofmailsend++;
- log.info("Email -> fake send to " + to);
- return true;
- } else {
+
+
+ if (messageFormatArguments != null ) {
+ body = applyMessageFormat(body, messageFormatArguments);
+ from = applyMessageFormat(from, messageFormatArguments);
+ String subj = headers.get("Subject");
+ subj = applyMessageFormat(subj, messageFormatArguments);
+ headers.put("Subject", subj);
+ }
+
boolean mailResult = false;
MessagingException exception = null;
@@ -315,23 +303,23 @@
}
if (! mailResult) {
log.warn("Mail to " + to.email + " failed", exception);
+ if (node.getNodeManager().hasField("mailstatus")) {
node.setValue("mailstatus", EmailBuilder.STATE_FAILED);
+ }
// add one to the sendmail counter
// refix numberofmailsend++;
} else {
// add one to the sendmail counter
// refix numberofmailsend++;
log.debug("Email -> mail send");
+ if (node.getNodeManager().hasField("mailstatus")) {
node.setValue("mailstatus", EmailBuilder.STATE_DELIVERED);
}
+ }
if (exception != null) throw exception;
return true;
- }
- } else {
- log.debug("Don't mail tag found");
- return true;
- }
+
}
/**
* Simple structure representing an email-adres which is associated with a
node-number.
_______________________________________________
Cvs mailing list
[email protected]
http://lists.mmbase.org/mailman/listinfo/cvs