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