View Javadoc

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