1 29 30 package com.caucho.quercus.lib; 31 32 import com.caucho.quercus.QuercusModuleException; 33 import com.caucho.quercus.annotation.Optional; 34 import com.caucho.quercus.env.Env; 35 import com.caucho.quercus.env.StringValue; 36 import com.caucho.quercus.module.AbstractQuercusModule; 37 import com.caucho.util.L10N; 38 39 import javax.mail.Address ; 40 import javax.mail.Message ; 41 import javax.mail.MessagingException ; 42 import javax.mail.Session ; 43 import javax.mail.Transport ; 44 import javax.mail.internet.InternetAddress ; 45 import javax.mail.internet.MimeMessage ; 46 import java.util.ArrayList ; 47 import java.util.Properties ; 48 import java.util.logging.Level ; 49 import java.util.logging.Logger ; 50 51 54 public class MailModule extends AbstractQuercusModule { 55 private static final Logger log = 56 Logger.getLogger(MailModule.class.getName()); 57 58 private static final L10N L = new L10N(MailModule.class); 59 60 63 public static boolean mail(Env env, 64 String to, 65 String subject, 66 StringValue message, 67 @Optional String additionalHeaders, 68 @Optional String additionalParameters) 69 { 70 Transport smtp = null; 71 72 try { 73 Properties props = new Properties (); 74 75 StringValue host = env.getIni("SMTP"); 76 if (host != null && ! host.toString().equals("")) 77 props.put("mail.smtp.host", host.toString()); 78 79 StringValue port = env.getIni("smtp_port"); 80 if (port != null && ! port.toString().equals("")) 81 props.put("mail.smtp.port", port.toString()); 82 83 StringValue user = env.getIni("sendmail_from"); 84 if (user != null && ! user.toString().equals("")) 85 props.put("mail.from", user.toString()); 86 87 Session mailSession = Session.getInstance(props, null); 88 smtp = mailSession.getTransport("smtp"); 89 90 MimeMessage msg = new MimeMessage (mailSession); 91 msg.setFrom(); 92 msg.setSubject(subject); 93 msg.setContent(message.toString(), "text/plain"); 94 95 ArrayList <Address > addrList; 96 addrList = addRecipients(msg, Message.RecipientType.TO, to); 97 98 if (additionalHeaders != null) 99 addHeaders(msg, additionalHeaders); 100 101 msg.saveChanges(); 102 103 if (addrList.size() == 0) 104 throw new QuercusModuleException(L.l("mail has no recipients")); 105 106 String username = env.getIniString("smtp_username"); 107 String password = env.getIniString("smtp_password"); 108 109 if (password != null && ! "".equals(password)) 110 smtp.connect(username, password); 111 else 112 smtp.connect(); 113 114 Address [] addr = new Address [addrList.size()]; 115 addrList.toArray(addr); 116 117 smtp.sendMessage(msg, addr); 118 119 log.fine("quercus-mail: sent mail to " + to); 120 121 return true; 122 } catch (RuntimeException e) { 123 log.log(Level.FINER, e.toString(), e); 124 125 throw e; 126 } catch (MessagingException e) { 127 log.log(Level.FINE, e.toString(), e); 128 129 env.warning(e.getMessage()); 130 131 return false; 132 } catch (Exception e) { 133 log.log(Level.FINE, e.toString(), e); 134 135 env.warning(e.toString()); 136 137 return false; 138 } finally { 139 try { 140 if (smtp != null) 141 smtp.close(); 142 } catch (Exception e) { 143 log.log(Level.FINER, e.toString(), e); 144 } 145 } 146 } 147 148 private static ArrayList <Address > addRecipients(MimeMessage msg, 149 Message.RecipientType type, 150 String to) 151 throws MessagingException 152 { 153 String []split = to.split("[ \t,<>]"); 154 155 ArrayList <Address > addresses = new ArrayList <Address >(); 156 157 for (int i = 0; i < split.length; i++) { 158 if (split[i].indexOf('@') > 0) { 159 Address addr = new InternetAddress (split[i]); 160 161 addresses.add(addr); 162 msg.addRecipient(type, addr); 163 } 164 } 165 166 return addresses; 167 } 168 169 private static void addHeaders(MimeMessage msg, String headers) 170 throws MessagingException 171 { 172 int i = 0; 173 int len = headers.length(); 174 175 while (true) { 176 char ch; 177 178 for (; 179 i < len && Character.isWhitespace(headers.charAt(i)); 180 i++) { 181 } 182 183 if (len <= i) 184 return; 185 186 StringBuilder name = new StringBuilder (); 187 188 for (; 189 i < len && (! Character.isWhitespace(ch = headers.charAt(i)) && 190 ch != ':'); 191 i++) { 192 name.append((char) ch); 193 } 194 195 for (; 196 i < len && (Character.isWhitespace(ch = headers.charAt(i)) || 197 ch == ':'); 198 i++) { 199 } 200 201 StringBuilder value = new StringBuilder (); 202 203 for (; 204 i < len && ((ch = headers.charAt(i)) != '\r' && 205 ch != '\n'); 206 i++) { 207 value.append((char) ch); 208 } 209 210 msg.addHeader(name.toString(), value.toString()); 211 } 212 } 213 } 214 215 | Popular Tags |