1 package club.wpia.gigi.email;
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;
17 import club.wpia.gigi.util.PEM;
18 import club.wpia.gigi.util.ServerConstants;
19 import club.wpia.gigi.util.SystemKeywords;
21 public class SendMail extends EmailProvider {
23 private final String targetHost;
25 private final int targetPort;
27 protected SendMail(Properties props) {
28 targetHost = props.getProperty("emailProvider.smtpHost", "localhost");
29 targetPort = Integer.parseInt(props.getProperty("emailProvider.smtpPort", "25"));
32 private static final Pattern NON_ASCII = Pattern.compile("[^a-zA-Z0-9 .-\\[\\]!_@]");
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");
41 readSMTPResponse(in, 250);
42 out.print("MAIL FROM: <" + from + ">\r\n");
44 readSMTPResponse(in, 250);
45 String[] bits = to.split(",");
46 for (String user : bits) {
47 out.print("RCPT TO:<" + user.trim() + ">\r\n");
49 readSMTPResponse(in, 250);
51 out.print("DATA\r\n");
53 readSMTPResponse(in, 250);
54 out.print("X-Mailer: " + ServerConstants.getAppName() + " Website\r\n");
55 // if (array_key_exists("REMOTE_ADDR", $_SERVER)) {
56 // out.print("X-OriginatingIP: ".$_SERVER["REMOTE_ADDR"]."\r\n");
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");
65 if (replyto != null) {
66 out.print("Reply-To: " + replyto + "\r\n");
68 out.print("Reply-To: " + from + "\r\n");
70 out.print("From: " + from + "\r\n");
71 out.print("To: " + to + "\r\n");
72 if (NON_ASCII.matcher(subject).matches()) {
74 out.print("Subject: =?utf-8?B?" + Base64.getEncoder().encodeToString(subject.getBytes("UTF-8")) + "?=\r\n");
76 out.print("Subject: " + subject + "\r\n");
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");
88 sendSigned(headers.toString(), out);
89 out.print("\r\n.\r\n");
91 } catch (GeneralSecurityException e) {
95 readSMTPResponse(in, 250);
98 readSMTPResponse(in, 221);
102 public static boolean readSMTPResponse(BufferedReader in, int code) throws IOException {
104 while ((line = in.readLine()) != null) {
105 if (line.startsWith(code + " ")) {
107 } else if ( !line.startsWith(code + "-")) {