]> WPIA git - gigi.git/blob - src/org/cacert/gigi/email/Sendmail.java
f47d2bc3cbefdba25a40a95f20c5557f4b7e5c1e
[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
19         private static final Pattern NON_ASCII = Pattern.compile("[^a-zA-Z0-9 .-\\[\\]!_@]");
20
21         @Override
22         public void sendmail(String to, String subject, String message, String from, String replyto, String toname,
23                 String fromname, 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(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("E, d MMM yyyy HH:mm:ss ZZZZ (z)", Locale.ENGLISH);
52                 out.print("Date: " + emailDate.format(new Date(System.currentTimeMillis())) + "\r\n");
53                 out.print("Sender: " + errorsto + "\r\n");
54                 out.print("Errors-To: " + errorsto + "\r\n");
55                 if (replyto != null) {
56                         out.print("Reply-To: " + replyto + "\r\n");
57                 } else {
58                         out.print("Reply-To: " + from + "\r\n");
59                 }
60                 out.print("From: " + from + "\r\n");
61                 out.print("To: " + to + "\r\n");
62                 if (NON_ASCII.matcher(subject).matches()) {
63
64                         out.print("Subject: =?utf-8?B?" + Base64.getEncoder().encodeToString(subject.getBytes()) + "?=\r\n");
65                 } else {
66                         out.print("Subject: " + subject + "\r\n");
67                 }
68                 out.print("Mime-Version: 1.0\r\n");
69                 if (!extra) {
70                         out.print("Content-Type: text/plain; charset=\"utf-8\"\r\n");
71                         out.print("Content-Transfer-Encoding: 8bit\r\n");
72                 } else {
73                         out.print("Content-Type: text/plain; charset=\"iso-8859-1\"\r\n");
74                         out.print("Content-Transfer-Encoding: quoted-printable\r\n");
75                         out.print("Content-Disposition: inline\r\n");
76                 }
77                 // out.print("Content-Transfer-Encoding: BASE64\r\n");
78                 out.print("\r\n");
79                 // out.print(chunk_split(base64_encode(recode("html..utf-8",
80                 // $message)))."\r\n.\r\n");
81                 message = message + "\r\n";
82
83                 String sendM = message.replace("\r", "").replace("\n.\n", "\n").replace("\n.\n", "\n").replace("\n", "\r\n")
84                         + ".\r\n";
85                 out.print(sendM);
86                 out.flush();
87                 readResponse(in);
88                 out.print("QUIT\n");
89                 out.flush();
90                 readResponse(in);
91                 smtp.close();
92         }
93
94         private static void readResponse(BufferedReader in) throws IOException {
95                 String line;
96                 while ((line = in.readLine()) != null && line.matches("\\d+-")) {
97                 }
98
99         }
100
101 }