View Javadoc
1 package org.apache.turbine.util.webmacro; 2 3 /* ==================================================================== 4 * The Apache Software License, Version 1.1 5 * 6 * Copyright (c) 2001 The Apache Software Foundation. All rights 7 * reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in 18 * the documentation and/or other materials provided with the 19 * distribution. 20 * 21 * 3. The end-user documentation included with the redistribution, 22 * if any, must include the following acknowledgment: 23 * "This product includes software developed by the 24 * Apache Software Foundation (http://www.apache.org/)." 25 * Alternately, this acknowledgment may appear in the software itself, 26 * if and wherever such third-party acknowledgments normally appear. 27 * 28 * 4. The names "Apache" and "Apache Software Foundation" and 29 * "Apache Turbine" must not be used to endorse or promote products 30 * derived from this software without prior written permission. For 31 * written permission, please contact apache@apache.org. 32 * 33 * 5. Products derived from this software may not be called "Apache", 34 * "Apache Turbine", nor may "Apache" appear in their name, without 35 * prior written permission of the Apache Software Foundation. 36 * 37 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 38 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 39 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 40 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 41 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 44 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 45 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 46 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 47 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 48 * SUCH DAMAGE. 49 * ==================================================================== 50 * 51 * This software consists of voluntary contributions made by many 52 * individuals on behalf of the Apache Software Foundation. For more 53 * information on the Apache Software Foundation, please see 54 * <http://www.apache.org/>;. 55 */ 56 57 import org.apache.turbine.services.webmacro.TurbineWebMacro; 58 import org.apache.turbine.util.Log; 59 import org.apache.turbine.util.mail.SimpleEmail; 60 import org.webmacro.servlet.WebContext; 61 62 /*** 63 * This is a simple class for sending email from within WebMacro. 64 * Essentially, the body of the email is a WebMacro Context object. 65 * The beauty of this is that you can send email from within your 66 * WebMacro template or from your business logic in your Java code. 67 * The body of the email is just a WebMacro template so you can use 68 * all the template functionality of WebMacro within your emails! 69 * 70 * <p>Example Usage (This all needs to be on one line in your 71 * template): 72 * 73 * <p>Setup your context: 74 * 75 * <p>context.put ("WebMacroEmail", new WebMacroEmail() ); 76 * 77 * <p>Then, in your template: 78 * 79 * <pre> 80 * $WebMacroEmail.setTo("Jon Stevens", "jon@clearink.com") 81 * .setFrom("Mom", "mom@mom.com").setSubject("Eat dinner") 82 * .setTemplate("email/momEmail.wm") 83 * .setContext($context) 84 * </pre> 85 * 86 * The email/momEmail.wm template will then be parsed with the current 87 * Context that is stored in the 88 * RunData.getUser().getTemp(WebMacroService.WEBMACRO_CONTEXT) 89 * location. If the context does not already exist there, it will be 90 * created and then that will be used. 91 * 92 * <p>If you want to use this class from within your Java code all you 93 * have to do is something like this: 94 * 95 * <pre> 96 * WebMacroEmail wme = new WebMacroEmail(); 97 * wme.setTo("Jon Stevens", "jon@clearink.com"); 98 * wme.setFrom("Mom", "mom@mom.com").setSubject("Eat dinner"); 99 * wme.setContext(context); 100 * wme.setTemplate("email/momEmail.wm") 101 * wme.send(); 102 * </pre> 103 * 104 * (Note that when used within a WebMacro template, the send method 105 * will be called for you when WebMacro tries to convert the 106 * WebMacroEmail to a string by calling toString). 107 * 108 * <p>This class is just a wrapper around the SimpleEmail class. 109 * Thus, it uses the JavaMail API and also depends on having the 110 * mail.server property set in the TurbineResources.properties file. 111 * If you want to use this class outside of Turbine for general 112 * processing that is also possible by making sure to set the path to 113 * the TurbineResources.properties. See the 114 * TurbineResourceService.setPropertiesFileName() method for more 115 * information. 116 * 117 * @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a> 118 * @version $Id: WebMacroEmail.java,v 1.2 2002/07/11 16:53:19 mpoeschl Exp $ 119 * @deprecated you should use velocity 120 */ 121 public class WebMacroEmail 122 { 123 /*** The to name field. */ 124 private String toName = null; 125 126 /*** The to email field. */ 127 private String toEmail = null; 128 129 /*** The from name field. */ 130 private String fromName = null; 131 132 /*** The from email field. */ 133 private String fromEmail = null; 134 135 /*** The subject of the message. */ 136 private String subject = null; 137 138 /*** 139 * The template to process, relative to WM's template 140 * directory. 141 */ 142 private String template = null; 143 144 /*** 145 * A WebContext 146 */ 147 private WebContext context = null; 148 149 /*** 150 * Constructor 151 */ 152 public WebMacroEmail () 153 { 154 } 155 156 /*** 157 * Constructor 158 */ 159 public WebMacroEmail(WebContext context) 160 { 161 this.context = context; 162 } 163 164 /*** 165 * To: name, email 166 * 167 * @param to A String with the TO name. 168 * @param email A String with the TO email. 169 * @return A WebMacroEmail (self). 170 */ 171 public WebMacroEmail setTo(String to, String email) 172 { 173 this.toName = to; 174 this.toEmail = email; 175 return (this); 176 } 177 178 /*** 179 * From: name, email. 180 * 181 * @param from A String with the FROM name. 182 * @param email A String with the FROM email. 183 * @return A WebMacroEmail (self). 184 */ 185 public WebMacroEmail setFrom(String from, String email) 186 { 187 this.fromName = from; 188 this.fromEmail = email; 189 return (this); 190 } 191 192 /*** 193 * Subject. 194 * 195 * @param subject A String with the subject. 196 * @return A WebMacroEmail (self). 197 */ 198 public WebMacroEmail setSubject(String subject) 199 { 200 this.subject = subject; 201 return (this); 202 } 203 204 /*** 205 * Webmacro template to execute. Path is relative to the WM 206 * templates directory. 207 * 208 * @param template A String with the template. 209 * @return A WebMacroEmail (self). 210 */ 211 public WebMacroEmail setTemplate(String template) 212 { 213 this.template = template; 214 return (this); 215 } 216 217 /*** 218 * Set the context object that will be merged with the 219 * template. 220 * 221 * @param context A WebMacro context object. 222 * @return A WebMacroEmail (self). 223 */ 224 public WebMacroEmail setContext(WebContext context) 225 { 226 this.context = context; 227 return (this); 228 } 229 230 /*** 231 * Get the context object that will be merged with the 232 * template. 233 * 234 * @return A WebContext (self). 235 */ 236 public WebContext getContext() 237 { 238 return this.context; 239 } 240 241 /*** 242 * This method sends the email. 243 */ 244 public void send() 245 { 246 context.put("mail",this); 247 try 248 { 249 // Process the template. 250 String body = TurbineWebMacro.handleRequest(context,template); 251 252 SimpleEmail se = new SimpleEmail(); 253 se.setFrom(fromEmail, fromName); 254 se.addTo(toEmail, toName); 255 se.setSubject(subject); 256 se.setMsg(body); 257 se.send(); 258 } 259 catch (Exception e) 260 { 261 // Log the error. 262 Log.error("WebMacroEmail error: ", e); 263 } 264 } 265 266 /*** 267 * The method toString() calls send() for ease of use within a 268 * WebMacro template (see example usage above). 269 * 270 * @return An empty ("") String. 271 */ 272 public String toString() 273 { 274 send(); 275 return ""; 276 } 277 }

This page was automatically generated by Maven