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