smtp.php 24 KB

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