]> WPIA git - gigi.git/blob - src/org/cacert/gigi/email/Sendmail.java
Merge branch 'libs/jetty/upstream' into libs/jetty/local
[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.Properties;
13 import java.util.regex.Pattern;
14
15 public class Sendmail extends EmailProvider {
16         protected Sendmail(Properties props) {
17         }
18         private static final Pattern NON_ASCII = Pattern
19                         .compile("[^a-zA-Z0-9 .-\\[\\]!_@]");
20
21         @Override
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 {
25
26                 String[] bits = from.split(",");
27
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()));
32                 readResponse(in);
33                 out.print("HELO www.cacert.org\r\n");
34                 out.flush();
35                 readResponse(in);
36                 out.print("MAIL FROM:<returns@cacert.org>\r\n");
37                 out.flush();
38                 readResponse(in);
39                 bits = to.split(",");
40                 for (String user : bits) {
41                         out.print("RCPT TO:<" + user.trim() + ">\r\n");
42                         out.flush();
43                         readResponse(in);
44                 }
45                 out.print("DATA\r\n");
46                 out.flush();
47                 readResponse(in);
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");
51                 // }
52                 // TODO
53                 SimpleDateFormat emailDate = new SimpleDateFormat(
54                                 "E, d MMM yyyy HH:mm:ss ZZZZ (z)", Locale.ENGLISH);
55                 out.print("Date: "
56                                 + emailDate.format(new Date(System.currentTimeMillis()))
57                                 + "\r\n");
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");
62                 } else {
63                         out.print("Reply-To: " + from + "\r\n");
64                 }
65                 out.print("From: " + from + "\r\n");
66                 out.print("To: " + to + "\r\n");
67                 if (NON_ASCII.matcher(subject).matches()) {
68
69                         out.print("Subject: =?utf-8?B?"
70                                         + Base64.getEncoder().encodeToString(subject.getBytes())
71                                         + "?=\r\n");
72                 } else {
73                         out.print("Subject: " + subject + "\r\n");
74                 }
75                 out.print("Mime-Version: 1.0\r\n");
76                 if (!extra) {
77                         out.print("Content-Type: text/plain; charset=\"utf-8\"\r\n");
78                         out.print("Content-Transfer-Encoding: 8bit\r\n");
79                 } else {
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");
83                 }
84                 // out.print("Content-Transfer-Encoding: BASE64\r\n");
85                 out.print("\r\n");
86                 // out.print(chunk_split(base64_encode(recode("html..utf-8",
87                 // $message)))."\r\n.\r\n");
88                 message = message + "\r\n";
89
90                 String sendM = message.replace("\r", "").replace("\n.\n", "\n")
91                                 .replace("\n.\n", "\n").replace("\n", "\r\n")
92                                 + ".\r\n";
93                 out.print(sendM);
94                 out.flush();
95                 readResponse(in);
96                 out.print("QUIT\n");
97                 out.flush();
98                 readResponse(in);
99                 smtp.close();
100         }
101         private static void readResponse(BufferedReader in) throws IOException {
102                 String line;
103                 while ((line = in.readLine()) != null && line.matches("\\d+-")) {
104                 }
105
106         }
107
108 }