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.PrintWriter;
7 import java.net.Socket;
8 import java.text.SimpleDateFormat;
9 import java.util.Base64;
10 import java.util.Date;
11 import java.util.Locale;
12 import java.util.Properties;
13 import java.util.regex.Pattern;
15 public class Sendmail extends EmailProvider {
16 protected Sendmail(Properties props) {
18 private static final Pattern NON_ASCII = Pattern
19 .compile("[^a-zA-Z0-9 .-\\[\\]!_@]");
22 public void sendmail(String to, String subject, String message,
23 String from, String replyto, String toname, String fromname,
24 String errorsto, boolean extra) throws IOException {
26 String[] bits = from.split(",");
28 Socket smtp = new Socket("dogcraft.de", 25);
29 PrintWriter out = new PrintWriter(smtp.getOutputStream());
30 BufferedReader in = new BufferedReader(new InputStreamReader(
31 smtp.getInputStream()));
33 out.print("HELO www.cacert.org\r\n");
36 out.print("MAIL FROM:<returns@cacert.org>\r\n");
40 for (String user : bits) {
41 out.print("RCPT TO:<" + user.trim() + ">\r\n");
45 out.print("DATA\r\n");
48 out.print("X-Mailer: CAcert.org Website\r\n");
49 // if (array_key_exists("REMOTE_ADDR", $_SERVER)) {
50 // out.print("X-OriginatingIP: ".$_SERVER["REMOTE_ADDR"]."\r\n");
53 SimpleDateFormat emailDate = new SimpleDateFormat(
54 "E, d MMM yyyy HH:mm:ss ZZZZ (z)", Locale.ENGLISH);
56 + emailDate.format(new Date(System.currentTimeMillis()))
58 out.print("Sender: " + errorsto + "\r\n");
59 out.print("Errors-To: " + errorsto + "\r\n");
60 if (replyto != null) {
61 out.print("Reply-To: " + replyto + "\r\n");
63 out.print("Reply-To: " + from + "\r\n");
65 out.print("From: " + from + "\r\n");
66 out.print("To: " + to + "\r\n");
67 if (NON_ASCII.matcher(subject).matches()) {
69 out.print("Subject: =?utf-8?B?"
70 + Base64.getEncoder().encodeToString(subject.getBytes())
73 out.print("Subject: " + subject + "\r\n");
75 out.print("Mime-Version: 1.0\r\n");
77 out.print("Content-Type: text/plain; charset=\"utf-8\"\r\n");
78 out.print("Content-Transfer-Encoding: 8bit\r\n");
80 out.print("Content-Type: text/plain; charset=\"iso-8859-1\"\r\n");
81 out.print("Content-Transfer-Encoding: quoted-printable\r\n");
82 out.print("Content-Disposition: inline\r\n");
84 // out.print("Content-Transfer-Encoding: BASE64\r\n");
86 // out.print(chunk_split(base64_encode(recode("html..utf-8",
87 // $message)))."\r\n.\r\n");
88 message = message + "\r\n";
90 String sendM = message.replace("\r", "").replace("\n.\n", "\n")
91 .replace("\n.\n", "\n").replace("\n", "\r\n")
101 private static void readResponse(BufferedReader in) throws IOException {
103 while ((line = in.readLine()) != null && line.matches("\\d+-")) {