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