View Javadoc

1   /**
2    * This file Copyright (c) 2003-2013 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.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   * the command for sending mail.
62   * @author jackie
63   * @author niko
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       * @deprecated since 5.2.1. use {@link #MailCommand(ModuleRegistry)} instead.
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              //get parameters from mail page parameter text area
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             //find attachments in parameters or form if we are using one
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 }