View Javadoc

1   /**
2    * This file Copyright (c) 2003-2010 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.module.mail;
35  
36  import info.magnolia.module.mail.templates.MailAttachment;
37  import info.magnolia.module.mail.util.MailUtil;
38  
39  import java.util.ArrayList;
40  import java.util.HashMap;
41  import java.util.List;
42  import java.util.Map;
43  import java.util.Properties;
44  
45  import javax.mail.Authenticator;
46  import javax.mail.PasswordAuthentication;
47  import javax.mail.Session;
48  
49  import org.apache.commons.collections.CollectionUtils;
50  import org.apache.commons.lang.StringUtils;
51  
52  /**
53   * Mail template used to send emails with Magnolia.
54   *
55   */
56  public class MailTemplate {
57  
58      public static String MAIL_CONTENT_TYPE = "contentType";
59  
60      public static String MAIL_FROM = "from";
61  
62      public static String MAIL_SUBJECT = "subject";
63  
64      public static String MAIL_TO = "to";
65  
66      public static String MAIL_TO_WORKFLOW = "mailTo";
67  
68      public static String MAIL_CC = "cc";
69  
70      public static String MAIL_TYPE = "type";
71  
72      public static String MAIL_PARAMETERS = "parameters";
73  
74      public static String MAIL_ATTACHMENTS = "attachments";
75  
76      public static String MAIL_BODY = "body";
77  
78      public static final String MAIL_HTML = "html";
79  
80      public static final String MAIL_TEMPLATE_FILE = "templateFile";
81  
82      private static final String MAIL_REPLY_TO = "replyTo";
83  
84      private static final String MAIL_BCC = "bcc";
85  
86      private Map<String, Object> parameters = new HashMap<String, Object>();
87  
88      private List<MailAttachment> attachments = new ArrayList<MailAttachment>();
89  
90      private String from;
91  
92      private String to;
93  
94      private String cc;
95  
96      private String subject;
97  
98      private String type;
99  
100     private String contentType;
101 
102     private String name;
103 
104     private String text;
105 
106     private String templateFile;
107 
108     private String bcc;
109 
110     private String replyTo;
111 
112     public MailTemplate() {
113 
114     }
115 
116     public Map<String, Object> getParameters() {
117         // instance of this class will be re-used. Do not let references to internal variables escape the instance
118         return new HashMap<String, Object>(parameters);
119     }
120 
121     public void setParameters(Map<String, Object> parameters) {
122         // instance of this class will be re-used. Do not let references to internal variables escape the instance
123         this.parameters.clear();
124         this.parameters.putAll(parameters);
125     }
126 
127     public List<MailAttachment> getAttachments() {
128         // instance of this class will be re-used. Do not let references to internal variables escape the instance
129         return new ArrayList<MailAttachment>(attachments);
130     }
131 
132     public void setAttachments(List<MailAttachment> attachments) {
133         // instance of this class will be re-used. Do not let references to internal variables escape the instance
134         this.attachments.clear();
135         this.attachments.addAll(attachments);
136     }
137 
138     public void addAttachment(MailAttachment attachment) {
139         // instance of this class will be re-used. Do not let references to internal variables escape the instance
140         this.attachments.add(attachment);
141     }
142 
143     public String getFrom() {
144         return from;
145     }
146 
147     public void setFrom(String from) {
148         this.from = from;
149     }
150 
151     public String getTo() {
152         return to;
153     }
154 
155     public void setTo(String to) {
156         this.to = to;
157     }
158 
159     public String getSubject() {
160         return subject;
161     }
162 
163     public void setSubject(String subject) {
164         this.subject = subject;
165     }
166 
167     public String getType() {
168         return type;
169     }
170 
171     public void setType(String type) {
172         this.type = type;
173     }
174 
175     public String getContentType() {
176         return contentType;
177     }
178 
179     public void setContentType(String contentType) {
180         this.contentType = contentType;
181     }
182 
183     public String getName() {
184         return name;
185     }
186 
187     public void setName(String name) {
188         this.name = name;
189     }
190 
191     public String getText() {
192         return text;
193     }
194 
195     public void setText(String text) {
196         this.text = text;
197     }
198 
199 
200     public String getCc() {
201         return cc;
202     }
203 
204 
205     public void setCc(String cc) {
206         this.cc = cc;
207     }
208 
209     public void setValues(Map<String, Object> params, List<MailAttachment> attachments) {
210 
211         if(params.containsKey(MAIL_TEMPLATE_FILE)) {
212             this.templateFile = (String) params.get(MAIL_TEMPLATE_FILE);
213         }
214 
215         if(params.containsKey(MAIL_CONTENT_TYPE)) {
216             this.contentType = (String) params.get(MAIL_CONTENT_TYPE);
217         }
218 
219         if(params.containsKey(MAIL_FROM)) {
220             this.from = (String) params.get(MAIL_FROM);
221         }
222 
223         if(params.containsKey(MAIL_SUBJECT)) {
224             this.subject = (String) params.get(MAIL_SUBJECT);
225         }
226 
227         if(params.containsKey(MAIL_TO)) {
228             this.to = (String) params.get(MAIL_TO);
229         }
230         if(params.containsKey(MailTemplate.MAIL_TO_WORKFLOW)) {
231             this.to = (String) params.get(MailTemplate.MAIL_TO_WORKFLOW);
232         }
233 
234         if(params.containsKey(MAIL_CC)) {
235             this.cc = (String) params.get(MAIL_CC);
236         }
237 
238         if(params.containsKey(MAIL_TYPE)) {
239             this.type = (String) params.get(MAIL_TYPE);
240         }
241 
242         if(params.containsKey(MAIL_BODY)) {
243             this.text = (String) params.get(MAIL_BODY);
244         }
245 
246         if(params.containsKey(MAIL_REPLY_TO)) {
247             this.replyTo = (String) params.get(MAIL_REPLY_TO);
248         }
249 
250         if(params.containsKey(MAIL_BCC)) {
251             this.bcc = (String) params.get(MAIL_BCC);
252         }
253 
254         // instance of this class will be re-used. Do not let references to internal variables escape the instance
255         this.parameters.clear();
256         this.parameters.putAll(params);
257 
258         if(!CollectionUtils.isEmpty(attachments)) {
259             // instance of this class will be re-used. Do not let references to internal variables escape the instance
260             this.attachments.clear();
261             this.attachments.addAll(attachments);
262         }
263     }
264 
265     public String getTemplateFile() {
266         return templateFile;
267     }
268 
269     public void setTemplateFile(String templateFile) {
270         this.templateFile = templateFile;
271     }
272 
273     public String getBcc() {
274         return bcc;
275     }
276 
277     public void setBcc(String bcc) {
278         this.bcc = bcc;
279     }
280 
281     public String getReplyTo() {
282         return replyTo;
283     }
284 
285     public void setReplyTo(String replyTo) {
286         this.replyTo = replyTo;
287     }
288 
289     public Session initSession() {
290 
291         Map<String, String> smtp = MailModule.getInstance().getSmtp();
292         Properties props = new Properties(); // System.getProperties(); should I try to use the system properties ?
293 
294         props.put("mail.smtp.host", MailUtil.getParameter(getParameters(), MailConstants.SMTP_SERVER, smtp.get(MailConstants.SMTP_SERVER)));
295         props.put("mail.smtp.port", MailUtil.getParameter(getParameters(), MailConstants.SMTP_PORT, smtp.get(MailConstants.SMTP_PORT)));
296 
297         final String starttls = (String) MailUtil.getParameter(getParameters(), MailConstants.SMTP_TLS, smtp.get(MailConstants.SMTP_TLS));
298         if ("true".equals(starttls)) {
299             //MAGNOLIA-2420
300             props.put("mail.smtp.starttls.enable", starttls);
301         }
302         final String ssl = (String) MailUtil.getParameter(getParameters(), MailConstants.SMTP_SSL, smtp.get(MailConstants.SMTP_SSL));
303         if ("true".equals(ssl)) {
304             //MAGNOLIA-2420
305             props.put("mail.smtp.socketFactory.port", MailUtil.getParameter(getParameters(), MailConstants.SMTP_PORT, smtp.get(MailConstants.SMTP_PORT)));
306             props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
307             props.put("mail.smtp.socketFactory.fallback", "false");
308         }
309 
310         Authenticator auth = null;
311         final String smtpUser = (String) MailUtil.getParameter(getParameters(), MailConstants.SMTP_USER, smtp.get(MailConstants.SMTP_USER));
312         final String smtpPassword = (String) MailUtil.getParameter(getParameters(), MailConstants.SMTP_PASSWORD, smtp.get(MailConstants.SMTP_PASSWORD));
313         if (StringUtils.isNotBlank(smtpUser)) {
314             props.put("mail.smtp.auth", "true");
315             props.put("mail.smtp.user", smtpUser);
316             auth = new Authenticator() {
317                 protected PasswordAuthentication getPasswordAuthentication() {
318                     return new PasswordAuthentication(smtpUser, smtpPassword);
319                 }
320             };
321         }
322         props.put("mail.smtp.sendpartial", StringUtils.defaultString(smtp.get(MailConstants.SMTP_SEND_PARTIAL)));
323         return Session.getInstance(props, auth);
324     }
325 
326 }