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.commands;
35
36 import info.magnolia.cms.util.AlertUtil;
37 import info.magnolia.commands.MgnlCommand;
38 import info.magnolia.context.Context;
39 import info.magnolia.context.WebContext;
40 import info.magnolia.module.ModuleRegistry;
41 import info.magnolia.module.mail.MailModule;
42 import info.magnolia.module.mail.MailTemplate;
43 import info.magnolia.module.mail.MgnlMailFactory;
44 import info.magnolia.module.mail.templates.MailAttachment;
45 import info.magnolia.module.mail.templates.MgnlEmail;
46 import info.magnolia.module.mail.util.MailUtil;
47 import info.magnolia.objectfactory.Components;
48
49 import java.util.Arrays;
50 import java.util.List;
51 import java.util.Map;
52
53 import javax.inject.Inject;
54
55 import org.apache.commons.lang.StringUtils;
56 import org.slf4j.Logger;
57 import org.slf4j.LoggerFactory;
58
59
60
61
62
63
64
65 public class MailCommand extends MgnlCommand {
66
67 public static Logger log = LoggerFactory.getLogger(MailCommand.class);
68
69 private ModuleRegistry moduleRegistry;
70
71
72
73
74 public MailCommand() {
75 this.moduleRegistry = Components.getComponent(ModuleRegistry.class);
76 }
77
78 @Inject
79 public MailCommand(ModuleRegistry moduleRegistry) {
80 this.moduleRegistry = moduleRegistry;
81 }
82
83 @Override
84 public boolean execute(Context ctx) throws Exception {
85 if (log.isDebugEnabled()) {
86 log.debug("starting sending mail");
87 }
88
89 try {
90 MgnlMailFactory factory = moduleRegistry.getModuleInstance(MailModule.class).getFactory();
91 MgnlEmail email;
92 if (log.isDebugEnabled()) {
93 log.debug(Arrays.asList(ctx.entrySet().toArray()).toString());
94 }
95
96 String template = (String) ctx.get("mailTemplate");
97
98 if(ctx.containsKey(MailTemplate.MAIL_PARAMETERS)) {
99 Map<String, String> temp = MailUtil.convertToMap((String)ctx.get(MailTemplate.MAIL_PARAMETERS));
100 ctx.putAll(temp);
101 }
102
103
104 List<MailAttachment> attachments = null;
105
106 if(ctx instanceof WebContext) {
107 attachments = MailUtil.createAttachmentList(((WebContext)ctx).getParameters());
108 }
109
110 if (StringUtils.isNotEmpty(template)) {
111 log.debug("Command using mail template: " + template);
112
113 email = factory.getEmailFromTemplate(template, attachments, ctx);
114 email.setBodyFromResourceFile();
115 }
116 else {
117 log.debug("command using static parameters");
118
119 email = factory.getEmail(ctx, attachments);
120 if(StringUtils.isEmpty(email.getTemplate().getTemplateFile())) {
121 email.setBody();
122 } else {
123 email.setBodyFromResourceFile();
124 }
125
126 }
127 factory.getEmailHandler().sendMail(email);
128
129 log.info("send mail successfully to:" + email.getTemplate().getTo());
130 }
131 catch (Exception e) {
132 log.debug("Could not send email:" + e.getMessage(), e);
133 log.error("Could not send email:" + e.getMessage());
134 AlertUtil.setMessage("Error: " + e.getMessage());
135 return false;
136 }
137
138 return true;
139 }
140
141 }