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