smtp.php 23 KB

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