X-Git-Url: https://code.wpia.club/?a=blobdiff_plain;f=src%2Forg%2Fcacert%2Fgigi%2Femail%2FSendmail.java;h=c12523611ac09d9b93565bba01d5bd5568ca123d;hb=915fdaf71e3eced70b96efc5c2196351a754fd74;hp=5a85cd78baa6c98f2a471cffd6f19d58683b0be1;hpb=12a50b628704757377141d80da9dfc2785d225ed;p=gigi.git diff --git a/src/org/cacert/gigi/email/Sendmail.java b/src/org/cacert/gigi/email/Sendmail.java index 5a85cd78..c1252361 100644 --- a/src/org/cacert/gigi/email/Sendmail.java +++ b/src/org/cacert/gigi/email/Sendmail.java @@ -5,103 +5,106 @@ import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.Socket; +import java.security.GeneralSecurityException; import java.text.SimpleDateFormat; import java.util.Base64; import java.util.Date; import java.util.Locale; +import java.util.Properties; import java.util.regex.Pattern; +import org.cacert.gigi.util.ServerConstants; + public class Sendmail extends EmailProvider { - protected Sendmail() { - } - private static final Pattern NON_ASCII = Pattern - .compile("[^a-zA-Z0-9 .-\\[\\]!_@]"); - @Override - public void sendmail(String to, String subject, String message, - String from, String replyto, String toname, String fromname, - String errorsto, boolean extra) throws IOException { + protected Sendmail(Properties props) {} + + private static final Pattern NON_ASCII = Pattern.compile("[^a-zA-Z0-9 .-\\[\\]!_@]"); + + @Override + public void sendmail(String to, String subject, String message, String from, String replyto, String toname, String fromname, String errorsto, boolean extra) throws IOException { + + String[] bits = from.split(","); - 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"); + 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()) { - Socket smtp = new Socket("dogcraft.de", 25); - PrintWriter out = new PrintWriter(smtp.getOutputStream()); - BufferedReader in = new BufferedReader(new InputStreamReader( - smtp.getInputStream())); - readResponse(in); - out.print("HELO www.cacert.org\r\n"); - out.flush(); - readResponse(in); - out.print("MAIL FROM:\r\n"); - out.flush(); - readResponse(in); - bits = to.split(","); - for (String user : bits) { - out.print("RCPT TO:<" + user.trim() + ">\r\n"); - out.flush(); - readResponse(in); - } - out.print("DATA\r\n"); - out.flush(); - readResponse(in); - 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"); - 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: " + from + "\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("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(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()) - + "?=\r\n"); - } else { - out.print("Subject: " + subject + "\r\n"); - } - out.print("Mime-Version: 1.0\r\n"); - if (!extra) { - out.print("Content-Type: text/plain; charset=\"utf-8\"\r\n"); - out.print("Content-Transfer-Encoding: 8bit\r\n"); - } else { - out.print("Content-Type: text/plain; charset=\"iso-8859-1\"\r\n"); - out.print("Content-Transfer-Encoding: quoted-printable\r\n"); - out.print("Content-Disposition: inline\r\n"); - } - // out.print("Content-Transfer-Encoding: BASE64\r\n"); - out.print("\r\n"); - // out.print(chunk_split(base64_encode(recode("html..utf-8", - // $message)))."\r\n.\r\n"); - message = message + "\r\n"; + try { + sendSigned(headers.toString(), out); + out.print("\r\n.\r\n"); + out.flush(); + } catch (GeneralSecurityException e) { + e.printStackTrace(); + smtp.close(); + return; + } + readSMTPResponse(in, 250); + out.print("QUIT\n"); + out.flush(); + readSMTPResponse(in, 221); + smtp.close(); + } - String sendM = message.replace("\r", "").replace("\n.\n", "\n") - .replace("\n.\n", "\n").replace("\n", "\r\n") - + ".\r\n"; - out.print(sendM); - out.flush(); - readResponse(in); - out.print("QUIT\n"); - out.flush(); - readResponse(in); - smtp.close(); - } - private static void readResponse(BufferedReader in) throws IOException { - String line; - while ((line = in.readLine()) != null && line.matches("\\d+-")) { - } + public static boolean readSMTPResponse(BufferedReader in, int code) throws IOException { + String line; + while ((line = in.readLine()) != null) { + if (line.startsWith(code + " ")) { + return true; + } else if ( !line.startsWith(code + "-")) { + return false; + } + } + return false; - } + } }