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
55
56
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
130
131 @Deprecated
132 public MailTemplate() {
133 this(Components.getComponent(MailModule.class));
134 }
135
136 public Map<String, Object> getParameters() {
137
138 return new HashMap<>(parameters);
139 }
140
141 public void setParameters(Map<String, Object> parameters) {
142
143 this.parameters.clear();
144 this.parameters.putAll(parameters);
145 }
146
147 public List<MailAttachment> getAttachments() {
148
149 return new ArrayList<>(attachments);
150 }
151
152 public void setAttachments(List<MailAttachment> attachments) {
153
154 this.attachments.clear();
155 this.attachments.addAll(attachments);
156 }
157
158 public void addAttachment(MailAttachment attachment) {
159
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
297 this.parameters.clear();
298 this.parameters.putAll(params);
299
300 if (!CollectionUtils.isEmpty(attachments)) {
301
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();
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
342 props.put("mail.smtp.starttls.enable", true);
343 } else if (SmtpConfiguration.Security.SSL == security) {
344
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 }