smtp.php 23 KB

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