18 package org.turro.mail.impl;
20 import java.io.BufferedReader;
22 import java.io.FileReader;
23 import java.io.IOException;
24 import java.util.Date;
25 import java.util.HashSet;
26 import java.util.Properties;
28 import java.util.logging.Level;
29 import java.util.logging.Logger;
30 import javax.mail.Message;
31 import javax.mail.MessagingException;
32 import javax.mail.Session;
33 import javax.mail.internet.InternetAddress;
34 import javax.mail.internet.MimeMessage;
35 import javax.naming.InitialContext;
36 import javax.naming.NamingException;
37 import org.turro.string.Strings;
38 import org.turro.assistant.Assistant;
39 import org.turro.assistant.AssistantSet;
40 import org.turro.assistant.Assistants;
41 import org.turro.elephant.context.ElephantContext;
42 import org.turro.elephant.context.HeadlessApplication;
43 import org.turro.elephant.impl.abstracts.AbstractImplementation;
44 import org.turro.elephant.impl.context.ContextFactory;
45 import org.turro.elephant.impl.util.FileUtil;
46 import org.turro.elephant.impl.util.Parser;
47 import org.turro.elephant.impl.util.StringParser;
57 private final Date date =
new Date();
58 private final Set<MimeMessage> pool =
new HashSet<>();
59 private final Set<String> cssFiles =
new HashSet<>();
60 private String encoding;
67 this.encoding = encoding;
74 public void addToPool(String from, String to, String cc, String subject, String message, String content) {
91 MimeMessage msg =
new MimeMessage(
session);
94 msg.setFrom(
new InternetAddress(from));
95 }
else if (poolFrom !=
null) {
96 msg.setFrom(
new InternetAddress(poolFrom));
100 msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to,
false));
103 msg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(cc,
false));
105 msg.setSubject(cleanSubject(subject), encoding);
107 message =
template ==
null ? message :
template.replaceAll(
"\\#body",
Parser.
escapeGrouping(message));
109 if (content ==
null) {
110 msg.setText(addStyles(message), encoding);
112 msg.setContent(addStyles(message), content +
";charset=\"" + encoding +
"\"");
114 msg.setHeader(
"X-Mailer",
"Elephant Mail System");
115 msg.setHeader(
"Content-Transfer-Encoding",
"quoted-printable");
116 msg.setSentDate(date);
120 Logger.getLogger(
MailPool.class.getName()).log(Level.INFO,
"Added mail from {0} to {1}. Subject: {2}",
121 new Object[]{InternetAddress.toString(msg.getFrom()),
122 InternetAddress.toString(msg.getAllRecipients()), msg.getSubject()});
124 }
catch (MessagingException mex) {
134 if(password ==
null) {
137 }
catch (Exception ex) {
149 Session tmpSession =
null;
151 if (mailhost.startsWith(
"java:")) {
153 InitialContext ic =
new InitialContext();
154 tmpSession = (Session) ic.lookup(mailhost);
155 }
catch (NamingException ex) {
157 throw new MessagingException(
"Naming problem with: " + mailhost +
"\n\n" + ex.getExplanation());
160 Properties props = System.getProperties();
161 tmpSession = Session.getDefaultInstance(props,
null);
162 if (mailhost !=
null) {
163 props.put(
"mail.smtp.host", mailhost);
166 props.put(
"mail.smtp.auth",
"true");
168 props.put(
"mail.smtp.auth",
"false");
176 if(cssFiles.isEmpty()) {
178 if(!Strings.isBlank(cssS)) {
179 String[] cssA = cssS.split(
",");
180 for(String css1 : cssA) {
185 cssFiles.add(cssFile);
188 private String cleanSubject(String subject) {
189 subject = subject.replaceAll(
"\\<\\/?[\\?a-zA-Z\\-\\:\\_0-9]+\\ ?.*?\\>",
"");
190 subject = subject.replaceAll(
"\\n|\\r",
" ");
194 private String addStyles(String message) {
198 for (String fileName : cssFiles) {
199 css =
new File(fileName);
202 BufferedReader br =
new BufferedReader(
new FileReader(css));
204 while ((l = br.readLine()) !=
null) {
207 }
catch (Exception ex) {
208 Logger.getLogger(
MailPool.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(
null), ex);
213 if(!Strings.isBlank(style)) {
214 style =
"<style type='text/css'>\n" + style +
"\n</style>\n";
217 return style +
"<div class='mail-body'>" + message +
"</div>";
220 private String loadFromFile(String file) {
221 if(!Strings.isBlank(file)) {
223 return FileUtil.getContent(
new File(ElephantContext.getRealPath(file)));
224 }
catch (IOException ex) {
256 public static void sendMailToRole(String role, String subject, String message, Object data) {
279 al.add(
new Assistant(
null, to,
null,
null));
298 al.add(
new Assistant(
null, to,
null,
null));
static void addAssistants(String role, AssistantSet list, Object data)
static String getRealPath(String path)
static String getSiteName()
static String logMsg(String msg)
static String getEncoding()
static byte[] decrypt(byte[] value)
static HeadlessApplication getInstance()
IConstructor getConstructor()
KeyValueMap getAttributes()
static Object getImplementation(IElement iel, String name)
static String escapeGrouping(String sequence)
static String toHTML(String value)
void addToPool(String from, String to, String cc, String subject, String message, String content)
void addCssFile(String cssFile)
String getTemplateString(String attribute)
static void sendMailToAssistants(String path, String subject, String message, Object data)
static void sendMailToRole(String role, String subject, String message, Object data)
static void sendMailToRoleAndAdmin(String role, String subject, String message, Object data)
static void sendMailToAdmin(String subject, String message)
static void sendMailToAssistantsAndAdmin(String path, String subject, String message, Object data)
void setEncoding(String encoding)