1 package info.magnolia.cms.mail.templates; 2 3 import info.magnolia.cms.mail.MailException; 4 5 import java.io.BufferedReader ; 6 import java.io.File ; 7 import java.io.FileReader ; 8 import java.net.URL ; 9 import java.util.ArrayList ; 10 import java.util.List ; 11 import java.util.Map ; 12 import java.util.regex.Matcher ; 13 import java.util.regex.Pattern ; 14 15 import javax.activation.MimetypesFileTypeMap ; 16 import javax.mail.Address ; 17 import javax.mail.Message ; 18 import javax.mail.MessagingException ; 19 import javax.mail.Session ; 20 import javax.mail.internet.AddressException ; 21 import javax.mail.internet.InternetAddress ; 22 import javax.mail.internet.MimeBodyPart ; 23 import javax.mail.internet.MimeMessage ; 24 25 import org.apache.commons.lang.ArrayUtils; 26 import org.apache.commons.lang.StringUtils; 27 import org.slf4j.Logger; 28 import org.slf4j.LoggerFactory; 29 30 31 35 public abstract class MgnlEmail extends MimeMessage { 36 37 protected static final String CONTENT_TYPE = "Content-Type"; 38 39 protected static final String TEXT_PLAIN_UTF = "text/plain; charset=UTF-8"; 40 41 protected static final String TEXT_HTML_UTF = "text/html; charset=UTF-8"; 42 43 protected static final String CHARSET_HEADER_STRING = "charset="; 44 45 protected static final Pattern EMAIL_WITH_PERSONAL_PATTERN = Pattern.compile("\"+(.*)\"+<(.*)>"); 46 47 public static Logger log = LoggerFactory.getLogger(MgnlEmail.class); 48 49 public static final MimetypesFileTypeMap map = new MimetypesFileTypeMap (); 50 51 private String template; 52 53 private Map parameters; 54 55 private boolean bodyNotSetFlag; 57 public boolean isBodyNotSetFlag() { 58 return this.bodyNotSetFlag; 59 } 60 61 public void setBodyNotSetFlag(boolean _bodyNotSetFlag) { 62 this.bodyNotSetFlag = _bodyNotSetFlag; 63 } 64 65 public MgnlEmail(Session _session) { 66 super(_session); 67 } 68 69 public abstract void setBody(String body, Map _parameters) throws Exception ; 70 71 public void setSubject(String arg0) throws MessagingException { 72 this.setSubject(arg0, "UTF8"); 73 } 74 75 public void setTemplate(String _template) { 76 this.template = _template; 77 } 78 79 public Map getParameters() { 80 return this.parameters; 81 } 82 83 public String getTemplate() { 84 return this.template; 85 } 86 87 public void setParameters(Map _parameters) { 88 this.parameters = _parameters; 89 } 90 91 public void addParameters(Map params) { 92 this.parameters.putAll(params); 93 } 94 95 public void setBody() throws Exception { 96 if (this.template != null) { 97 this.setBody(this.template, this.parameters); 98 } 99 } 100 101 104 public void setFrom(String _from) { 105 try { 106 InternetAddress address; 107 Matcher matcher = MgnlEmail.EMAIL_WITH_PERSONAL_PATTERN.matcher(_from); 108 if(matcher.matches()){ 109 String email = matcher.group(2); 110 String personal = matcher.group(1); 111 address = new InternetAddress (email, personal, "UTF8"); 112 } 113 else{ 114 address = new InternetAddress (_from); 115 } 116 this.setFrom(address); 117 } 118 catch (Exception e) { 119 log.error("Could not set from field of email:" + e.getMessage()); 120 } 121 } 122 123 public void setCharsetHeader(String charset) throws MailException { 124 try { 125 StringBuffer contentType = new StringBuffer (this.getHeader(CONTENT_TYPE, TEXT_PLAIN_UTF)); 126 int index = contentType.lastIndexOf(";"); 127 if (index != -1) { 128 contentType.substring(0, index); 129 } 130 contentType.append(CHARSET_HEADER_STRING).append(charset); 131 } 132 catch (Exception e) { 133 throw new MailException("Content type is not set. Set the content type before setting the charset"); 134 } 135 } 136 137 public void setToList(String list) throws Exception { 138 setRecipients(Message.RecipientType.TO, createAdressList(list)); 139 } 140 141 public void setCcList(String list) throws Exception { 142 setRecipients(Message.RecipientType.CC, createAdressList(list)); 143 } 144 145 public void setBccList(String list) throws Exception { 146 setRecipients(Message.RecipientType.BCC, createAdressList(list)); 147 } 148 149 public void setReplyToList(String list) throws Exception { 150 setReplyTo(createAdressList(list)); 151 } 152 153 private Address [] createAdressList(String adresses) throws AddressException { 154 if (adresses == null || adresses.equals(StringUtils.EMPTY)) { 155 return new Address [0]; 156 } 157 String [] toObj = adresses.split("\n"); 158 List atos = new ArrayList (); 159 for (int i = 0; i < toObj.length; i++) { 160 try { 161 atos.add(new InternetAddress (toObj[i])); 162 } catch (AddressException e) { 163 log.warn("Error while parsing address.", e); 164 } 165 } 166 return (Address []) atos.toArray(new Address [atos.size()]); 167 } 168 169 public void setAttachments(ArrayList list) throws MailException { 170 if (list == null) { 171 return; 172 } 173 if (log.isDebugEnabled()) { 174 log.debug("Set attachments [" + list.size() + "] for mail: [" + this.getClass().getName() + "]"); 175 } 176 for (int i = 0; i < list.size(); i++) { 177 addAttachment((MailAttachment) list.get(i)); 178 } 179 } 180 181 public MimeBodyPart addAttachment(MailAttachment attachment) throws MailException { 182 throw new MailException("Cannot add attachment to this email. It is not a Multimime email"); 183 } 184 185 public void setBodyFromResourceFile(String resourceFile, Map map) throws Exception { 186 URL url = this.getClass().getResource("/" + resourceFile); 187 log.info("This is the url:" + url); 188 BufferedReader br = new BufferedReader (new FileReader (url.getFile())); 189 String line; 190 StringBuffer buffer = new StringBuffer (); 191 while ((line = br.readLine()) != null) { 192 buffer.append(line).append(File.separator); 193 } 194 this.setBody(buffer.toString(), map); 195 } 196 197 } 198 | Popular Tags |