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.ModuleRegistry;
37 import info.magnolia.module.mail.templates.MailAttachment;
38 import info.magnolia.module.mail.util.MailUtil;
39 import info.magnolia.objectfactory.Components;
40
41 import java.util.ArrayList;
42 import java.util.HashMap;
43 import java.util.List;
44 import java.util.Map;
45 import java.util.Properties;
46
47 import javax.mail.Authenticator;
48 import javax.mail.PasswordAuthentication;
49 import javax.mail.Session;
50
51 import org.apache.commons.collections.CollectionUtils;
52 import org.apache.commons.lang.StringUtils;
53
54
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 Map<String, Object> parameters = new HashMap<String, Object>();
92
93 private 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
127 return new HashMap<String, Object>(parameters);
128 }
129
130 public void setParameters(Map<String, Object> parameters) {
131
132 this.parameters.clear();
133 this.parameters.putAll(parameters);
134 }
135
136 public List<MailAttachment> getAttachments() {
137
138 return new ArrayList<MailAttachment>(attachments);
139 }
140
141 public void setAttachments(List<MailAttachment> attachments) {
142
143 this.attachments.clear();
144 this.attachments.addAll(attachments);
145 }
146
147 public void addAttachment(MailAttachment attachment) {
148
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 public String getCc() {
209 return cc;
210 }
211
212 public void setCc(String cc) {
213 this.cc = cc;
214 }
215
216 public String getUsername() {
217 return username;
218 }
219
220 public void setUsername(String username) {
221 this.username = username;
222 }
223
224 public String getPassword() {
225 return password;
226 }
227
228 public void setPassword(String password) {
229 this.password = password;
230 }
231
232 public void setValues(Map<String, Object> params, List<MailAttachment> attachments) {
233
234 if (params.containsKey(MAIL_TEMPLATE_FILE)) {
235 this.templateFile = (String) params.get(MAIL_TEMPLATE_FILE);
236 }
237
238 if (params.containsKey(MAIL_CONTENT_TYPE)) {
239 this.contentType = (String) params.get(MAIL_CONTENT_TYPE);
240 }
241
242 if (params.containsKey(MAIL_FROM)) {
243 this.from = (String) params.get(MAIL_FROM);
244 }
245
246 if (params.containsKey(MAIL_SUBJECT)) {
247 this.subject = (String) params.get(MAIL_SUBJECT);
248 }
249
250 if (params.containsKey(MAIL_TO)) {
251 this.to = (String) params.get(MAIL_TO);
252 }
253 if (params.containsKey(MailTemplate.MAIL_TO_WORKFLOW)) {
254 this.to = (String) params.get(MailTemplate.MAIL_TO_WORKFLOW);
255 }
256
257 if (params.containsKey(MAIL_CC)) {
258 this.cc = (String) params.get(MAIL_CC);
259 }
260
261 if (params.containsKey(MAIL_TYPE)) {
262 this.type = (String) params.get(MAIL_TYPE);
263 }
264
265 if (params.containsKey(MAIL_BODY)) {
266 this.text = (String) params.get(MAIL_BODY);
267 }
268
269 if (params.containsKey(MAIL_REPLY_TO)) {
270 this.replyTo = (String) params.get(MAIL_REPLY_TO);
271 }
272
273 if (params.containsKey(MAIL_BCC)) {
274 this.bcc = (String) params.get(MAIL_BCC);
275 }
276
277 if (params.containsKey(MAIL_USERNAME)) {
278 this.username = (String) params.get(MAIL_USERNAME);
279 }
280
281 if (params.containsKey(MAIL_PASSWORD)) {
282 this.password = (String) params.get(MAIL_PASSWORD);
283 }
284
285
286 this.parameters.clear();
287 this.parameters.putAll(params);
288
289 if (!CollectionUtils.isEmpty(attachments)) {
290
291 this.attachments.clear();
292 this.attachments.addAll(attachments);
293 }
294 }
295
296 public String getTemplateFile() {
297 return templateFile;
298 }
299
300 public void setTemplateFile(String templateFile) {
301 this.templateFile = templateFile;
302 }
303
304 public String getBcc() {
305 return bcc;
306 }
307
308 public void setBcc(String bcc) {
309 this.bcc = bcc;
310 }
311
312 public String getReplyTo() {
313 return replyTo;
314 }
315
316 public void setReplyTo(String replyTo) {
317 this.replyTo = replyTo;
318 }
319
320 public Session initSession() {
321
322 Map<String, String> smtp = Components.getComponent(ModuleRegistry.class).getModuleInstance(MailModule.class).getSmtp();
323 Properties props = new Properties();
324
325 props.put("mail.smtp.host", MailUtil.getParameter(getParameters(), MailConstants.SMTP_SERVER, smtp.get(MailConstants.SMTP_SERVER)));
326 props.put("mail.smtp.port", MailUtil.getParameter(getParameters(), MailConstants.SMTP_PORT, smtp.get(MailConstants.SMTP_PORT)));
327
328 final String smtpSecurity = (String) MailUtil.getParameter(getParameters(), MailConstants.SMTP_SECURITY, smtp.get(MailConstants.SMTP_SECURITY));
329 if ("tls".equals(smtpSecurity)) {
330
331 props.put("mail.smtp.starttls.enable", true);
332 } else if ("ssl".equals(smtpSecurity)) {
333
334 props.put("mail.smtp.socketFactory.port", MailUtil.getParameter(getParameters(), MailConstants.SMTP_PORT, smtp.get(MailConstants.SMTP_PORT)));
335 props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
336 props.put("mail.smtp.socketFactory.fallback", "false");
337 }
338
339 Authenticator auth = null;
340 final String smtpUser = (String) MailUtil.getParameter(getParameters(), MailConstants.SMTP_USER, smtp.get(MailConstants.SMTP_USER));
341 final String smtpPassword = (String) MailUtil.getParameter(getParameters(), MailConstants.SMTP_PASSWORD, smtp.get(MailConstants.SMTP_PASSWORD));
342 if (StringUtils.isNotBlank(smtpUser)) {
343 props.put("mail.smtp.auth", "true");
344 props.put("mail.smtp.user", smtpUser);
345 auth = new Authenticator() {
346 @Override
347 protected PasswordAuthentication getPasswordAuthentication() {
348 return new PasswordAuthentication(smtpUser, smtpPassword);
349 }
350 };
351 }
352 props.put("mail.smtp.sendpartial", StringUtils.defaultString(smtp.get(MailConstants.SMTP_SEND_PARTIAL)));
353 return Session.getInstance(props, auth);
354 }
355
356 }