View Javadoc

1   /**
2    * This file Copyright (c) 2003-2011 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 static final String MAIL_USERNAME = "username";
87  
88      private static final String MAIL_PASSWORD = "password";
89  
90      private Map<String, Object> parameters = new HashMap<String, Object>();
91  
92      private List<MailAttachment> attachments = new ArrayList<MailAttachment>();
93  
94      private String from;
95  
96      private String to;
97  
98      private String cc;
99  
100     private String subject;
101 
102     private String type;
103 
104     private String contentType;
105 
106     private String name;
107 
108     private String text;
109 
110     private String templateFile;
111 
112     private String bcc;
113 
114     private String replyTo;
115     
116     private String username;
117     
118     private String password;
119 
120     public MailTemplate() {
121 
122     }
123 
124     public Map<String, Object> getParameters() {
125         // instance of this class will be re-used. Do not let references to internal variables escape the instance
126         return new HashMap<String, Object>(parameters);
127     }
128 
129     public void setParameters(Map<String, Object> parameters) {
130         // instance of this class will be re-used. Do not let references to internal variables escape the instance
131         this.parameters.clear();
132         this.parameters.putAll(parameters);
133     }
134 
135     public List<MailAttachment> getAttachments() {
136         // instance of this class will be re-used. Do not let references to internal variables escape the instance
137         return new ArrayList<MailAttachment>(attachments);
138     }
139 
140     public void setAttachments(List<MailAttachment> attachments) {
141         // instance of this class will be re-used. Do not let references to internal variables escape the instance
142         this.attachments.clear();
143         this.attachments.addAll(attachments);
144     }
145 
146     public void addAttachment(MailAttachment attachment) {
147         // instance of this class will be re-used. Do not let references to internal variables escape the instance
148         this.attachments.add(attachment);
149     }
150 
151     public String getFrom() {
152         return from;
153     }
154 
155     public void setFrom(String from) {
156         this.from = from;
157     }
158 
159     public String getTo() {
160         return to;
161     }
162 
163     public void setTo(String to) {
164         this.to = to;
165     }
166 
167     public String getSubject() {
168         return subject;
169     }
170 
171     public void setSubject(String subject) {
172         this.subject = subject;
173     }
174 
175     public String getType() {
176         return type;
177     }
178 
179     public void setType(String type) {
180         this.type = type;
181     }
182 
183     public String getContentType() {
184         return contentType;
185     }
186 
187     public void setContentType(String contentType) {
188         this.contentType = contentType;
189     }
190 
191     public String getName() {
192         return name;
193     }
194 
195     public void setName(String name) {
196         this.name = name;
197     }
198 
199     public String getText() {
200         return text;
201     }
202 
203     public void setText(String text) {
204         this.text = text;
205     }
206 
207 
208     public String getCc() {
209         return cc;
210     }
211 
212 
213     public void setCc(String cc) {
214         this.cc = cc;
215     }
216 
217     public String getUsername() {
218         return username;
219     }
220 
221     public void setUsername(String username) {
222         this.username = username;
223     }
224 
225     public String getPassword() {
226         return password;
227     }
228 
229     public void setPassword(String password) {
230         this.password = password;
231     }
232 
233     public void setValues(Map<String, Object> params, List<MailAttachment> attachments) {
234 
235         if(params.containsKey(MAIL_TEMPLATE_FILE)) {
236             this.templateFile = (String) params.get(MAIL_TEMPLATE_FILE);
237         }
238 
239         if(params.containsKey(MAIL_CONTENT_TYPE)) {
240             this.contentType = (String) params.get(MAIL_CONTENT_TYPE);
241         }
242 
243         if(params.containsKey(MAIL_FROM)) {
244             this.from = (String) params.get(MAIL_FROM);
245         }
246 
247         if(params.containsKey(MAIL_SUBJECT)) {
248             this.subject = (String) params.get(MAIL_SUBJECT);
249         }
250 
251         if(params.containsKey(MAIL_TO)) {
252             this.to = (String) params.get(MAIL_TO);
253         }
254         if(params.containsKey(MailTemplate.MAIL_TO_WORKFLOW)) {
255             this.to = (String) params.get(MailTemplate.MAIL_TO_WORKFLOW);
256         }
257 
258         if(params.containsKey(MAIL_CC)) {
259             this.cc = (String) params.get(MAIL_CC);
260         }
261 
262         if(params.containsKey(MAIL_TYPE)) {
263             this.type = (String) params.get(MAIL_TYPE);
264         }
265 
266         if(params.containsKey(MAIL_BODY)) {
267             this.text = (String) params.get(MAIL_BODY);
268         }
269 
270         if(params.containsKey(MAIL_REPLY_TO)) {
271             this.replyTo = (String) params.get(MAIL_REPLY_TO);
272         }
273 
274         if(params.containsKey(MAIL_BCC)) {
275             this.bcc = (String) params.get(MAIL_BCC);
276         }
277 
278         if(params.containsKey(MAIL_USERNAME)) {
279             this.username = (String) params.get(MAIL_USERNAME);
280         }
281 
282         if(params.containsKey(MAIL_PASSWORD)) {
283             this.password = (String) params.get(MAIL_PASSWORD);
284         }
285 
286         // instance of this class will be re-used. Do not let references to internal variables escape the instance
287         this.parameters.clear();
288         this.parameters.putAll(params);
289 
290         if(!CollectionUtils.isEmpty(attachments)) {
291             // instance of this class will be re-used. Do not let references to internal variables escape the instance
292             this.attachments.clear();
293             this.attachments.addAll(attachments);
294         }
295     }
296 
297     public String getTemplateFile() {
298         return templateFile;
299     }
300 
301     public void setTemplateFile(String templateFile) {
302         this.templateFile = templateFile;
303     }
304 
305     public String getBcc() {
306         return bcc;
307     }
308 
309     public void setBcc(String bcc) {
310         this.bcc = bcc;
311     }
312 
313     public String getReplyTo() {
314         return replyTo;
315     }
316 
317     public void setReplyTo(String replyTo) {
318         this.replyTo = replyTo;
319     }
320 
321     public Session initSession() {
322 
323         Map<String, String> smtp = MailModule.getInstance().getSmtp();
324         Properties props = new Properties(); // System.getProperties(); should I try to use the system properties ?
325 
326         props.put("mail.smtp.host", MailUtil.getParameter(getParameters(), MailConstants.SMTP_SERVER, smtp.get(MailConstants.SMTP_SERVER)));
327         props.put("mail.smtp.port", MailUtil.getParameter(getParameters(), MailConstants.SMTP_PORT, smtp.get(MailConstants.SMTP_PORT)));
328 
329         final String starttls = (String) MailUtil.getParameter(getParameters(), MailConstants.SMTP_TLS, smtp.get(MailConstants.SMTP_TLS));
330         if ("true".equals(starttls)) {
331             //MAGNOLIA-2420
332             props.put("mail.smtp.starttls.enable", starttls);
333         }
334         final String ssl = (String) MailUtil.getParameter(getParameters(), MailConstants.SMTP_SSL, smtp.get(MailConstants.SMTP_SSL));
335         if ("true".equals(ssl)) {
336             //MAGNOLIA-2420
337             props.put("mail.smtp.socketFactory.port", MailUtil.getParameter(getParameters(), MailConstants.SMTP_PORT, smtp.get(MailConstants.SMTP_PORT)));
338             props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
339             props.put("mail.smtp.socketFactory.fallback", "false");
340         }
341 
342         Authenticator auth = null;
343         final String smtpUser = (String) MailUtil.getParameter(getParameters(), MailConstants.SMTP_USER, smtp.get(MailConstants.SMTP_USER));
344         final String smtpPassword = (String) MailUtil.getParameter(getParameters(), MailConstants.SMTP_PASSWORD, smtp.get(MailConstants.SMTP_PASSWORD));
345         if (StringUtils.isNotBlank(smtpUser)) {
346             props.put("mail.smtp.auth", "true");
347             props.put("mail.smtp.user", smtpUser);
348             auth = new Authenticator() {
349                 @Override
350                 protected PasswordAuthentication getPasswordAuthentication() {
351                     return new PasswordAuthentication(smtpUser, smtpPassword);
352                 }
353             };
354         }
355         props.put("mail.smtp.sendpartial", StringUtils.defaultString(smtp.get(MailConstants.SMTP_SEND_PARTIAL)));
356         return Session.getInstance(props, auth);
357     }
358 
359 }