1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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 import org.slf4j.Logger;
55 import org.slf4j.LoggerFactory;
56
57
58
59
60 public class MailTemplate {
61
62 public static Logger log = LoggerFactory.getLogger(MailTemplate.class);
63
64 public static String MAIL_CONTENT_TYPE = "contentType";
65
66 public static String MAIL_FROM = "from";
67
68 public static String MAIL_SUBJECT = "subject";
69
70 public static String MAIL_TO = "to";
71
72 public static String MAIL_TO_WORKFLOW = "mailTo";
73
74 public static String MAIL_CC = "cc";
75
76 public static String MAIL_TYPE = "type";
77
78 public static String MAIL_PARAMETERS = "parameters";
79
80 public static String MAIL_ATTACHMENTS = "attachments";
81
82 public static String MAIL_BODY = "body";
83
84 public static final String MAIL_HTML = "html";
85
86 public static final String MAIL_TEMPLATE_FILE = "templateFile";
87
88 private static final String MAIL_REPLY_TO = "replyTo";
89
90 private static final String MAIL_BCC = "bcc";
91
92 private static final String MAIL_USERNAME = "username";
93
94 private static final String MAIL_PASSWORD = "password";
95
96 private Map<String, Object> parameters = new HashMap<>();
97
98 private List<MailAttachment> attachments = new ArrayList<>();
99
100 private String from;
101
102 private String to;
103
104 private String cc;
105
106 private String subject;
107
108 private String type;
109
110 private String contentType;
111
112 private String name;
113
114 private String text;
115
116 private String templateFile;
117
118 private String bcc;
119
120 private String replyTo;
121
122 private String username;
123
124 private String password;
125
126 private final MailModule mailModule;
127
128 public MailTemplate(MailModule mailModule) {
129 this.mailModule = mailModule;
130 }
131
132
133
134
135 @Deprecated
136 public MailTemplate() {
137 this(Components.getComponent(MailModule.class));
138 }
139
140 public Map<String, Object> getParameters() {
141
142 return new HashMap<>(parameters);
143 }
144
145 public void setParameters(Map<String, Object> parameters) {
146
147 this.parameters.clear();
148 this.parameters.putAll(parameters);
149 }
150
151 public List<MailAttachment> getAttachments() {
152
153 return new ArrayList<>(attachments);
154 }
155
156 public void setAttachments(List<MailAttachment> attachments) {
157
158 this.attachments.clear();
159 this.attachments.addAll(attachments);
160 }
161
162 public void addAttachment(MailAttachment attachment) {
163
164 this.attachments.add(attachment);
165 }
166
167 public String getFrom() {
168 return from;
169 }
170
171 public void setFrom(String from) {
172 this.from = from;
173 }
174
175 public String getTo() {
176 return to;
177 }
178
179 public void setTo(String to) {
180 this.to = to;
181 }
182
183 public String getSubject() {
184 return subject;
185 }
186
187 public void setSubject(String subject) {
188 this.subject = subject;
189 }
190
191 public String getType() {
192 return type;
193 }
194
195 public void setType(String type) {
196 this.type = type;
197 }
198
199 public String getContentType() {
200 return contentType;
201 }
202
203 public void setContentType(String contentType) {
204 this.contentType = contentType;
205 }
206
207 public String getName() {
208 return name;
209 }
210
211 public void setName(String name) {
212 this.name = name;
213 }
214
215 public String getText() {
216 return text;
217 }
218
219 public void setText(String text) {
220 this.text = text;
221 }
222
223 public String getCc() {
224 return cc;
225 }
226
227 public void setCc(String cc) {
228 this.cc = cc;
229 }
230
231 public String getUsername() {
232 return username;
233 }
234
235 public void setUsername(String username) {
236 this.username = username;
237 }
238
239 public String getPassword() {
240 return password;
241 }
242
243 public void setPassword(String password) {
244 this.password = password;
245 }
246
247 public void setValues(Map<String, Object> params, List<MailAttachment> attachments) {
248
249 if (params.containsKey(MAIL_TEMPLATE_FILE)) {
250 this.templateFile = (String) params.get(MAIL_TEMPLATE_FILE);
251 }
252
253 if (params.containsKey(MAIL_CONTENT_TYPE)) {
254 this.contentType = (String) params.get(MAIL_CONTENT_TYPE);
255 }
256
257 if (params.containsKey(MAIL_FROM)) {
258 this.from = (String) params.get(MAIL_FROM);
259 }
260
261 if (params.containsKey(MAIL_SUBJECT)) {
262 this.subject = (String) params.get(MAIL_SUBJECT);
263 }
264
265 if (params.containsKey(MAIL_TO)) {
266 this.to = (String) params.get(MAIL_TO);
267 }
268 if (params.containsKey(MailTemplate.MAIL_TO_WORKFLOW)) {
269 this.to = (String) params.get(MailTemplate.MAIL_TO_WORKFLOW);
270 }
271
272 if (params.containsKey(MAIL_CC)) {
273 this.cc = (String) params.get(MAIL_CC);
274 }
275
276 if (params.containsKey(MAIL_TYPE)) {
277 this.type = (String) params.get(MAIL_TYPE);
278 }
279
280 if (params.containsKey(MAIL_BODY)) {
281 this.text = (String) params.get(MAIL_BODY);
282 }
283
284 if (params.containsKey(MAIL_REPLY_TO)) {
285 this.replyTo = (String) params.get(MAIL_REPLY_TO);
286 }
287
288 if (params.containsKey(MAIL_BCC)) {
289 this.bcc = (String) params.get(MAIL_BCC);
290 }
291
292 if (params.containsKey(MAIL_USERNAME)) {
293 this.username = (String) params.get(MAIL_USERNAME);
294 }
295
296 if (params.containsKey(MAIL_PASSWORD)) {
297 this.password = (String) params.get(MAIL_PASSWORD);
298 }
299
300
301 this.parameters.clear();
302 this.parameters.putAll(params);
303
304 if (!CollectionUtils.isEmpty(attachments)) {
305
306 this.attachments.clear();
307 this.attachments.addAll(attachments);
308 }
309 }
310
311 public String getTemplateFile() {
312 return templateFile;
313 }
314
315 public void setTemplateFile(String templateFile) {
316 this.templateFile = templateFile;
317 }
318
319 public String getBcc() {
320 return bcc;
321 }
322
323 public void setBcc(String bcc) {
324 this.bcc = bcc;
325 }
326
327 public String getReplyTo() {
328 return replyTo;
329 }
330
331 public void setReplyTo(String replyTo) {
332 this.replyTo = replyTo;
333 }
334
335 public Session initSession() {
336
337 Session session = null;
338 final SmtpConfiguration smtpConfiguration = mailModule.getSmtpConfiguration();
339 final Properties props = new Properties();
340
341 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)));
342 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))));
343
344 SmtpConfiguration.Security security = mailModule.getSmtp().get(MailConstants.SMTP_SECURITY) == null ? smtpConfiguration.getSecurity() : SmtpConfiguration.Security.valueOf(mailModule.getSmtp().get(MailConstants.SMTP_SECURITY));
345 if (SmtpConfiguration.Security.TLS == security) {
346 props.put("mail.smtp.starttls.enable", true);
347 } else if (SmtpConfiguration.Security.SSL == security) {
348 props.put("mail.smtp.socketFactory.port", MailUtil.getParameter(getParameters(), MailConstants.SMTP_PORT, String.valueOf(smtpConfiguration.getPort())));
349 props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
350 props.put("mail.smtp.socketFactory.fallback", "false");
351 props.put("mail.smtp.ssl.enable", "true");
352 }
353
354 final SmtpAuthentication authentication = smtpConfiguration.getAuthentication();
355 final String smtpUser = (String) MailUtil.getParameter(getParameters(), MailConstants.SMTP_USER, authentication.getUser());
356 if (authentication instanceof UsernamePasswordSmtpAuthentication) {
357 props.put("mail.smtp.user", smtpUser);
358 props.put("mail.smtp.auth", "true");
359 } else if (authentication instanceof GoogleOauthSmtpAuthentication) {
360 props.put("mail.smtp.user", smtpUser);
361 props.put("mail.smtp.auth", "true");
362 props.put("mail.smtp.auth.mechanisms", "XOAUTH2");
363 }
364 props.put("mail.smtp.sendpartial", MailUtil.getParameter(getParameters(), MailConstants.SMTP_SEND_PARTIAL, StringUtils.EMPTY));
365 session = Session.getInstance(props, authentication.getAuthenticator());
366 if (smtpConfiguration.isDebug() && session != null) {
367 log.debug("Setting javax.mail.Session to debug");
368 session.setDebug(true);
369 }
370 return session;
371 }
372
373 }