-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
smtp.php
executable file
·281 lines (264 loc) · 9.25 KB
/
smtp.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
<?php
/* SMTP */
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
function addAddresses($mail, $header, $s) {
$addresses = explode(',', $s); /* XXX ideally, delimit on either , or ; - but the RFC says , is the right one */
foreach ($addresses as $address) {
if (strlen($address) < 1) {
continue;
}
$addr = '';
$name = '';
$c = strpos($address, '<');
if ($c !== false) {
/* Already in name <email> or <email> format.
* Split everything up as needed. */
$name = substr($address, 0, $c);
$addr = substr($address, $c + 1);
$c = strpos($addr, '>');
if ($c !== false) {
$addr = substr($addr, 0, $c); /* Strip trailing > */
}
} else {
$addr = $address;
}
switch ($header) {
case 'To':
$mail->addAddress($addr, $name);
break;
case 'Cc':
$mail->addCC($addr, $name);
break;
case 'Bcc':
$mail->addBCC($addr, $name);
break;
case 'From':
$mail->setFrom($addr, $name); /* Can only be 1 of these, but reuse parsing for it */
break;
case 'Reply-To':
$mail->addReplyTo($addr, $name);
break;
default:
/* Shouldn't happen */
fprintf(STDERR, "Default case");
die();
}
}
return $mail;
}
function send_message(array $webMailCookie, bool $send) {
global $settings;
$smtpDebug = false; /* Enable for SMTP debugging */
$connInfo = "Connecting to ";
$progname = "wssmail";
$progver = "0.1.1";
/* The only actual mandatory fields here is "To". */
if (strlen($_POST['to']) < 1) {
return "Missing recipient (To)";
}
$from = $_POST['from'];
$to = $_POST['to'];
$replyto = $_POST['replyto'];
$cc = $_POST['cc'];
$bcc = $_POST['bcc'];
$subject = $_POST['subject'];
$inreplyto = $_POST['inreplyto'];
$references = $_POST['references'];
$body = $_POST['body'];
$attachments = $_FILES['attachments'];
$priority = (int) $_POST['priority'];
/* Only the password from basic authentication is used.
* The username is ignored; we use the username from the cookie. */
$password = $_SERVER['PHP_AUTH_PW'];
/* We don't need to do any validation here. That's the SMTP server's responsibility.
* e.g. validation of the From address, etc.
* If something is wrong with the message, it will get rejected and we can just return that error. */
/* Create the actual message. At this time, we only support plain text messages. */
$messageid = uniqid() . "@" . $webMailCookie['smtpserver'];
$msg = "";
$msg .= "Message-ID: $messageid\r\n";
$msg .= "Date: " . date(DATE_RFC2822) . "\r\n";
if (strlen($subject) > 0) {
$msg .= "Subject: $subject\r\n";
}
if (!(strlen($from) > 0)) {
$from = $webMailCookie['username']; /* Default to the username (which is probably a user@domain) */
}
if (!(strlen($from) > 0)) {
return "Missing From";
}
$msg .= "From: $from\r\n";
if (strlen($replyto) > 0) {
$msg .= "Reply-To: $replyto\r\n";
}
/* comma-delimited */
$msg .= "To: $from\r\n";
$msg .= "Content-Type: text/plain; format=flowed\r\n";
$msg .= "User-Agent: $progname $progver (https://github.com/InterLinked1/wssmail)\r\n";
$msg .= "\r\n";
/* Create a format=flowed plain text body (RFC 3676 - 4.2) */
$ffbody = "";
$line = strtok($body, "\n");
while ($line !== false) {
rtrim($line); /* Trim spaces before user line breaks */
if (substr($line, -1) === "\r") {
/* Also strip CR if present */
$line = substr($line, 0, -1);
}
/* Need to space stuff.
* Could use str_starts_with, but use substring for <8 compatibility */
if (substr($line, 0, 1) === " " || substr($line, 0, 4) === "From" || substr($line, 0, 1) === ">") {
$line = " " . $line;
}
/* Now it's deterministic
* Wrap each line at 76 characters, with a space after to make it format=flowed.
*/
$line = wordwrap($line, 76, " \r\n");
$ffbody .= $line . "\r\n";
$line = strtok("\n");
}
/* Now we have the plain text component of the body done. */
$msg .= $ffbody;
/* Now we have a full message. Time to talk SMTP. */
try {
$mail = new PHPMailer();
$mail->Host = $webMailCookie['smtpserver'];
$connInfo .= $mail->Host;
$mail->isSMTP();
/* By default, these are 5 minutes, which is way too long. Do 15 seconds instead. */
$mail->getSMTPInstance()->Timeout = 15;
$mail->getSMTPInstance()->Timelimit = 15;
if ($smtpDebug) {
$mail->Debugoutput = 'html';
$mail->SMTPDebug = 3;
}
$mail->SMTPAuth = true;
/* Assume SMTP credentials are the same as the IMAP ones. */
$mail->Username = $webMailCookie['username'];
$mail->Password = $password;
$mail->Port = $webMailCookie['smtpport'];
$connInfo .= " port " . $mail->Port;
if ($webMailCookie['smtpsecurity'] === "starttls") {
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$connInfo .= " (STARTTLS)";
} else if ($webMailCookie['smtpsecurity'] === "tls") {
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
$connInfo .= " (TLS)";
} else {
$mail->SMTPAutoTLS = false; /* If the user did not select encryption, do not attempt it, even if server advertises. */
$mail->SMTPSecure = "";
$connInfo .= " (unencrypted)";
}
/* It's okay if the cert doesn't validate if we're connecting to an exempt server. */
if (isset($settings['tls']['noverify']) && in_array($mail->Host, $settings['tls']['noverify'])) {
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
}
/* Headers */
$mail = addAddresses($mail, "From", $from);
$mail = addAddresses($mail, "Reply-To", $replyto);
$mail = addAddresses($mail, "To", $to);
$mail = addAddresses($mail, "Cc", $cc);
$mail = addAddresses($mail, "Bcc", $bcc);
if (strlen($subject) > 0) {
$mail->Subject = $subject;
}
$mail->isHTML(false);
$mail->XMailer = ' ';
if (strlen($references) > 0) {
$mail->addCustomHeader("References", $references);
}
if (strlen($inreplyto) > 0) {
$mail->addCustomHeader("In-Reply-To", $inreplyto);
}
if ($priority !== 3) {
switch ($priority) {
case 1:
$mail->addCustomHeader("X-Priority", "1 (Highest)");
$mail->addCustomHeader("Importance", "High");
$mail->addCustomHeader("Priority", "Urgent");
break;
case 2:
$mail->addCustomHeader("X-Priority", "2 (High)");
$mail->addCustomHeader("Importance", "High");
$mail->addCustomHeader("Priority", "Urgent");
break;
case 4:
$mail->addCustomHeader("X-Priority", "4 (Low)");
$mail->addCustomHeader("Importance", "Low");
$mail->addCustomHeader("Priority", "Non-Urgent");
break;
case 5:
$mail->addCustomHeader("X-Priority", "5 (Lowest)");
$mail->addCustomHeader("Importance", "Low");
$mail->addCustomHeader("Priority", "Non-Urgent");
break;
case 3:
default:
$mail->addCustomHeader("X-Priority", "3 (Normal)");
$mail->addCustomHeader("Importance", "Normal");
$mail->addCustomHeader("Priority", "Normal");
break;
}
}
$mail->addCustomHeader("User-Agent", "$progname $progver (PHP)");
$mail->Body = $_POST['body'];
/*! \todo XXX need to send format=flowed !!! */
//$mail->CharSet = "ISO-8859-1";
//$mail->addCustomHeader("Content-Type", "text/plain; charset=UTF-8; format=flowed");
/* Attach attachments */
$numAttachments = count($_FILES['attachments']['name']);
for ($i = 0; $i < $numAttachments; $i++) {
$tmpfile = $_FILES['attachments']['tmp_name'][$i];
$name = $_FILES['attachments']['name'][$i];
$mail->addAttachment($tmpfile, $name);
}
if ($send) {
if (!$mail->send()) {
return $connInfo . "<br>" . $mail->ErrorInfo;
}
} else {
$mail->preSend();
/* Needed so we can call getSentMIMEMessage */
}
if ($webMailCookie['append']) {
$sentMessage = $mail->getSentMIMEMessage() . "\r\n"; /* So it's consistent with the message */
/* Save a copy to the Sent folder.
* We have to open a new IMAP connection for this.
* In theory, for instances where JS opens the editor as a child tab,
* we could pass data "back up" to the parent, and it could use its
* existing IMAP connection to do the append, but in the cases of
* resubmits, we're basically in our own window, and it's cleaner to
* handle it here without involving the parent. */
$path = "{" . $webMailCookie['server'] . ":" . $webMailCookie['port'] . "/imap" . ($webMailCookie['security'] === 'tls' ? "/ssl" : "/notls") . "}" . ($send ? "Sent" : "Drafts");
$imap = imap_open($path, $webMailCookie['username'], $password);
if ($imap === false) {
return "Message sent, but failed to save copy of message to $path: " . imap_last_error();
}
$result = imap_append($imap, $path, $sentMessage, "\\Seen", date('d-M-Y H:i:s O'));
if ($webMailCookie['security'] !== 'tls') {
/* By default, PHP will log this notice to the error log for unencrypted IMAP servers offering AUTH=PLAIN:
* PHP Notice: PHP Request Shutdown: SECURITY PROBLEM: insecure server advertised AUTH=PLAIN (errflg=1)
* This can be ignored, so suppress from the logs by calling the error functions to flush the error before closing. */
imap_errors();
imap_alerts();
}
imap_close($imap);
if (!$result) {
return "Message sent, but failed to save copy of message to $path: " . imap_last_error();
}
/* Close IMAP connection and return success */
}
} catch (Exception $e) {
return $connInfo . "<br>" . $mail->ErrorInfo;
}
return null;
}
?>