]> WPIA git - gigi.git/blob - src/org/cacert/gigi/email/Sendmail.java
make email factor out usable
[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.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.regex.Pattern;
13
14 public class Sendmail extends EmailProvider {
15         protected Sendmail() {
16         }
17         private static final Pattern NON_ASCII = Pattern
18                         .compile("[^a-zA-Z0-9 .-\\[\\]!_@]");
19
20         @Override
21         public void sendmail(String to, String subject, String message,
22                         String from, String replyto, String toname, String fromname,
23                         String errorsto, boolean extra) throws IOException {
24
25                 String[] bits = from.split(",");
26
27                 Socket smtp = new Socket("dogcraft.de", 25);
28                 PrintWriter out = new PrintWriter(smtp.getOutputStream());
29                 BufferedReader in = new BufferedReader(new InputStreamReader(
30                                 smtp.getInputStream()));
31                 readResponse(in);
32                 out.print("HELO www.cacert.org\r\n");
33                 out.flush();
34                 readResponse(in);
35                 out.print("MAIL FROM:<returns@cacert.org>\r\n");
36                 out.flush();
37                 readResponse(in);
38                 bits = to.split(",");
39                 for (String user : bits) {
40                         out.print("RCPT TO:<" + user.trim() + ">\r\n");
41                         out.flush();
42                         readResponse(in);
43                 }
44                 out.print("DATA\r\n");
45                 out.flush();
46                 readResponse(in);
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(
53                                 "E, d MMM yyyy HH:mm:ss ZZZZ (z)", Locale.ENGLISH);
54                 out.print("Date: "
55                                 + emailDate.format(new Date(System.currentTimeMillis()))
56                                 + "\r\n");
57                 out.print("Sender: " + errorsto + "\r\n");
58                 out.print("Errors-To: " + errorsto + "\r\n");
59                 if (replyto != null) {
60                         out.print("Reply-To: " + replyto + "\r\n");
61                 } else {
62                         out.print("Reply-To: " + from + "\r\n");
63                 }
64                 out.print("From: " + from + "\r\n");
65                 out.print("To: " + to + "\r\n");
66                 if (NON_ASCII.matcher(subject).matches()) {
67
68                         out.print("Subject: =?utf-8?B?"
69                                         + Base64.getEncoder().encodeToString(subject.getBytes())
70                                         + "?=\r\n");
71                 } else {
72                         out.print("Subject: " + subject + "\r\n");
73                 }
74                 out.print("Mime-Version: 1.0\r\n");
75                 if (!extra) {
76                         out.print("Content-Type: text/plain; charset=\"utf-8\"\r\n");
77                         out.print("Content-Transfer-Encoding: 8bit\r\n");
78                 } else {
79                         out.print("Content-Type: text/plain; charset=\"iso-8859-1\"\r\n");
80                         out.print("Content-Transfer-Encoding: quoted-printable\r\n");
81                         out.print("Content-Disposition: inline\r\n");
82                 }
83                 // out.print("Content-Transfer-Encoding: BASE64\r\n");
84                 out.print("\r\n");
85                 // out.print(chunk_split(base64_encode(recode("html..utf-8",
86                 // $message)))."\r\n.\r\n");
87                 message = message + "\r\n";
88
89                 String sendM = message.replace("\r", "").replace("\n.\n", "\n")
90                                 .replace("\n.\n", "\n").replace("\n", "\r\n")
91                                 + ".\r\n";
92                 out.print(sendM);
93                 out.flush();
94                 readResponse(in);
95                 out.print("QUIT\n");
96                 out.flush();
97                 readResponse(in);
98                 smtp.close();
99         }
100         private static void readResponse(BufferedReader in) throws IOException {
101                 String line;
102                 while ((line = in.readLine()) != null && line.matches("\\d+-")) {
103                 }
104
105         }
106
107 }