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