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.templates.impl;
35
36 import info.magnolia.freemarker.FreemarkerHelper;
37 import info.magnolia.module.mail.MailTemplate;
38 import info.magnolia.module.mail.templates.MgnlMultipartEmail;
39
40 import javax.mail.MessagingException;
41
42 import java.io.Reader;
43 import java.io.StringReader;
44 import java.io.StringWriter;
45
46
47
48
49
50 public class FreemarkerEmail extends MgnlMultipartEmail {
51
52 public FreemarkerEmail(MailTemplate template) {
53 super(template);
54 }
55
56 public void setFrom(String from) {
57 try {
58 from = proccesFreemarkerString(from);
59 } catch (Exception e) {
60 log.error("Couldn't set from: " + from);
61 }
62 super.setFrom(from);
63 }
64
65 public void setSubject(String subject) throws MessagingException {
66 try {
67 subject = proccesFreemarkerString(subject);
68 super.setSubject(subject);
69 } catch (Exception e) {
70 throw new MessagingException();
71 }
72
73 }
74
75 public void setToList(String list) throws Exception {
76 list = proccesFreemarkerString(list);
77 super.setToList(list);
78 }
79
80 public void setBody(String body) throws Exception {
81 body = proccesFreemarkerString(body);
82 super.setBody(body);
83 }
84
85
86 public void setBodyFromResourceFile() throws Exception {
87 final StringWriter writer = new StringWriter();
88 FreemarkerHelper.getInstance().render(super.getTemplate().getTemplateFile(), super.getTemplate().getParameters(), writer);
89 super.setBody(writer.toString());
90 }
91
92 protected String proccesFreemarkerString(String text) throws Exception {
93
94 Reader reader = new StringReader(text);
95 final StringWriter writer = new StringWriter();
96 FreemarkerHelper.getInstance().render(reader, super.getTemplate().getParameters(), writer);
97 reader.close();
98 return writer.toString();
99 }
100
101
102 }