1 package org.cacert.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 org.cacert.gigi.util.PEM;
18 import org.cacert.gigi.util.ServerConstants;
20 public class Sendmail extends EmailProvider {
22 private final String targetHost;
24 private final int targetPort;
26 protected Sendmail(Properties props) {
27 targetHost = props.getProperty("emailProvider.smtpHost", "localhost");
28 targetPort = Integer.parseInt(props.getProperty("emailProvider.smtpPort", "25"));
31 private static final Pattern NON_ASCII = Pattern.compile("[^a-zA-Z0-9 .-\\[\\]!_@]");
34 public void sendmail(String to, String subject, String message, String from, String replyto, String toname, String fromname, String errorsto, boolean extra) throws IOException {
36 String[] bits = from.split(",");
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");
42 readSMTPResponse(in, 250);
43 out.print("MAIL FROM:<returns@cacert.org>\r\n");
45 readSMTPResponse(in, 250);
47 for (String user : bits) {
48 out.print("RCPT TO:<" + user.trim() + ">\r\n");
50 readSMTPResponse(in, 250);
52 out.print("DATA\r\n");
54 readSMTPResponse(in, 250);
55 out.print("X-Mailer: CAcert.org Website\r\n");
56 // if (array_key_exists("REMOTE_ADDR", $_SERVER)) {
57 // out.print("X-OriginatingIP: ".$_SERVER["REMOTE_ADDR"]."\r\n");
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");
66 if (replyto != null) {
67 out.print("Reply-To: " + replyto + "\r\n");
69 out.print("Reply-To: " + from + "\r\n");
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()) {
75 out.print("Subject: =?utf-8?B?" + Base64.getEncoder().encodeToString(subject.getBytes("UTF-8")) + "?=\r\n");
77 out.print("Subject: " + subject + "\r\n");
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");
89 sendSigned(headers.toString(), out);
90 out.print("\r\n.\r\n");
92 } catch (GeneralSecurityException e) {
96 readSMTPResponse(in, 250);
99 readSMTPResponse(in, 221);
103 public static boolean readSMTPResponse(BufferedReader in, int code) throws IOException {
105 while ((line = in.readLine()) != null) {
106 if (line.startsWith(code + " ")) {
108 } else if ( !line.startsWith(code + "-")) {