smtp.php 26 KB

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