smtp.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. <?php
  2. /** smtp.php
  3. **
  4. ** This contains all the functions needed to send messages through
  5. ** an smtp server or sendmail.
  6. **/
  7. $smtp_php = true;
  8. // Returns true only if this message is multipart
  9. function isMultipart () {
  10. global $attachments;
  11. if (count($attachments)>0)
  12. return true;
  13. else
  14. return false;
  15. }
  16. // Attach the files that are due to be attached
  17. function attachFiles ($fp) {
  18. global $attachments, $attachment_dir;
  19. $length = 0;
  20. if (isMultipart()) {
  21. reset($attachments);
  22. while (list($localname, $remotename) = each($attachments)) {
  23. // This is to make sure noone is giving a filename in another
  24. // directory
  25. $localname = ereg_replace ("\\/", "", $localname);
  26. $fileinfo = fopen ($attachment_dir.$localname.".info", "r");
  27. $filetype = fgets ($fileinfo, 8192);
  28. fclose ($fileinfo);
  29. $filetype = trim ($filetype);
  30. if ($filetype=="")
  31. $filetype = "application/octet-stream";
  32. $header = "--".mimeBoundary()."\r\n";
  33. $header .= "Content-Type: $filetype\r\n";
  34. $header .= "Content-Disposition: attachment; filename=\"$remotename\"\r\n";
  35. $header .= "Content-Transfer-Encoding: base64\r\n\r\n";
  36. fputs ($fp, $header);
  37. $length += strlen($header);
  38. $file = fopen ($attachment_dir.$localname, "r");
  39. while ($tmp = fread($file, 570)) {
  40. $encoded = chunk_split(base64_encode($tmp));
  41. $length += strlen($encoded);
  42. fputs ($fp, $encoded);
  43. }
  44. fclose ($file);
  45. }
  46. }
  47. return $length;
  48. }
  49. // Delete files that are uploaded for attaching
  50. function deleteAttachments() {
  51. global $attachments, $attachment_dir;
  52. if (isMultipart()) {
  53. reset($attachments);
  54. while (list($localname, $remotename) = each($attachments)) {
  55. if (!ereg ("\\/", $localname)) {
  56. unlink ($attachment_dir.$localname);
  57. unlink ($attachment_dir.$localname.".info");
  58. }
  59. }
  60. }
  61. }
  62. // Return a nice MIME-boundary
  63. function mimeBoundary () {
  64. global $version, $REMOTE_ADDR, $SERVER_NAME, $REMOTE_PORT;
  65. static $mimeBoundaryString;
  66. if ($mimeBoundaryString == "") {
  67. $temp = "SquirrelMail".$version.$REMOTE_ADDR.$SERVER_NAME.
  68. $REMOTE_PORT;
  69. $mimeBoundaryString = "=-_+".substr(md5($temp),1,20);
  70. }
  71. return $mimeBoundaryString;
  72. }
  73. /* Time offset for correct timezone */
  74. function timezone () {
  75. $diff_second = date("Z");
  76. if ($diff_second > 0)
  77. $sign = "+";
  78. else
  79. $sign = "-";
  80. $diff_second = abs($diff_second);
  81. $diff_hour = floor ($diff_second / 3600);
  82. $diff_minute = floor (($diff_second-3600*$diff_hour) / 60);
  83. $zonename = "(".strftime("%Z").")";
  84. $result = sprintf ("%s%02d%02d %s", $sign, $diff_hour, $diff_minute, $zonename);
  85. return ($result);
  86. }
  87. /* Print all the needed RFC822 headers */
  88. function write822Header ($fp, $t, $c, $b, $subject) {
  89. global $REMOTE_ADDR, $SERVER_NAME, $REMOTE_PORT;
  90. global $data_dir, $username, $domain, $version, $useSendmail;
  91. global $default_charset;
  92. // Storing the header to make sure the header is the same
  93. // everytime the header is printed.
  94. static $header, $headerlength;
  95. if ($header == "") {
  96. $to = parseAddrs($t);
  97. $cc = parseAddrs($c);
  98. $bcc = parseAddrs($b);
  99. $reply_to = getPref($data_dir, $username, "reply_to");
  100. $from = getPref($data_dir, $username, "full_name");
  101. $from_addr = getPref($data_dir, $username, "email_address");
  102. if ($from_addr == "")
  103. $from_addr = "$username@$domain";
  104. $to_list = getLineOfAddrs($to);
  105. $cc_list = getLineOfAddrs($cc);
  106. $bcc_list = getLineOfAddrs($bcc);
  107. if ($from == "")
  108. $from = "<$from_addr>";
  109. else
  110. $from = $from . " <$from_addr>";
  111. /* Encoding 8-bit characters */
  112. $subject = encodeHeader($subject);
  113. $from = encodeHeader($from);
  114. /* This creates an RFC 822 date */
  115. $date = date("D, j M Y H:i:s ", mktime()) . timezone();
  116. /* Create a message-id */
  117. $message_id = "<" . $REMOTE_PORT . "." . $REMOTE_ADDR . ".";
  118. $message_id .= time() . "@" . $SERVER_NAME .">";
  119. /* Make an RFC822 Received: line */
  120. $header = "Received: from $REMOTE_ADDR by $SERVER_NAME with HTTP; ";
  121. $header .= "$date\r\n";
  122. /* Insert the rest of the header fields */
  123. $header .= "Message-ID: $message_id\r\n";
  124. $header .= "Date: $date\r\n";
  125. $header .= "Subject: $subject\r\n";
  126. $header .= "From: $from\r\n";
  127. $header .= "To: $to_list \r\n"; // Who it's TO
  128. if ($cc_list) {
  129. $header .= "Cc: $cc_list\r\n"; // Who the CCs are
  130. }
  131. if ($reply_to != "")
  132. $header .= "Reply-To: $reply_to\r\n";
  133. if ($useSendmail) {
  134. if ($bcc_list) {
  135. // BCCs is removed from header by sendmail
  136. $header .= "Bcc: $bcc_list\r\n";
  137. }
  138. }
  139. $header .= "X-Mailer: SquirrelMail (version $version)\r\n"; // Identify SquirrelMail
  140. // Do the MIME-stuff
  141. $header .= "MIME-Version: 1.0\r\n";
  142. if (isMultipart()) {
  143. $header .= "Content-Type: multipart/mixed; boundary=\"";
  144. $header .= mimeBoundary();
  145. $header .= "\"\r\n";
  146. } else {
  147. if ($default_charset != "")
  148. $header .= "Content-Type: text/plain; charset=$default_charset\r\n";
  149. else
  150. $header .= "Content-Type: text/plain;\r\n";
  151. $header .= "Content-Transfer-Encoding: 8bit\r\n";
  152. }
  153. $header .= "\r\n"; // One blank line to separate header and body
  154. $headerlength = strlen($header);
  155. }
  156. // Write the header
  157. fputs ($fp, $header);
  158. return $headerlength;
  159. }
  160. // Send the body
  161. function writeBody ($fp, $passedBody) {
  162. global $default_charset;
  163. $attachmentlength = 0;
  164. if (isMultipart()) {
  165. $body = "--".mimeBoundary()."\r\n";
  166. if ($default_charset != "")
  167. $body .= "Content-Type: text/plain; charset=$default_charset\r\n";
  168. else
  169. $body .= "Content-Type: text/plain\r\n";
  170. $body .= "Content-Transfer-Encoding: 8bit\r\n\r\n";
  171. $body .= stripslashes($passedBody) . "\r\n";
  172. fputs ($fp, $body);
  173. $attachmentlength = attachFiles($fp);
  174. $postbody .= "\r\n--".mimeBoundary()."--\r\n\r\n";
  175. fputs ($fp, $postbody);
  176. } else {
  177. $body = stripslashes($passedBody) . "\r\n";
  178. fputs ($fp, $body);
  179. $postbody = "\r\n";
  180. fputs ($fp, $postbody);
  181. }
  182. return (strlen($body) + strlen($postbody) + $attachmentlength);
  183. }
  184. // Send mail using the sendmail command
  185. function sendSendmail($t, $c, $b, $subject, $body) {
  186. global $sendmail_path, $username, $domain;
  187. // open pipe to sendmail
  188. $fp = popen (escapeshellcmd("$sendmail_path -t -f$username@$domain"), "w");
  189. $headerlength = write822Header ($fp, $t, $c, $b, $subject);
  190. $bodylength = writeBody($fp, $body);
  191. pclose($fp);
  192. return ($headerlength + $bodylenght);
  193. }
  194. function smtpReadData($smtpConnection) {
  195. $read = fgets($smtpConnection, 1024);
  196. $counter = 0;
  197. while ($read) {
  198. echo $read . "<BR>";
  199. $data[$counter] = $read;
  200. $read = fgets($smtpConnection, 1024);
  201. $counter++;
  202. }
  203. }
  204. function sendSMTP($t, $c, $b, $subject, $body) {
  205. global $username, $domain, $version, $smtpServerAddress, $smtpPort,
  206. $data_dir, $color;
  207. $to = parseAddrs($t);
  208. $cc = parseAddrs($c);
  209. $bcc = parseAddrs($b);
  210. $from_addr = getPref($data_dir, $username, "email_address");
  211. if ($from_addr == "")
  212. $from_addr = "$username@$domain";
  213. $smtpConnection = fsockopen($smtpServerAddress, $smtpPort, $errorNumber, $errorString);
  214. if (!$smtpConnection) {
  215. echo "Error connecting to SMTP Server.<br>";
  216. echo "$errorNumber : $errorString<br>";
  217. exit;
  218. }
  219. $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
  220. errorCheck($tmp);
  221. $to_list = getLineOfAddrs($to);
  222. $cc_list = getLineOfAddrs($cc);
  223. /** Lets introduce ourselves */
  224. fputs($smtpConnection, "HELO $domain\r\n");
  225. $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
  226. errorCheck($tmp);
  227. /** Ok, who is sending the message? */
  228. fputs($smtpConnection, "MAIL FROM:<$from_addr>\r\n");
  229. $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
  230. errorCheck($tmp);
  231. /** send who the recipients are */
  232. for ($i = 0; $i < count($to); $i++) {
  233. fputs($smtpConnection, "RCPT TO:<$to[$i]>\r\n");
  234. $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
  235. errorCheck($tmp);
  236. }
  237. for ($i = 0; $i < count($cc); $i++) {
  238. fputs($smtpConnection, "RCPT TO:<$cc[$i]>\r\n");
  239. $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
  240. errorCheck($tmp);
  241. }
  242. for ($i = 0; $i < count($bcc); $i++) {
  243. fputs($smtpConnection, "RCPT TO:<$bcc[$i]>\r\n");
  244. $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
  245. errorCheck($tmp);
  246. }
  247. /** Lets start sending the actual message */
  248. fputs($smtpConnection, "DATA\r\n");
  249. $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
  250. errorCheck($tmp);
  251. // Send the message
  252. $headerlength = write822Header ($smtpConnection, $t, $c, $b, $subject);
  253. $bodylength = writeBody($smtpConnection, $body);
  254. fputs($smtpConnection, ".\r\n"); // end the DATA part
  255. $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
  256. $num = errorCheck($tmp);
  257. if ($num != 250) {
  258. echo "ERROR<BR>Message not sent!<BR>Reason given: $tmp<BR></BODY></HTML>";
  259. }
  260. fputs($smtpConnection, "QUIT\r\n"); // log off
  261. fclose($smtpConnection);
  262. return ($headerlength + $bodylength);
  263. }
  264. function errorCheck($line) {
  265. global $color;
  266. // Status: 0 = fatal
  267. // 5 = ok
  268. $err_num = substr($line, 0, strpos($line, " "));
  269. switch ($err_num) {
  270. case 500: $message = "Syntax error; command not recognized";
  271. $status = 0;
  272. break;
  273. case 501: $message = "Syntax error in parameters or arguments";
  274. $status = 0;
  275. break;
  276. case 502: $message = "Command not implemented";
  277. $status = 0;
  278. break;
  279. case 503: $message = "Bad sequence of commands";
  280. $status = 0;
  281. break;
  282. case 504: $message = "Command parameter not implemented";
  283. $status = 0;
  284. break;
  285. case 211: $message = "System status, or system help reply";
  286. $status = 5;
  287. break;
  288. case 214: $message = "Help message";
  289. $status = 5;
  290. break;
  291. case 220: $message = "Service ready";
  292. $status = 5;
  293. break;
  294. case 221: $message = "Service closing transmission channel";
  295. $status = 5;
  296. break;
  297. case 421: $message = "Service not available, closing chanel";
  298. $status = 0;
  299. break;
  300. case 250: $message = "Requested mail action okay, completed";
  301. $status = 5;
  302. break;
  303. case 251: $message = "User not local; will forward";
  304. $status = 5;
  305. break;
  306. case 450: $message = "Requested mail action not taken: mailbox unavailable";
  307. $status = 0;
  308. break;
  309. case 550: $message = "Requested action not taken: mailbox unavailable";
  310. $status = 0;
  311. break;
  312. case 451: $message = "Requested action aborted: error in processing";
  313. $status = 0;
  314. break;
  315. case 551: $message = "User not local; please try forwarding";
  316. $status = 0;
  317. break;
  318. case 452: $message = "Requested action not taken: insufficient system storage";
  319. $status = 0;
  320. break;
  321. case 552: $message = "Requested mail action aborted: exceeding storage allocation";
  322. $status = 0;
  323. break;
  324. case 553: $message = "Requested action not taken: mailbox name not allowed";
  325. $status = 0;
  326. break;
  327. case 354: $message = "Start mail input; end with .";
  328. $status = 5;
  329. break;
  330. case 554: $message = "Transaction failed";
  331. $status = 0;
  332. break;
  333. default: $message = "Unknown response: $line";
  334. $status = 0;
  335. $error_num = "001";
  336. break;
  337. }
  338. if ($status == 0) {
  339. echo "<HTML><BODY BGCOLOR=#ffffff>";
  340. echo "<TT>";
  341. echo "<BR><B>ERROR</B><BR><BR>";
  342. echo "&nbsp;&nbsp;&nbsp;<B>Error Number: </B>$err_num<BR>";
  343. echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<B>Reason: </B>$message<BR>";
  344. echo "<B>Server Response: </B>$line<BR>";
  345. echo "<BR>MAIL NOT SENT";
  346. echo "</TT></BODY></HTML>";
  347. exit;
  348. }
  349. return $err_num;
  350. }
  351. function sendMessage($t, $c, $b, $subject, $body) {
  352. global $useSendmail, $msg_id, $is_reply;
  353. global $data_dir, $username, $domain, $key, $version, $sent_folder, $imapServerAddress, $imapPort;
  354. if ($useSendmail==true) {
  355. $length = sendSendmail($t, $c, $b, $subject, $body);
  356. } else {
  357. $length = sendSMTP($t, $c, $b, $subject, $body);
  358. }
  359. $imap_stream = sqimap_login($username, $key, $imapServerAddress, $imapPort, 1);
  360. if (sqimap_mailbox_exists ($imap_stream, $sent_folder)) {
  361. sqimap_append ($imap_stream, $sent_folder, $length);
  362. write822Header ($imap_stream, $t, $c, $b, $subject);
  363. writeBody ($imap_stream, $body);
  364. sqimap_append_done ($imap_stream);
  365. }
  366. // Delete the files uploaded for attaching (if any).
  367. deleteAttachments();
  368. }
  369. ?>