1 16 17 package org.pentaho.plugin.email; 18 19 import java.io.BufferedInputStream ; 20 import java.io.OutputStream ; 21 import java.util.ArrayList ; 22 import java.util.Date ; 23 import java.util.HashMap ; 24 import java.util.Iterator ; 25 import java.util.List ; 26 import java.util.Properties ; 27 28 import javax.activation.DataHandler ; 29 import javax.activation.DataSource ; 30 import javax.activation.FileDataSource ; 31 import javax.mail.AuthenticationFailedException ; 32 import javax.mail.Authenticator ; 33 import javax.mail.Message ; 34 import javax.mail.Multipart ; 35 import javax.mail.PasswordAuthentication ; 36 import javax.mail.SendFailedException ; 37 import javax.mail.Session ; 38 import javax.mail.Transport ; 39 import javax.mail.internet.InternetAddress ; 40 import javax.mail.internet.MimeBodyPart ; 41 import javax.mail.internet.MimeMessage ; 42 import javax.mail.internet.MimeMultipart ; 43 44 import org.apache.commons.logging.Log; 45 import org.apache.commons.logging.LogFactory; 46 import org.dom4j.Document; 47 import org.dom4j.Node; 48 import org.pentaho.core.solution.IActionResource; 49 import org.pentaho.core.system.PentahoSystem; 50 import org.pentaho.core.util.XmlHelper; 51 import org.pentaho.messages.Messages; 52 import org.pentaho.messages.util.LocaleHelper; 53 import org.pentaho.plugin.ComponentBase; 54 55 61 public class EmailComponent extends ComponentBase { 62 63 66 private static final long serialVersionUID = 1584906077946023715L; 67 68 private String defaultFrom; 69 70 String mailer = "smtpsend"; 72 String protocol = null, host = null; 73 74 String recordDir = null; 75 76 public Log getLogger() { 77 return LogFactory.getLog(EmailComponent.class); 78 } 79 80 protected boolean validateSystemSettings() { 81 String mailhost = PentahoSystem.getSystemSetting("smtp-email/email_config.xml", "mail.smtp.host", null); boolean authenticate = "true".equalsIgnoreCase(PentahoSystem.getSystemSetting("smtp-email/email_config.xml", "mail.smtp.auth", "false")); String user = PentahoSystem.getSystemSetting("smtp-email/email_config.xml", "mail.userid", ""); String password = PentahoSystem.getSystemSetting("smtp-email/email_config.xml", "mail.password", ""); defaultFrom = PentahoSystem.getSystemSetting("smtp-email/email_config.xml", "mail.from.default", ""); 89 if (mailhost.equals("") || (user.equals("") && authenticate) || defaultFrom.equals("")) { 94 boolean allowParameterUI = feedbackAllowed(); 95 96 if (allowParameterUI) { 97 OutputStream feedbackStream = getFeedbackOutputStream(); 98 StringBuffer messageBuffer = new StringBuffer (); 99 org.pentaho.core.util.UIUtil.formatErrorMessage( 100 "text/html", Messages.getString("Email.USER_COULD_NOT_SEND_EMAIL"), Messages.getString("Email.USER_SETTINGS_HELP", "~/pentaho-demo/pentaho-solutions/system/smtp-email/email_config.xml"), messageBuffer); try { 102 feedbackStream.write(messageBuffer.toString().getBytes(LocaleHelper.getSystemEncoding())); 103 } catch (Exception e) { 104 error(Messages.getErrorString("Base.ERROR_0003_INVALID_FEEDBACK_STREAM"), e); return false; 106 } 107 return false; 108 } else { 109 error(Messages.getErrorString("Email.ERROR_0009_SERVER_SETTINGS_NOT_SET")); return false; 112 } 113 } 114 boolean ok = (mailhost != null); 115 if (authenticate) { 116 ok &= (user != null) && (password != null); 117 } 118 ok &= defaultFrom != null; 119 return ok; 120 } 121 122 public boolean init() { 123 return true; 124 } 125 126 public boolean validateAction() { 127 if (!isDefinedInput("to")) { error(Messages.getErrorString("Email.ERROR_0001_TO_NOT_DEFINED", getActionName())); return false; 131 } 132 if (!isDefinedInput("subject")) { error(Messages.getErrorString("Email.ERROR_0002_SUBJECT_NOT_DEFINED", getActionName())); return false; 136 } 137 if (!isDefinedInput("message-plain") && !isDefinedInput("message-html")) { error(Messages.getErrorString("Email.ERROR_0003_BODY_NOT_DEFINED", getActionName())); return false; 141 } 142 143 return true; 144 145 } 146 147 public boolean executeAction() { 148 149 ArrayList tos = new ArrayList (); 150 String from; 151 String subject; 152 ArrayList ccs = new ArrayList (); 153 ArrayList bccs = new ArrayList (); 154 155 ArrayList attachments = new ArrayList (); 156 157 String messagePlain = null; 158 Object toObj = getInputValue("to"); if (toObj instanceof HashMap ) { 160 HashMap data = (HashMap ) toObj; 161 162 tos.add(data.get("to")); subject = (String ) data.get("subject"); messagePlain = (String ) data.get("message-plain"); } else { 166 String toStr = getInputStringValue("to"); if (toStr.trim().length() > 0) { 168 tos.add(toStr); 169 } 170 171 subject = getInputStringValue("subject"); if (isDefinedInput("message-plain")) { messagePlain = getInputStringValue("message-plain"); } 175 } 176 177 from = null; 179 if (isDefinedInput("from")) { from = getInputStringValue("from"); } else { 182 from = defaultFrom; 183 } 184 185 if (isDefinedInput("cc")) { ccs.add(getInputStringValue("cc")); } 188 189 if (isDefinedInput("bcc")) { bccs.add(getInputStringValue("bcc")); } 192 193 String messageHtml = null; 194 if (isDefinedInput("message-html")) { messageHtml = getInputStringValue("message-html"); } 197 198 String attachParameter = null; 199 String attachName = null; 200 Node curNode = null; 201 boolean isResource; 202 203 List attachNodes = getComponentDefinition().selectNodes("attachment-ref"); if ( (attachNodes != null) && (attachNodes.size() > 0) ) { 205 for (int i = 0; i < attachNodes.size(); ++i) { 206 curNode = (Node) attachNodes.get(i); 207 attachName = getInputStringValue(XmlHelper.getNodeText("@name-param", curNode, null)); attachParameter = XmlHelper.getNodeText("@resource-param", curNode, null); isResource = true; 210 if (attachParameter == null) { 211 attachParameter = XmlHelper.getNodeText("@input-param", curNode, null); isResource = false; 213 } 214 215 AttachStruct attachData = getAttachData(attachParameter, attachName, isResource); 216 if (attachData != null) { 217 attachments.add(attachData); 218 } 219 } 220 } else { if (isDefinedInput("attach")) { attachParameter = getInputStringValue("attach"); attachName = getInputStringValue("attach-name"); if ((attachParameter != null) && (attachName != null)) { 225 AttachStruct attachData = getAttachData(attachParameter, attachName, false); 226 if (attachData != null) { 227 attachments.add(attachData); 228 } 229 } 230 } 231 } 232 262 263 if (debug) 264 debug(Messages.getString("Email.DEBUG_TO_FROM", tos.toString(), from)); if (debug) 266 debug(Messages.getString("Email.DEBUG_CC_BCC", ccs.toString(), bccs.toString())); if (debug) 268 debug(Messages.getString("Email.DEBUG_SUBJECT", subject)); if (debug) 270 debug(Messages.getString("Email.DEBUG_PLAIN_MESSAGE", messagePlain)); if (debug) 272 debug(Messages.getString("Email.DEBUG_HTML_MESSAGE", messageHtml)); 274 if (tos.size() == 0) { 275 276 OutputStream feedbackStream = getFeedbackOutputStream(); 278 if (feedbackStream != null) { 279 createFeedbackParameter("to", Messages.getString("Email.USER_ENTER_EMAIL_ADDRESS"), "", "", true); setFeedbackMimeType("text/html"); return true; 282 } else { 283 return false; 284 } 285 } 286 if (subject == null) { 287 error(Messages.getErrorString("Email.ERROR_0005_NULL_SUBJECT", getActionName())); return false; 289 } 290 if ((messagePlain == null) && (messageHtml == null)) { 291 error(Messages.getErrorString("Email.ERROR_0006_NULL_BODY", getActionName())); return false; 293 } 294 295 if (getRuntimeContext().isPromptPending() ) { 296 return true; 297 } 298 299 try { 300 301 Properties props = new Properties (); 302 303 try { 304 Document configDocument = PentahoSystem.getSystemSettings().getSystemSettingsDocument("smtp-email/email_config.xml"); List properties = configDocument.selectNodes("/email-smtp/properties/*"); Iterator propertyIterator = properties.iterator(); 307 while (propertyIterator.hasNext()) { 308 Node propertyNode = (Node) propertyIterator.next(); 309 String propertyName = propertyNode.getName(); 310 String propertyValue = propertyNode.getText(); 311 props.put(propertyName, propertyValue); 312 } 313 } catch (Exception e) { 314 error(Messages.getString("Email.ERROR_0013_CONFIG_FILE_INVALID"), e); return false; 316 } 317 318 boolean authenticate = "true".equalsIgnoreCase(props.getProperty("mail.smtp.auth")); 320 322 Session session; 323 if (authenticate) { 324 Authenticator authenticator = new EmailAuthenticator(); 325 session = Session.getInstance(props, authenticator); 326 } else { 327 session = Session.getInstance(props); 328 } 329 330 if (debug && !props.containsKey("mail.debug")) { session.setDebug(true); 334 } 335 336 MimeMessage msg = new MimeMessage (session); 338 if (from != null) { 339 msg.setFrom(new InternetAddress (from)); 340 } else { 341 error(Messages.getString("Email.ERROR_0012_FROM_NOT_DEFINED")); } 344 345 if (tos != null) { 346 for (int idx = 0; idx < tos.size(); idx++) { 347 msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse((String ) tos.get(idx), false)); 348 } 349 } 350 if (ccs != null) { 351 for (int idx = 0; idx < ccs.size(); idx++) { 352 msg.setRecipients(Message.RecipientType.CC, InternetAddress.parse((String ) ccs.get(idx), false)); 353 } 354 } 355 if (bccs != null) { 356 for (int idx = 0; idx < bccs.size(); idx++) { 357 msg.setRecipients(Message.RecipientType.BCC, InternetAddress.parse((String ) bccs.get(idx), false)); 358 } 359 } 360 361 if (subject != null) { 362 msg.setSubject(subject); 363 } 364 365 if ((messagePlain != null) && (messageHtml == null) && (attachments.size() == 0)) { 366 msg.setText(messagePlain); 367 } else if (attachments.size() == 0) { 368 if (messagePlain != null) { 369 msg.setContent(messagePlain, "text/plain"); } 371 if (messageHtml != null) { 372 msg.setContent(messageHtml, "text/html"); } 374 } else { 375 Multipart multipart = new MimeMultipart (); 378 if (messageHtml != null) { 380 MimeBodyPart htmlBodyPart = new MimeBodyPart (); 382 htmlBodyPart.setContent(messageHtml, "text/html"); multipart.addBodyPart(htmlBodyPart); 384 } 385 386 if (messagePlain != null) { 387 MimeBodyPart textBodyPart = new MimeBodyPart (); 388 textBodyPart.setContent(messagePlain, "text/plain"); multipart.addBodyPart(textBodyPart); 390 } 391 392 for (int idx = 0; idx < attachments.size(); idx++) { 394 AttachStruct attachment = (AttachStruct) attachments.get(idx); 395 396 DataSource dataSource = attachment.dataSrc; 397 if (debug) 398 debug(Messages.getString("Email.DEBUG_ADDING_ATTACHMENT", attachment.name)); 400 MimeBodyPart attachmentBodyPart = new MimeBodyPart (); 402 403 attachmentBodyPart.setDataHandler(new DataHandler (dataSource)); 405 attachmentBodyPart.setFileName(attachment.name); 406 if (debug) 407 debug(Messages.getString("Email.DEBUG_ATTACHMENT_SOURCE", dataSource.getName())); multipart.addBodyPart(attachmentBodyPart); 409 } 410 411 msg.setContent(multipart); 413 } 414 415 msg.setHeader("X-Mailer", mailer); msg.setSentDate(new Date ()); 417 418 Transport.send(msg); 419 420 if (debug) 421 debug(Messages.getString("Email.DEBUG_EMAIL_SUCCESS")); return true; 423 } catch (SendFailedException e) { 425 error(Messages.getErrorString("Email.ERROR_0011_SEND_FAILED", tos.toString() ), e); 434 } catch (AuthenticationFailedException e) { 435 error(Messages.getString("Email.ERROR_0014_AUTHENTICATION_FAILED", tos.toString()), e); } catch (Throwable e) { 437 error(Messages.getErrorString("Email.ERROR_0011_SEND_FAILED", tos.toString()), e); } 439 return false; 440 } 441 442 public void done() { 443 444 } 445 446 private AttachStruct getAttachData(String paramName, String attachName, boolean isResource) { 447 DataSource dataSrc = null; 448 if (isResource) { 449 IActionResource param = getResource(paramName); 450 if (param != null) { 451 dataSrc = getResourceDataSource(param); 452 } 453 } else { 454 dataSrc = getDataSource(paramName); 455 } 456 457 int fileLength = -1; 458 if (dataSrc instanceof FileDataSource ) { 459 fileLength = (int) ((FileDataSource ) dataSrc).getFile().length(); 460 } else { 461 try { 462 BufferedInputStream inStrm = new BufferedInputStream (dataSrc.getInputStream()); 463 byte bytes[] = new byte[1024]; 464 fileLength = 0; 465 int bytesRead = inStrm.read(bytes); 466 while (bytesRead > 0) { 467 bytesRead = inStrm.read(bytes); 468 fileLength += bytesRead; 469 } 470 inStrm.close(); 471 } catch (Throwable t) { 472 return (null); 473 } 474 } 475 return (new AttachStruct(fileLength, (attachName == null) ? dataSrc.getName() : attachName, dataSrc)); 476 } 477 478 private class EmailAuthenticator extends Authenticator { 479 480 protected PasswordAuthentication getPasswordAuthentication() { 481 String user = PentahoSystem.getSystemSetting("smtp-email/email_config.xml", "mail.userid", null); String password = PentahoSystem.getSystemSetting("smtp-email/email_config.xml", "mail.password", null); return new PasswordAuthentication (user, password); 484 } 485 } 486 487 class AttachStruct { 488 long size; 489 490 String name; 491 492 DataSource dataSrc; 493 494 AttachStruct(long size, String name, DataSource dataSrc) { 495 this.size = size; 496 this.name = name; 497 this.dataSrc = dataSrc; 498 } 499 } 500 501 } 502 | Popular Tags |