59177427 |
1 | <?php |
2ba13803 |
2 | |
35586184 |
3 | /** |
4 | * smtp.php |
5 | * |
6 | * Copyright (c) 1999-2001 The SquirrelMail Development Team |
7 | * Licensed under the GNU GPL. For full terms see the file COPYING. |
8 | * |
9 | * This contains all the functions needed to send messages through |
10 | * an smtp server or sendmail. |
11 | * |
12 | * $Id$ |
13 | */ |
b8ea4ed6 |
14 | |
35586184 |
15 | /*****************************************************************/ |
16 | /*** THIS FILE NEEDS TO HAVE ITS FORMATTING FIXED!!! ***/ |
17 | /*** PLEASE DO SO AND REMOVE THIS COMMENT SECTION. ***/ |
18 | /*** + Base level indent should begin at left margin, as ***/ |
19 | /*** the require_once and global lines below. ***/ |
20 | /*** + All identation should consist of four space blocks ***/ |
21 | /*** + Tab characters are evil. ***/ |
22 | /*** + all comments should use "slash-star ... star-slash" ***/ |
23 | /*** style -- no pound characters, no slash-slash style ***/ |
24 | /*** + FLOW CONTROL STATEMENTS (if, while, etc) SHOULD ***/ |
25 | /*** ALWAYS USE { AND } CHARACTERS!!! ***/ |
26 | /*** + Please use ' instead of ", when possible. Note " ***/ |
27 | /*** should always be used in _( ) function calls. ***/ |
28 | /*** Thank you for your help making the SM code more readable. ***/ |
29 | /*****************************************************************/ |
f435778e |
30 | |
35586184 |
31 | require_once('../functions/addressbook.php'); |
32 | require_once('../functions/plugin.php'); |
33 | |
34 | global $username, $popuser, $domain; |
465db5d7 |
35 | |
31afdefb |
36 | // This should most probably go to some initialization... |
37 | if (ereg("^([^@%/]+)[@%/](.+)$", $username, $usernamedata)) { |
38 | $popuser = $usernamedata[1]; |
39 | $domain = $usernamedata[2]; |
7350889b |
40 | unset($usernamedata); |
31afdefb |
41 | } else { |
42 | $popuser = $username; |
43 | } |
44 | // We need domain for smtp |
45 | if (!$domain) |
ec9f1c41 |
46 | $domain = getenv('HOSTNAME'); |
31afdefb |
47 | |
df15de21 |
48 | // Returns true only if this message is multipart |
49 | function isMultipart () { |
4ba45d11 |
50 | global $attachments; |
51 | |
52 | if (count($attachments)>0) |
53 | return true; |
54 | else |
55 | return false; |
df15de21 |
56 | } |
57 | |
a4102fd7 |
58 | // looks up aliases in the addressbook and expands them to |
59 | // the full address. |
cb13511f |
60 | // Adds @$domain if it wasn't in the address book and if it |
61 | // doesn't have an @ symbol in it |
a4102fd7 |
62 | function expandAddrs ($array) { |
cb13511f |
63 | global $domain; |
64 | |
44bace96 |
65 | // don't show errors -- kinda critical that we don't see |
66 | // them here since the redirect won't work if we do show them |
67 | $abook = addressbook_init(false); |
a4102fd7 |
68 | for ($i=0; $i < count($array); $i++) { |
69 | $result = $abook->lookup($array[$i]); |
70 | $ret = ""; |
71 | if (isset($result['email'])) { |
72 | if (isset($result['name'])) { |
73 | $ret = '"'.$result['name'].'" '; |
74 | } |
75 | $ret .= '<'.$result['email'].'>'; |
76 | $array[$i] = $ret; |
77 | } |
18e206dd |
78 | else |
79 | { |
912c949d |
80 | if (strpos($array[$i], '@') === false) |
81 | $array[$i] .= '@' . $domain; |
18e206dd |
82 | $array[$i] = '<' . $array[$i] . '>'; |
83 | } |
a4102fd7 |
84 | } |
85 | return $array; |
86 | } |
87 | |
93555af8 |
88 | |
89 | // looks up aliases in the addressbook and expands them to |
90 | // the RFC 821 valid RCPT address. ie <user@example.com> |
91 | // Adds @$domain if it wasn't in the address book and if it |
92 | // doesn't have an @ symbol in it |
93 | function expandRcptAddrs ($array) { |
94 | global $domain; |
95 | |
96 | // don't show errors -- kinda critical that we don't see |
97 | // them here since the redirect won't work if we do show them |
98 | $abook = addressbook_init(false); |
99 | for ($i=0; $i < count($array); $i++) { |
100 | $result = $abook->lookup($array[$i]); |
101 | $ret = ""; |
102 | if (isset($result['email'])) { |
103 | $ret = '<'.$result['email'].'>'; |
104 | $array[$i] = $ret; |
105 | } |
106 | else |
107 | { |
108 | if (strpos($array[$i], '@') === false) |
109 | $array[$i] .= '@' . $domain; |
110 | $array[$i] = '<' . $array[$i] . '>'; |
111 | } |
112 | } |
113 | return $array; |
114 | } |
115 | |
116 | |
df15de21 |
117 | // Attach the files that are due to be attached |
4ba45d11 |
118 | function attachFiles ($fp) { |
c3c37167 |
119 | global $attachments, $attachment_dir; |
4ba45d11 |
120 | |
7b67334e |
121 | $length = 0; |
122 | |
a7d75834 |
123 | if (isMultipart()) { |
f972eb46 |
124 | foreach ($attachments as $info) |
125 | { |
4f601b14 |
126 | if (isset($info['type'])) |
7d50f13c |
127 | $filetype = $info['type']; |
128 | else |
ec9f1c41 |
129 | $filetype = 'application/octet-stream'; |
a7d75834 |
130 | |
ec9f1c41 |
131 | $header = '--'.mimeBoundary()."\r\n"; |
f972eb46 |
132 | $header .= "Content-Type: $filetype; name=\"" . |
133 | $info['remotefilename'] . "\"\r\n"; |
134 | $header .= "Content-Disposition: attachment; filename=\"" . |
135 | $info['remotefilename'] . "\"\r\n"; |
a7d75834 |
136 | |
f61de966 |
137 | // Use 'rb' for NT systems -- read binary |
138 | // Unix doesn't care -- everything's binary! :-) |
139 | $file = fopen ($attachment_dir . $info['localfilename'], 'rb'); |
aaf9abef |
140 | if (substr($filetype, 0, 5) == 'text/' || |
141 | $filetype == 'message/rfc822') { |
142 | $header .= "\r\n"; |
143 | fputs ($fp, $header); |
144 | $length += strlen($header); |
145 | while ($tmp = fgets($file, 4096)) { |
146 | $tmp = str_replace("\r\n", "\n", $tmp); |
147 | $tmp = str_replace("\r", "\n", $tmp); |
148 | $tmp = str_replace("\n", "\r\n", $tmp); |
e9eca7fe |
149 | if (feof($fp) && substr($tmp, -2) != "\r\n") |
150 | $tmp .= "\r\n"; |
aaf9abef |
151 | fputs($fp, $tmp); |
152 | $length += strlen($tmp); |
153 | } |
154 | } else { |
155 | $header .= "Content-Transfer-Encoding: base64\r\n\r\n"; |
156 | fputs ($fp, $header); |
157 | $length += strlen($header); |
158 | while ($tmp = fread($file, 570)) { |
159 | $encoded = chunk_split(base64_encode($tmp)); |
160 | $length += strlen($encoded); |
161 | fputs ($fp, $encoded); |
162 | } |
163 | } |
a7d75834 |
164 | fclose ($file); |
7b67334e |
165 | } |
4ba45d11 |
166 | } |
7b67334e |
167 | |
168 | return $length; |
df15de21 |
169 | } |
170 | |
a7d75834 |
171 | // Delete files that are uploaded for attaching |
172 | function deleteAttachments() { |
173 | global $attachments, $attachment_dir; |
174 | |
175 | if (isMultipart()) { |
176 | reset($attachments); |
177 | while (list($localname, $remotename) = each($attachments)) { |
178 | if (!ereg ("\\/", $localname)) { |
179 | unlink ($attachment_dir.$localname); |
ec9f1c41 |
180 | unlink ($attachment_dir.$localname.'.info'); |
a7d75834 |
181 | } |
182 | } |
183 | } |
184 | } |
185 | |
df15de21 |
186 | // Return a nice MIME-boundary |
187 | function mimeBoundary () { |
7b67334e |
188 | static $mimeBoundaryString; |
df15de21 |
189 | |
190 | if ($mimeBoundaryString == "") { |
ed865adf |
191 | $mimeBoundaryString = "----=_" . |
fe53c3b9 |
192 | GenerateRandomString(60, '\'()+,-./:=?_', 7); |
465db5d7 |
193 | } |
df15de21 |
194 | |
195 | return $mimeBoundaryString; |
465db5d7 |
196 | } |
197 | |
24397423 |
198 | /* Time offset for correct timezone */ |
199 | function timezone () { |
b0d1b51e |
200 | global $invert_time; |
201 | |
ec9f1c41 |
202 | $diff_second = date('Z'); |
d47b2518 |
203 | if ($invert_time) |
204 | $diff_second = - $diff_second; |
24397423 |
205 | if ($diff_second > 0) |
ec9f1c41 |
206 | $sign = '+'; |
24397423 |
207 | else |
ec9f1c41 |
208 | $sign = '-'; |
24397423 |
209 | |
210 | $diff_second = abs($diff_second); |
211 | |
212 | $diff_hour = floor ($diff_second / 3600); |
213 | $diff_minute = floor (($diff_second-3600*$diff_hour) / 60); |
214 | |
ec9f1c41 |
215 | $zonename = '('.strftime('%Z').')'; |
24397423 |
216 | $result = sprintf ("%s%02d%02d %s", $sign, $diff_hour, $diff_minute, $zonename); |
217 | return ($result); |
218 | } |
219 | |
df15de21 |
220 | /* Print all the needed RFC822 headers */ |
60994e13 |
221 | function write822Header ($fp, $t, $c, $b, $subject, $more_headers) { |
a7d75834 |
222 | global $REMOTE_ADDR, $SERVER_NAME, $REMOTE_PORT; |
31afdefb |
223 | global $data_dir, $username, $popuser, $domain, $version, $useSendmail; |
9949206d |
224 | global $default_charset, $HTTP_VIA, $HTTP_X_FORWARDED_FOR; |
aaf9abef |
225 | global $REMOTE_HOST, $identity; |
465db5d7 |
226 | |
7b67334e |
227 | // Storing the header to make sure the header is the same |
228 | // everytime the header is printed. |
229 | static $header, $headerlength; |
230 | |
ec9f1c41 |
231 | if ($header == '') { |
a4102fd7 |
232 | $to = expandAddrs(parseAddrs($t)); |
233 | $cc = expandAddrs(parseAddrs($c)); |
234 | $bcc = expandAddrs(parseAddrs($b)); |
aaf9abef |
235 | if (isset($identity) && $identity != 'default') |
236 | { |
237 | $reply_to = getPref($data_dir, $username, 'reply_to' . $identity); |
238 | $from = getPref($data_dir, $username, 'full_name' . $identity); |
239 | $from_addr = getPref($data_dir, $username, 'email_address' . $identity); |
240 | } |
241 | else |
242 | { |
243 | $reply_to = getPref($data_dir, $username, 'reply_to'); |
244 | $from = getPref($data_dir, $username, 'full_name'); |
245 | $from_addr = getPref($data_dir, $username, 'email_address'); |
246 | } |
31afdefb |
247 | |
ec9f1c41 |
248 | if ($from_addr == '') |
249 | $from_addr = $popuser.'@'.$domain; |
7b67334e |
250 | |
251 | $to_list = getLineOfAddrs($to); |
252 | $cc_list = getLineOfAddrs($cc); |
253 | $bcc_list = getLineOfAddrs($bcc); |
8ae331c2 |
254 | |
78e230b7 |
255 | /* Encoding 8-bit characters and making from line */ |
d51894be |
256 | $subject = encodeHeader($subject); |
ec9f1c41 |
257 | if ($from == '') |
7b67334e |
258 | $from = "<$from_addr>"; |
259 | else |
ec9f1c41 |
260 | $from = '"' . encodeHeader($from) . "\" <$from_addr>"; |
7b67334e |
261 | |
a7d75834 |
262 | /* This creates an RFC 822 date */ |
7b67334e |
263 | $date = date("D, j M Y H:i:s ", mktime()) . timezone(); |
a7d75834 |
264 | |
265 | /* Create a message-id */ |
ec9f1c41 |
266 | $message_id = '<' . $REMOTE_PORT . '.' . $REMOTE_ADDR . '.'; |
267 | $message_id .= time() . '.squirrel@' . $SERVER_NAME .'>'; |
7b67334e |
268 | |
269 | /* Make an RFC822 Received: line */ |
a5363806 |
270 | if (isset($REMOTE_HOST)) |
8a2848f0 |
271 | $received_from = "$REMOTE_HOST ([$REMOTE_ADDR])"; |
a5363806 |
272 | else |
8a2848f0 |
273 | $received_from = $REMOTE_ADDR; |
a5363806 |
274 | |
9949206d |
275 | if (isset($HTTP_VIA) || isset ($HTTP_X_FORWARDED_FOR)) { |
ec9f1c41 |
276 | if ($HTTP_X_FORWARDED_FOR == '') |
277 | $HTTP_X_FORWARDED_FOR = 'unknown'; |
9949206d |
278 | $received_from .= " (proxying for $HTTP_X_FORWARDED_FOR)"; |
279 | } |
a5363806 |
280 | |
8a2848f0 |
281 | $header = "Received: from $received_from\r\n"; |
282 | $header .= " (SquirrelMail authenticated user $username)\r\n"; |
283 | $header .= " by $SERVER_NAME with HTTP;\r\n"; |
284 | $header .= " $date\r\n"; |
7b67334e |
285 | |
a7d75834 |
286 | /* Insert the rest of the header fields */ |
287 | $header .= "Message-ID: $message_id\r\n"; |
7b67334e |
288 | $header .= "Date: $date\r\n"; |
289 | $header .= "Subject: $subject\r\n"; |
290 | $header .= "From: $from\r\n"; |
18e206dd |
291 | $header .= "To: $to_list\r\n"; // Who it's TO |
60994e13 |
292 | |
293 | /* Insert headers from the $more_headers array */ |
294 | if(is_array($more_headers)) { |
295 | reset($more_headers); |
296 | while(list($h_name, $h_val) = each($more_headers)) { |
297 | $header .= sprintf("%s: %s\r\n", $h_name, $h_val); |
298 | } |
299 | } |
300 | |
7b67334e |
301 | if ($cc_list) { |
302 | $header .= "Cc: $cc_list\r\n"; // Who the CCs are |
df15de21 |
303 | } |
7b67334e |
304 | |
ec9f1c41 |
305 | if ($reply_to != '') |
7b67334e |
306 | $header .= "Reply-To: $reply_to\r\n"; |
307 | |
308 | if ($useSendmail) { |
309 | if ($bcc_list) { |
310 | // BCCs is removed from header by sendmail |
311 | $header .= "Bcc: $bcc_list\r\n"; |
312 | } |
313 | } |
314 | |
315 | $header .= "X-Mailer: SquirrelMail (version $version)\r\n"; // Identify SquirrelMail |
316 | |
317 | // Do the MIME-stuff |
f8308f16 |
318 | $header .= "MIME-Version: 1.0\r\n"; |
7b67334e |
319 | |
320 | if (isMultipart()) { |
ec9f1c41 |
321 | $header .= 'Content-Type: multipart/mixed; boundary="'; |
7b67334e |
322 | $header .= mimeBoundary(); |
323 | $header .= "\"\r\n"; |
324 | } else { |
ec9f1c41 |
325 | if ($default_charset != '') |
17ce8467 |
326 | $header .= "Content-Type: text/plain; charset=$default_charset\r\n"; |
327 | else |
328 | $header .= "Content-Type: text/plain;\r\n"; |
7b67334e |
329 | $header .= "Content-Transfer-Encoding: 8bit\r\n"; |
330 | } |
331 | $header .= "\r\n"; // One blank line to separate header and body |
df15de21 |
332 | |
7b67334e |
333 | $headerlength = strlen($header); |
334 | } |
335 | |
336 | // Write the header |
337 | fputs ($fp, $header); |
df15de21 |
338 | |
7b67334e |
339 | return $headerlength; |
340 | } |
df15de21 |
341 | |
7b67334e |
342 | // Send the body |
343 | function writeBody ($fp, $passedBody) { |
17ce8467 |
344 | global $default_charset; |
345 | |
7b67334e |
346 | $attachmentlength = 0; |
347 | |
df15de21 |
348 | if (isMultipart()) { |
ec9f1c41 |
349 | $body = '--'.mimeBoundary()."\r\n"; |
17ce8467 |
350 | |
351 | if ($default_charset != "") |
352 | $body .= "Content-Type: text/plain; charset=$default_charset\r\n"; |
353 | else |
354 | $body .= "Content-Type: text/plain\r\n"; |
355 | |
7b67334e |
356 | $body .= "Content-Transfer-Encoding: 8bit\r\n\r\n"; |
d51894be |
357 | $body .= $passedBody . "\r\n\r\n"; |
7b67334e |
358 | fputs ($fp, $body); |
359 | |
6441f7c6 |
360 | $attachmentlength = attachFiles($fp); |
7b67334e |
361 | |
f8cfeb5c |
362 | if (!isset($postbody)) $postbody = ""; |
7b67334e |
363 | $postbody .= "\r\n--".mimeBoundary()."--\r\n\r\n"; |
364 | fputs ($fp, $postbody); |
df15de21 |
365 | } else { |
d51894be |
366 | $body = $passedBody . "\r\n"; |
7b67334e |
367 | fputs ($fp, $body); |
368 | $postbody = "\r\n"; |
369 | fputs ($fp, $postbody); |
df15de21 |
370 | } |
df15de21 |
371 | |
7b67334e |
372 | return (strlen($body) + strlen($postbody) + $attachmentlength); |
c99c5b31 |
373 | } |
465db5d7 |
374 | |
c99c5b31 |
375 | // Send mail using the sendmail command |
60994e13 |
376 | function sendSendmail($t, $c, $b, $subject, $body, $more_headers) { |
31afdefb |
377 | global $sendmail_path, $popuser, $username, $domain; |
7b67334e |
378 | |
6ebf8b30 |
379 | // Build envelope sender address. Make sure it doesn't contain |
380 | // spaces or other "weird" chars that would allow a user to |
381 | // exploit the shell/pipe it is used in. |
31afdefb |
382 | $envelopefrom = "$popuser@$domain"; |
ec9f1c41 |
383 | $envelopefrom = ereg_replace("[[:blank:]]",'', $envelopefrom); |
384 | $envelopefrom = ereg_replace("[[:space:]]",'', $envelopefrom); |
385 | $envelopefrom = ereg_replace("[[:cntrl:]]",'', $envelopefrom); |
6ebf8b30 |
386 | |
e957cd4a |
387 | // open pipe to sendmail or qmail-inject (qmail-inject doesn't accept -t param) |
388 | if (strstr($sendmail_path, "qmail-inject")) { |
389 | $fp = popen (escapeshellcmd("$sendmail_path -f$envelopefrom"), "w"); |
390 | } else { |
391 | $fp = popen (escapeshellcmd("$sendmail_path -t -f$envelopefrom"), "w"); |
392 | } |
393 | |
60994e13 |
394 | $headerlength = write822Header ($fp, $t, $c, $b, $subject, $more_headers); |
7b67334e |
395 | $bodylength = writeBody($fp, $body); |
465db5d7 |
396 | |
397 | pclose($fp); |
7b67334e |
398 | |
0775b17f |
399 | return ($headerlength + $bodylength); |
465db5d7 |
400 | } |
401 | |
b8ea4ed6 |
402 | function smtpReadData($smtpConnection) { |
403 | $read = fgets($smtpConnection, 1024); |
404 | $counter = 0; |
405 | while ($read) { |
ec9f1c41 |
406 | echo $read . '<BR>'; |
b8ea4ed6 |
407 | $data[$counter] = $read; |
408 | $read = fgets($smtpConnection, 1024); |
409 | $counter++; |
410 | } |
411 | } |
412 | |
60994e13 |
413 | function sendSMTP($t, $c, $b, $subject, $body, $more_headers) { |
8de3c34d |
414 | global $username, $popuser, $domain, $version, $smtpServerAddress, |
415 | $smtpPort, $data_dir, $color, $use_authenticated_smtp, $identity, |
416 | $key, $onetimepad; |
7831268e |
417 | |
93555af8 |
418 | $to = expandRcptAddrs(parseAddrs($t)); |
419 | $cc = expandRcptAddrs(parseAddrs($c)); |
420 | $bcc = expandRcptAddrs(parseAddrs($b)); |
aaf9abef |
421 | if (isset($identity) && $identity != 'default') |
422 | $from_addr = getPref($data_dir, $username, 'email_address' . $identity); |
423 | else |
424 | $from_addr = getPref($data_dir, $username, 'email_address'); |
356d7825 |
425 | |
31afdefb |
426 | if (!$from_addr) |
427 | $from_addr = "$popuser@$domain"; |
d3cdb279 |
428 | |
c175e2e4 |
429 | $smtpConnection = fsockopen($smtpServerAddress, $smtpPort, $errorNumber, $errorString); |
b8ea4ed6 |
430 | if (!$smtpConnection) { |
ec9f1c41 |
431 | echo 'Error connecting to SMTP Server.<br>'; |
b8ea4ed6 |
432 | echo "$errorNumber : $errorString<br>"; |
433 | exit; |
434 | } |
f8a9394d |
435 | $tmp = fgets($smtpConnection, 1024); |
d5f34863 |
436 | if (errorCheck($tmp, $smtpConnection)!=5) return(0); |
b8ea4ed6 |
437 | |
40ee9452 |
438 | $to_list = getLineOfAddrs($to); |
439 | $cc_list = getLineOfAddrs($cc); |
440 | |
441 | /** Lets introduce ourselves */ |
d402e33e |
442 | if (! isset ($use_authenticated_smtp) || $use_authenticated_smtp == false) { |
d2f4c914 |
443 | fputs($smtpConnection, "HELO $domain\r\n"); |
444 | $tmp = fgets($smtpConnection, 1024); |
d5f34863 |
445 | if (errorCheck($tmp, $smtpConnection)!=5) return(0); |
d2f4c914 |
446 | } else { |
447 | fputs($smtpConnection, "EHLO $domain\r\n"); |
448 | $tmp = fgets($smtpConnection, 1024); |
d5f34863 |
449 | if (errorCheck($tmp, $smtpConnection)!=5) return(0); |
d2f4c914 |
450 | |
451 | fputs($smtpConnection, "AUTH LOGIN\r\n"); |
452 | $tmp = fgets($smtpConnection, 1024); |
d5f34863 |
453 | if (errorCheck($tmp, $smtpConnection)!=5) return(0); |
d2f4c914 |
454 | |
455 | fputs($smtpConnection, base64_encode ($username) . "\r\n"); |
456 | $tmp = fgets($smtpConnection, 1024); |
d5f34863 |
457 | if (errorCheck($tmp, $smtpConnection)!=5) return(0); |
d2f4c914 |
458 | |
16cad51a |
459 | fputs($smtpConnection, base64_encode (OneTimePadDecrypt($key, $onetimepad)) . "\r\n"); |
d2f4c914 |
460 | $tmp = fgets($smtpConnection, 1024); |
d5f34863 |
461 | if (errorCheck($tmp, $smtpConnection)!=5) return(0); |
d2f4c914 |
462 | } |
7831268e |
463 | |
40ee9452 |
464 | /** Ok, who is sending the message? */ |
f89627f3 |
465 | fputs($smtpConnection, "MAIL FROM: <$from_addr>\r\n"); |
f8a9394d |
466 | $tmp = fgets($smtpConnection, 1024); |
d5f34863 |
467 | if (errorCheck($tmp, $smtpConnection)!=5) return(0); |
b8ea4ed6 |
468 | |
40ee9452 |
469 | /** send who the recipients are */ |
470 | for ($i = 0; $i < count($to); $i++) { |
18e206dd |
471 | fputs($smtpConnection, "RCPT TO: $to[$i]\r\n"); |
f8a9394d |
472 | $tmp = fgets($smtpConnection, 1024); |
d5f34863 |
473 | if (errorCheck($tmp, $smtpConnection)!=5) return(0); |
40ee9452 |
474 | } |
475 | for ($i = 0; $i < count($cc); $i++) { |
b4338e67 |
476 | fputs($smtpConnection, "RCPT TO: $cc[$i]\r\n"); |
f8a9394d |
477 | $tmp = fgets($smtpConnection, 1024); |
d5f34863 |
478 | if (errorCheck($tmp, $smtpConnection)!=5) return(0); |
40ee9452 |
479 | } |
480 | for ($i = 0; $i < count($bcc); $i++) { |
b4338e67 |
481 | fputs($smtpConnection, "RCPT TO: $bcc[$i]\r\n"); |
f8a9394d |
482 | $tmp = fgets($smtpConnection, 1024); |
d5f34863 |
483 | if (errorCheck($tmp, $smtpConnection)!=5) return(0); |
40ee9452 |
484 | } |
b8ea4ed6 |
485 | |
40ee9452 |
486 | /** Lets start sending the actual message */ |
3379d740 |
487 | fputs($smtpConnection, "DATA\r\n"); |
f8a9394d |
488 | $tmp = fgets($smtpConnection, 1024); |
d5f34863 |
489 | if (errorCheck($tmp, $smtpConnection)!=5) return(0); |
7831268e |
490 | |
7b67334e |
491 | // Send the message |
60994e13 |
492 | $headerlength = write822Header ($smtpConnection, $t, $c, $b, $subject, $more_headers); |
7b67334e |
493 | $bodylength = writeBody($smtpConnection, $body); |
7831268e |
494 | |
3379d740 |
495 | fputs($smtpConnection, ".\r\n"); // end the DATA part |
f8a9394d |
496 | $tmp = fgets($smtpConnection, 1024); |
354eb441 |
497 | $num = errorCheck($tmp, $smtpConnection, true); |
3021b626 |
498 | if ($num != 250) { |
d5f34863 |
499 | $tmp = nl2br(htmlspecialchars($tmp)); |
500 | displayPageHeader($color, 'None'); |
0fc2aca0 |
501 | include_once('../functions/display_messages.php'); |
d5f34863 |
502 | $msg = "Message not sent!<br>\nReason given: $tmp"; |
503 | plain_error_message($msg, $color); |
504 | return(0); |
3021b626 |
505 | } |
d3cdb279 |
506 | |
3379d740 |
507 | fputs($smtpConnection, "QUIT\r\n"); // log off |
78509c54 |
508 | |
509 | fclose($smtpConnection); |
7b67334e |
510 | |
511 | return ($headerlength + $bodylength); |
b8ea4ed6 |
512 | } |
3021b626 |
513 | |
514 | |
354eb441 |
515 | function errorCheck($line, $smtpConnection, $verbose = false) { |
28010579 |
516 | global $color; |
776c7431 |
517 | |
f8a9394d |
518 | // Read new lines on a multiline response |
519 | $lines = $line; |
520 | while(ereg("^[0-9]+-", $line)) { |
521 | $line = fgets($smtpConnection, 1024); |
522 | $lines .= $line; |
523 | } |
524 | |
3021b626 |
525 | // Status: 0 = fatal |
526 | // 5 = ok |
527 | |
528 | $err_num = substr($line, 0, strpos($line, " ")); |
529 | switch ($err_num) { |
ec9f1c41 |
530 | case 500: $message = 'Syntax error; command not recognized'; |
3021b626 |
531 | $status = 0; |
532 | break; |
ec9f1c41 |
533 | case 501: $message = 'Syntax error in parameters or arguments'; |
3021b626 |
534 | $status = 0; |
535 | break; |
ec9f1c41 |
536 | case 502: $message = 'Command not implemented'; |
3021b626 |
537 | $status = 0; |
538 | break; |
ec9f1c41 |
539 | case 503: $message = 'Bad sequence of commands'; |
3021b626 |
540 | $status = 0; |
541 | break; |
ec9f1c41 |
542 | case 504: $message = 'Command parameter not implemented'; |
3021b626 |
543 | $status = 0; |
544 | break; |
545 | |
546 | |
ec9f1c41 |
547 | case 211: $message = 'System status, or system help reply'; |
3021b626 |
548 | $status = 5; |
549 | break; |
ec9f1c41 |
550 | case 214: $message = 'Help message'; |
3021b626 |
551 | $status = 5; |
552 | break; |
553 | |
554 | |
ec9f1c41 |
555 | case 220: $message = 'Service ready'; |
3021b626 |
556 | $status = 5; |
557 | break; |
ec9f1c41 |
558 | case 221: $message = 'Service closing transmission channel'; |
3021b626 |
559 | $status = 5; |
560 | break; |
ec9f1c41 |
561 | case 421: $message = 'Service not available, closing chanel'; |
3021b626 |
562 | $status = 0; |
563 | break; |
564 | |
d5f34863 |
565 | case 235: return(5); break; |
ec9f1c41 |
566 | case 250: $message = 'Requested mail action okay, completed'; |
3021b626 |
567 | $status = 5; |
568 | break; |
ec9f1c41 |
569 | case 251: $message = 'User not local; will forward'; |
3021b626 |
570 | $status = 5; |
571 | break; |
d5f34863 |
572 | case 334: return(5); break; |
ec9f1c41 |
573 | case 450: $message = 'Requested mail action not taken: mailbox unavailable'; |
3021b626 |
574 | $status = 0; |
575 | break; |
ec9f1c41 |
576 | case 550: $message = 'Requested action not taken: mailbox unavailable'; |
3021b626 |
577 | $status = 0; |
578 | break; |
ec9f1c41 |
579 | case 451: $message = 'Requested action aborted: error in processing'; |
3021b626 |
580 | $status = 0; |
581 | break; |
ec9f1c41 |
582 | case 551: $message = 'User not local; please try forwarding'; |
3021b626 |
583 | $status = 0; |
584 | break; |
ec9f1c41 |
585 | case 452: $message = 'Requested action not taken: insufficient system storage'; |
3021b626 |
586 | $status = 0; |
587 | break; |
ec9f1c41 |
588 | case 552: $message = 'Requested mail action aborted: exceeding storage allocation'; |
3021b626 |
589 | $status = 0; |
590 | break; |
ec9f1c41 |
591 | case 553: $message = 'Requested action not taken: mailbox name not allowed'; |
3021b626 |
592 | $status = 0; |
593 | break; |
ec9f1c41 |
594 | case 354: $message = 'Start mail input; end with .'; |
3021b626 |
595 | $status = 5; |
596 | break; |
ec9f1c41 |
597 | case 554: $message = 'Transaction failed'; |
3021b626 |
598 | $status = 0; |
599 | break; |
434666c7 |
600 | default: $message = 'Unknown response: '. nl2br(htmlspecialchars($lines)); |
3021b626 |
601 | $status = 0; |
ec9f1c41 |
602 | $error_num = '001'; |
3021b626 |
603 | break; |
604 | } |
605 | |
606 | if ($status == 0) { |
0fc2aca0 |
607 | include_once('../functions/page_header.php'); |
ec9f1c41 |
608 | displayPageHeader($color, 'None'); |
0fc2aca0 |
609 | include_once('../functions/display_messages.php'); |
f8a9394d |
610 | $lines = nl2br(htmlspecialchars($lines)); |
d5f34863 |
611 | $msg = $message . "<br>\nServer replied: $lines"; |
612 | plain_error_message($msg, $color); |
3021b626 |
613 | } |
354eb441 |
614 | if (! $verbose) return $status; |
3021b626 |
615 | return $err_num; |
616 | } |
df15de21 |
617 | |
020abcf3 |
618 | function sendMessage($t, $c, $b, $subject, $body, $reply_id, $prio = 3) { |
d2f4c914 |
619 | global $useSendmail, $msg_id, $is_reply, $mailbox, $onetimepad; |
6441f7c6 |
620 | global $data_dir, $username, $domain, $key, $version, $sent_folder, $imapServerAddress, $imapPort; |
020abcf3 |
621 | global $default_use_priority; |
8fa48477 |
622 | global $more_headers; |
60994e13 |
623 | $more_headers = Array(); |
d1b8b679 |
624 | |
8fa48477 |
625 | do_hook("smtp_send"); |
626 | |
966286ae |
627 | $imap_stream = sqimap_login($username, $key, $imapServerAddress, $imapPort, 1); |
60994e13 |
628 | |
214e1343 |
629 | if (isset($reply_id) && $reply_id) { |
966286ae |
630 | sqimap_mailbox_select ($imap_stream, $mailbox); |
ec9f1c41 |
631 | sqimap_messages_flag ($imap_stream, $reply_id, $reply_id, 'Answered'); |
60994e13 |
632 | |
c95df380 |
633 | // Insert In-Reply-To and References headers if the |
634 | // message-id of the message we reply to is set (longer than "<>") |
635 | // The References header should really be the old Referenced header |
636 | // with the message ID appended, but it can be only the message ID too. |
637 | $hdr = sqimap_get_small_header ($imap_stream, $reply_id, false); |
638 | if(strlen($hdr->message_id) > 2) { |
ec9f1c41 |
639 | $more_headers['In-Reply-To'] = $hdr->message_id; |
640 | $more_headers['References'] = $hdr->message_id; |
c95df380 |
641 | } |
966286ae |
642 | } |
020abcf3 |
643 | if ($default_use_priority) { |
644 | $more_headers = array_merge($more_headers, createPriorityHeaders($prio)); |
645 | } |
8ae331c2 |
646 | |
af8eba43 |
647 | // In order to remove the problem of users not able to create |
eedcdd97 |
648 | // messages with "." on a blank line, RFC821 has made provision |
649 | // in section 4.5.2 (Transparency). |
650 | $body = ereg_replace("\n\\.", "\n..", $body); |
651 | $body = ereg_replace("^\\.", "..", $body); |
af8eba43 |
652 | |
8ae331c2 |
653 | // this is to catch all plain \n instances and |
eedcdd97 |
654 | // replace them with \r\n. All newlines were converted |
655 | // into just \n inside the compose.php file. |
8ae331c2 |
656 | $body = ereg_replace("\n", "\r\n", $body); |
3a8c414d |
657 | |
8ae331c2 |
658 | if ($useSendmail) { |
60994e13 |
659 | $length = sendSendmail($t, $c, $b, $subject, $body, $more_headers); |
df15de21 |
660 | } else { |
60994e13 |
661 | $length = sendSMTP($t, $c, $b, $subject, $body, $more_headers); |
df15de21 |
662 | } |
d1b8b679 |
663 | |
2966f172 |
664 | if (sqimap_mailbox_exists ($imap_stream, $sent_folder)) { |
665 | sqimap_append ($imap_stream, $sent_folder, $length); |
60994e13 |
666 | write822Header ($imap_stream, $t, $c, $b, $subject, $more_headers); |
8ae331c2 |
667 | writeBody ($imap_stream, $body); |
2966f172 |
668 | sqimap_append_done ($imap_stream); |
8ae331c2 |
669 | } |
670 | sqimap_logout($imap_stream); |
a7d75834 |
671 | // Delete the files uploaded for attaching (if any). |
f95d9e7b |
672 | // only if $length != 0 (if there was no error) |
673 | if ($length) |
674 | ClearAttachments(); |
675 | |
676 | return $length; |
df15de21 |
677 | } |
020abcf3 |
678 | |
679 | function createPriorityHeaders($prio) { |
680 | $prio_headers = Array(); |
681 | $prio_headers["X-Priority"] = $prio; |
682 | |
683 | switch($prio) { |
684 | case 1: $prio_headers["Importance"] = "High"; |
685 | $prio_headers["X-MSMail-Priority"] = "High"; |
686 | break; |
687 | |
688 | case 3: $prio_headers["Importance"] = "Normal"; |
689 | $prio_headers["X-MSMail-Priority"] = "Normal"; |
690 | break; |
691 | |
692 | case 5: |
693 | $prio_headers["Importance"] = "Low"; |
694 | $prio_headers["X-MSMail-Priority"] = "Low"; |
695 | break; |
696 | } |
697 | return $prio_headers; |
698 | } |
699 | ?> |