]> WPIA git - gigi.git/blob - src/club/wpia/gigi/email/SendMail.java
fix: ResultSet.getDate is often wrong as it fetches day-precision times
[gigi.git] / src / club / wpia / gigi / email / SendMail.java
1 package club.wpia.gigi.email;
2
3 import java.io.BufferedReader;
4 import java.io.IOException;
5 import java.io.InputStreamReader;
6 import java.io.OutputStreamWriter;
7 import java.io.PrintWriter;
8 import java.net.Socket;
9 import java.security.GeneralSecurityException;
10 import java.text.SimpleDateFormat;
11 import java.util.Base64;
12 import java.util.Date;
13 import java.util.Locale;
14 import java.util.Properties;
15 import java.util.regex.Pattern;
16
17 import club.wpia.gigi.util.PEM;
18 import club.wpia.gigi.util.ServerConstants;
19 import club.wpia.gigi.util.SystemKeywords;
20
21 public class SendMail extends EmailProvider {
22
23     private final String targetHost;
24
25     private final int targetPort;
26
27     protected SendMail(Properties props) {
28         targetHost = props.getProperty("emailProvider.smtpHost", "localhost");
29         targetPort = Integer.parseInt(props.getProperty("emailProvider.smtpPort", "25"));
30     }
31
32     private static final Pattern NON_ASCII = Pattern.compile("[^a-zA-Z0-9 .-\\[\\]!_@]");
33
34     @Override
35     public void sendMail(String to, String subject, String message, String replyto, String toname, String fromname, String errorsto, boolean extra) throws IOException {
36         String from = ServerConstants.getSupportMailAddress();
37         try (Socket smtp = new Socket(targetHost, targetPort); PrintWriter out = new PrintWriter(new OutputStreamWriter(smtp.getOutputStream(), "UTF-8")); BufferedReader in = new BufferedReader(new InputStreamReader(smtp.getInputStream(), "UTF-8"));) {
38             readSMTPResponse(in, 220);
39             out.print("HELO " + SystemKeywords.SMTP_NAME + "\r\n");
40             out.flush();
41             readSMTPResponse(in, 250);
42             out.print("MAIL FROM: <" + from + ">\r\n");
43             out.flush();
44             readSMTPResponse(in, 250);
45             String[] bits = to.split(",");
46             for (String user : bits) {
47                 out.print("RCPT TO:<" + user.trim() + ">\r\n");
48                 out.flush();
49                 readSMTPResponse(in, 250);
50             }
51             out.print("DATA\r\n");
52             out.flush();
53             readSMTPResponse(in, 250);
54             out.print("X-Mailer: SomeCA.org Website\r\n");
55             // if (array_key_exists("REMOTE_ADDR", $_SERVER)) {
56             // out.print("X-OriginatingIP: ".$_SERVER["REMOTE_ADDR"]."\r\n");
57             // }
58             // TODO
59             SimpleDateFormat emailDate = new SimpleDateFormat("E, d MMM yyyy HH:mm:ss ZZZZ (z)", Locale.ENGLISH);
60             out.print("Date: " + emailDate.format(new Date(System.currentTimeMillis())) + "\r\n");
61             if (errorsto != null) {
62                 out.print("Sender: " + errorsto + "\r\n");
63                 out.print("Errors-To: " + errorsto + "\r\n");
64             }
65             if (replyto != null) {
66                 out.print("Reply-To: " + replyto + "\r\n");
67             } else {
68                 out.print("Reply-To: " + from + "\r\n");
69             }
70             out.print("From: " + from + "\r\n");
71             out.print("To: " + to + "\r\n");
72             if (NON_ASCII.matcher(subject).matches()) {
73
74                 out.print("Subject: =?utf-8?B?" + Base64.getEncoder().encodeToString(subject.getBytes("UTF-8")) + "?=\r\n");
75             } else {
76                 out.print("Subject: " + subject + "\r\n");
77             }
78             StringBuffer headers = new StringBuffer();
79             headers.append("Content-Type: text/plain; charset=\"utf-8\"\r\n");
80             headers.append("Content-Transfer-Encoding: base64\r\n");
81             // out.print(chunk_split(base64_encode(recode("html..utf-8",
82             // $message)))."\r\n.\r\n");
83             headers.append("\r\n");
84             headers.append(PEM.formatBase64(message.getBytes("UTF-8")));
85             headers.append("\r\n");
86
87             try {
88                 sendSigned(headers.toString(), out);
89                 out.print("\r\n.\r\n");
90                 out.flush();
91             } catch (GeneralSecurityException e) {
92                 e.printStackTrace();
93                 return;
94             }
95             readSMTPResponse(in, 250);
96             out.print("QUIT\n");
97             out.flush();
98             readSMTPResponse(in, 221);
99         }
100     }
101
102     public static boolean readSMTPResponse(BufferedReader in, int code) throws IOException {
103         String line;
104         while ((line = in.readLine()) != null) {
105             if (line.startsWith(code + " ")) {
106                 return true;
107             } else if ( !line.startsWith(code + "-")) {
108                 return false;
109             }
110         }
111         return false;
112
113     }
114
115 }