View Javadoc

1   /**
2    * This file Copyright (c) 2003-2011 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.module.mail.commands;
35  
36  import info.magnolia.context.WebContext;
37  import info.magnolia.cms.util.AlertUtil;
38  import info.magnolia.module.mail.MailModule;
39  import info.magnolia.module.mail.MailTemplate;
40  import info.magnolia.module.mail.MgnlMailFactory;
41  import info.magnolia.module.mail.templates.MailAttachment;
42  import info.magnolia.module.mail.templates.MgnlEmail;
43  import info.magnolia.module.mail.util.MailUtil;
44  
45  import java.util.Arrays;
46  import java.util.List;
47  import java.util.Map;
48  
49  import org.apache.commons.chain.Command;
50  import org.apache.commons.chain.Context;
51  import org.apache.commons.lang.StringUtils;
52  import org.slf4j.Logger;
53  import org.slf4j.LoggerFactory;
54  
55  
56  /**
57   * the command for sending mail.
58   * @author jackie
59   * @author niko
60   */
61  public class MailCommand implements Command {
62  
63      public static Logger log = LoggerFactory.getLogger(MailCommand.class);
64  
65      public boolean execute(Context ctx) {
66          if (log.isDebugEnabled()) {
67              log.debug("starting sending mail");
68          }
69  
70          try {
71              MgnlMailFactory factory = MailModule.getInstance().getFactory();
72              MgnlEmail email;
73              if (log.isDebugEnabled())
74                  log.debug(Arrays.asList(ctx.entrySet().toArray()).toString());
75  
76              String template = (String) ctx.get("mailTemplate");
77              //get parameters from mail page parameter text area
78              if(ctx.containsKey(MailTemplate.MAIL_PARAMETERS)) {
79                  Map<String, String> temp = MailUtil.convertToMap((String)ctx.get(MailTemplate.MAIL_PARAMETERS));
80                  ctx.putAll(temp);
81              }
82  
83              //find attachments in parameters or form if we are using one
84              List<MailAttachment> attachments = null;
85  
86              if(ctx instanceof WebContext) {
87                  attachments = MailUtil.createAttachmentList(((WebContext)ctx).getParameters());
88              }
89  
90              if (StringUtils.isNotEmpty(template)) {
91                  log.debug("Command using mail template: " + template);
92  
93                  email = factory.getEmailFromTemplate(template, attachments, ctx);
94                  email.setBodyFromResourceFile();
95              }
96              else {
97                  log.debug("command using static parameters");
98  
99                  email = factory.getEmail(ctx, attachments);
100                 if(StringUtils.isEmpty(email.getTemplate().getTemplateFile())) {
101                     email.setBody();
102                 } else {
103                     email.setBodyFromResourceFile();
104                 }
105 
106             }
107             factory.getEmailHandler().sendMail(email);
108 
109             log.info("send mail successfully to:" + email.getTemplate().getTo());
110         }
111         catch (Exception e) {
112             log.debug("Could not send email:" + e.getMessage(), e);
113             log.error("Could not send email:" + e.getMessage());
114             AlertUtil.setMessage("Error: " + e.getMessage());
115             return false;
116         }
117 
118         return true;
119     }
120 
121 }