smtp.php 23 KB

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