X-Git-Url: https://code.wpia.club/?a=blobdiff_plain;f=src%2Forg%2Fcacert%2Fgigi%2Femail%2FSendmail.java;h=a65b13262d659c9ba98218f6010d8f6284751d3d;hb=a398544a86bdffe288ded209d9cc6b19df199964;hp=008f3c69be986aec9dcc4f36d05cb0b80f04df40;hpb=06860e55306d268f5db3c49ac9090c4455752cc0;p=gigi.git diff --git a/src/org/cacert/gigi/email/Sendmail.java b/src/org/cacert/gigi/email/Sendmail.java index 008f3c69..a65b1326 100644 --- a/src/org/cacert/gigi/email/Sendmail.java +++ b/src/org/cacert/gigi/email/Sendmail.java @@ -3,6 +3,7 @@ package org.cacert.gigi.email; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; +import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.net.Socket; import java.security.GeneralSecurityException; @@ -13,11 +14,19 @@ import java.util.Locale; import java.util.Properties; import java.util.regex.Pattern; +import org.cacert.gigi.util.PEM; import org.cacert.gigi.util.ServerConstants; public class Sendmail extends EmailProvider { - protected Sendmail(Properties props) {} + private final String targetHost; + + private final int targetPort; + + protected Sendmail(Properties props) { + targetHost = props.getProperty("emailProvider.smtpHost", "localhost"); + targetPort = Integer.parseInt(props.getProperty("emailProvider.smtpPort", "25")); + } private static final Pattern NON_ASCII = Pattern.compile("[^a-zA-Z0-9 .-\\[\\]!_@]"); @@ -26,72 +35,69 @@ public class Sendmail extends EmailProvider { String[] bits = from.split(","); - Socket smtp = new Socket("localhost", 25); - PrintWriter out = new PrintWriter(smtp.getOutputStream()); - BufferedReader in = new BufferedReader(new InputStreamReader(smtp.getInputStream())); - readSMTPResponse(in, 220); - out.print("HELO www.cacert.org\r\n"); - out.flush(); - readSMTPResponse(in, 250); - out.print("MAIL FROM:\r\n"); - out.flush(); - readSMTPResponse(in, 250); - bits = to.split(","); - for (String user : bits) { - out.print("RCPT TO:<" + user.trim() + ">\r\n"); + 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"));) { + readSMTPResponse(in, 220); + out.print("HELO www.cacert.org\r\n"); out.flush(); readSMTPResponse(in, 250); - } - out.print("DATA\r\n"); - out.flush(); - readSMTPResponse(in, 250); - out.print("X-Mailer: CAcert.org Website\r\n"); - // if (array_key_exists("REMOTE_ADDR", $_SERVER)) { - // out.print("X-OriginatingIP: ".$_SERVER["REMOTE_ADDR"]."\r\n"); - // } - // TODO - SimpleDateFormat emailDate = new SimpleDateFormat("E, d MMM yyyy HH:mm:ss ZZZZ (z)", Locale.ENGLISH); - out.print("Date: " + emailDate.format(new Date(System.currentTimeMillis())) + "\r\n"); - if (errorsto != null) { - out.print("Sender: " + errorsto + "\r\n"); - out.print("Errors-To: " + errorsto + "\r\n"); - } - if (replyto != null) { - out.print("Reply-To: " + replyto + "\r\n"); - } else { - out.print("Reply-To: " + from + "\r\n"); - } - out.print("From: support@" + ServerConstants.getWwwHostName().replaceAll("^www.", "") + "\r\n"); - out.print("To: " + to + "\r\n"); - if (NON_ASCII.matcher(subject).matches()) { + out.print("MAIL FROM:\r\n"); + out.flush(); + readSMTPResponse(in, 250); + bits = to.split(","); + for (String user : bits) { + out.print("RCPT TO:<" + user.trim() + ">\r\n"); + out.flush(); + readSMTPResponse(in, 250); + } + out.print("DATA\r\n"); + out.flush(); + readSMTPResponse(in, 250); + out.print("X-Mailer: SomeCA.org Website\r\n"); + // if (array_key_exists("REMOTE_ADDR", $_SERVER)) { + // out.print("X-OriginatingIP: ".$_SERVER["REMOTE_ADDR"]."\r\n"); + // } + // TODO + SimpleDateFormat emailDate = new SimpleDateFormat("E, d MMM yyyy HH:mm:ss ZZZZ (z)", Locale.ENGLISH); + out.print("Date: " + emailDate.format(new Date(System.currentTimeMillis())) + "\r\n"); + if (errorsto != null) { + out.print("Sender: " + errorsto + "\r\n"); + out.print("Errors-To: " + errorsto + "\r\n"); + } + if (replyto != null) { + out.print("Reply-To: " + replyto + "\r\n"); + } else { + out.print("Reply-To: " + from + "\r\n"); + } + out.print("From: support@" + ServerConstants.getWwwHostName().replaceAll("^www.", "") + "\r\n"); + out.print("To: " + to + "\r\n"); + if (NON_ASCII.matcher(subject).matches()) { - out.print("Subject: =?utf-8?B?" + Base64.getEncoder().encodeToString(subject.getBytes()) + "?=\r\n"); - } else { - out.print("Subject: " + subject + "\r\n"); - } - StringBuffer headers = new StringBuffer(); - headers.append("Content-Type: text/plain; charset=\"utf-8\"\r\n"); - headers.append("Content-Transfer-Encoding: base64\r\n"); - // out.print(chunk_split(base64_encode(recode("html..utf-8", - // $message)))."\r\n.\r\n"); - headers.append("\r\n"); - headers.append(Base64.getEncoder().encodeToString(message.getBytes("UTF-8")).replaceAll("(.{64})(?=.)", "$1\r\n")); - headers.append("\r\n"); + out.print("Subject: =?utf-8?B?" + Base64.getEncoder().encodeToString(subject.getBytes("UTF-8")) + "?=\r\n"); + } else { + out.print("Subject: " + subject + "\r\n"); + } + StringBuffer headers = new StringBuffer(); + headers.append("Content-Type: text/plain; charset=\"utf-8\"\r\n"); + headers.append("Content-Transfer-Encoding: base64\r\n"); + // out.print(chunk_split(base64_encode(recode("html..utf-8", + // $message)))."\r\n.\r\n"); + headers.append("\r\n"); + headers.append(PEM.formatBase64(message.getBytes("UTF-8"))); + headers.append("\r\n"); - try { - sendSigned(headers.toString(), out); - out.print("\r\n.\r\n"); + try { + sendSigned(headers.toString(), out); + out.print("\r\n.\r\n"); + out.flush(); + } catch (GeneralSecurityException e) { + e.printStackTrace(); + return; + } + readSMTPResponse(in, 250); + out.print("QUIT\n"); out.flush(); - } catch (GeneralSecurityException e) { - e.printStackTrace(); - smtp.close(); - return; + readSMTPResponse(in, 221); } - readSMTPResponse(in, 250); - out.print("QUIT\n"); - out.flush(); - readSMTPResponse(in, 221); - smtp.close(); } public static boolean readSMTPResponse(BufferedReader in, int code) throws IOException {