package org.intermine.util;

import static org.apache.commons.lang.StringUtils.isBlank;

import java.util.Map;
import java.util.Properties;

import javax.mail.MessagingException;

public class Emailer {

	private static final String PREFIX = "mail.regarding.";
	
	private final Properties properties;
	
	public Emailer(Properties properties) {
		this.properties = properties;
	}
	
	public void email(String to, String messageKey) throws MessagingException {
		String subject = properties.getProperty(PREFIX + messageKey + ".subject");
		String body = properties.getProperty(PREFIX + messageKey + ".body");
		MailUtils.email(to, subject, body, properties);
	}
	
	public void email(String to, String messageKey, Object... vars) throws MessagingException {
		String subject = properties.getProperty(PREFIX + messageKey + ".subject");
		String bodyFmt = properties.getProperty(PREFIX + messageKey + ".body");
		String body = String.format(bodyFmt, vars);
		MailUtils.email(to, subject, body, properties);
	}
	
	public void welcome(String to) throws MessagingException {
		MailUtils.welcome(to, properties);
	}
	
	public String subscribeToList(String address) throws MessagingException {
		String mailingList = properties.getProperty("mail.mailing-list");
        if (!isBlank(mailingList)) {
            MailUtils.subscribe(address, properties);
            return mailingList;
        }
        return null;
	}
	
}

@@ -173,16 +173,61 @@
 ption;%0A%0A
+import org.apache.commons.lang.StringUtils;%0A%0A
 public c
@@ -242,17 +242,20 @@
 iler %7B%0A%0A
-%09
+    
 private 
@@ -302,19 +302,25 @@
 ding.%22;%0A
-%09%0A%09
+    %0A    
 private 
@@ -348,19 +348,25 @@
 erties;%0A
-%09%0A%09
+    %0A    
 public E
@@ -397,18 +397,24 @@
 ties) %7B%0A
-%09%09
+        
 this.pro
@@ -435,22 +435,31 @@
 erties;%0A
-%09%7D%0A%09%0A%09
+    %7D%0A    %0A    
 public v
@@ -518,34 +518,40 @@
 gingException %7B%0A
-%09%09
+        
 String subject =
@@ -601,34 +601,40 @@
  + %22.subject%22);%0A
-%09%09
+        
 String body = pr
@@ -686,18 +686,24 @@
 body%22);%0A
-%09%09
+        
 MailUtil
@@ -734,38 +734,47 @@
 y, properties);%0A
-%09%7D%0A%09%0A%09
+    %7D%0A    %0A    
 public void emai
@@ -845,26 +845,32 @@
 Exception %7B%0A
-%09%09
+        
 String subje
@@ -928,26 +928,32 @@
 .subject%22);%0A
-%09%09
+        
 String bodyF
@@ -1012,18 +1012,170 @@
 body%22);%0A
-%09%09
+        if (StringUtils.isBlank(bodyFmt)) %7B%0A            throw new MessagingException(%22No email template configured for %22 + messageKey);%0A        %7D%0A        
 String b
@@ -1210,18 +1210,24 @@
  vars);%0A
-%09%09
+        
 MailUtil
@@ -1262,30 +1262,39 @@
 roperties);%0A
-%09%7D%0A%09%0A%09
+    %7D%0A    %0A    
 public void 
@@ -1340,18 +1340,24 @@
 ption %7B%0A
-%09%09
+        
 MailUtil
@@ -1387,14 +1387,23 @@
 s);%0A
-%09%7D%0A%09%0A%09
+    %7D%0A    %0A    
 publ
@@ -1476,10 +1476,16 @@
 n %7B%0A
-%09%09
+        
 Stri
@@ -1704,11 +1704,17 @@
 ll;%0A
-%09%7D%0A%09
+    %7D%0A    
 %0A%7D%0A

Handle missing templates nicely. Whitespace