smtp.php 25 KB

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