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