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