smtp.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. <?
  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. while (list($localname, $remotename) = each($attachments)) {
  20. // This is to make sure noone is giving a filename in another
  21. // directory
  22. $localname = ereg_replace ("\\/", "", $localname);
  23. $fileinfo = fopen ($attachment_dir.$localname.".info", "r");
  24. $filetype = fgets ($fileinfo, 8192);
  25. fclose ($fileinfo);
  26. $filetype = trim ($filetype);
  27. if ($filetype=="")
  28. $filetype = "application/octet-stream";
  29. fputs ($fp, "--".mimeBoundary()."\r\n");
  30. fputs ($fp, "Content-Type: $filetype\n");
  31. fputs ($fp, "Content-Disposition: attachment; filename=\"$remotename\"\r\n");
  32. fputs ($fp, "Content-Transfer-Encoding: base64\r\n\r\n");
  33. $file = fopen ($attachment_dir.$localname, "r");
  34. while ($tmp = fread($file, 57))
  35. fputs ($fp, chunk_split(base64_encode($tmp)));
  36. fclose ($file);
  37. unlink ($attachment_dir.$localname);
  38. unlink ($attachment_dir.$localname.".info");
  39. }
  40. }
  41. // Return a nice MIME-boundary
  42. function mimeBoundary () {
  43. global $mimeBoundaryString, $version, $REMOTE_ADDR, $SERVER_NAME,
  44. $REMOTE_PORT;
  45. if ($mimeBoundaryString == "") {
  46. $temp = "SquirrelMail".$version.$REMOTE_ADDR.$SERVER_NAME.
  47. $REMOTE_PORT;
  48. $mimeBoundaryString = "=-_+".substr(md5($temp),1,20);
  49. }
  50. return $mimeBoundaryString;
  51. }
  52. /* Time offset for correct timezone */
  53. function timezone () {
  54. $diff_second = date("Z");
  55. if ($diff_second > 0)
  56. $sign = "+";
  57. else
  58. $sign = "-";
  59. $diff_second = abs($diff_second);
  60. $diff_hour = floor ($diff_second / 3600);
  61. $diff_minute = floor (($diff_second-3600*$diff_hour) / 60);
  62. $zonename = "(".strftime("%Z").")";
  63. $result = sprintf ("%s%02d%02d %s", $sign, $diff_hour, $diff_minute, $zonename);
  64. return ($result);
  65. }
  66. /* Print all the needed RFC822 headers */
  67. function write822Header ($fp, $t, $c, $b, $subject) {
  68. global $REMOTE_ADDR, $SERVER_NAME;
  69. global $data_dir, $username, $domain, $version, $useSendmail;
  70. $to = parseAddrs($t);
  71. $cc = parseAddrs($c);
  72. $bcc = parseAddrs($b);
  73. $reply_to = getPref($data_dir, $username, "reply_to");
  74. $from = getPref($data_dir, $username, "full_name");
  75. $from_addr = getPref($data_dir, $username, "email_address");
  76. if ($from_addr == "")
  77. $from_addr = "$username@$domain";
  78. $to_list = getLineOfAddrs($to);
  79. $cc_list = getLineOfAddrs($cc);
  80. $bcc_list = getLineOfAddrs($bcc);
  81. if ($from == "")
  82. $from = "<$from_addr>";
  83. else
  84. $from = $from . " <$from_addr>";
  85. /* This creates an RFC 822 date showing GMT */
  86. $date = date("D, j M Y H:i:s ", mktime()) . timezone();
  87. /* Make an RFC822 Received: line */
  88. fputs ($fp, "Received: from $REMOTE_ADDR by $SERVER_NAME with HTTP; ");
  89. fputs ($fp, "$date\n");
  90. /* The rest of the header */
  91. fputs ($fp, "Date: $date\r\n");
  92. fputs ($fp, "Subject: $subject\r\n"); // Subject
  93. fputs ($fp, "From: $from\r\n"); // Subject
  94. fputs ($fp, "To: $to_list\r\n"); // Who it's TO
  95. if ($cc_list) {
  96. fputs($fp, "Cc: $cc_list\r\n"); // Who the CCs are
  97. }
  98. if ($reply_to != "")
  99. fputs($fp, "Reply-To: $reply_to\r\n");
  100. if ($useSendmail) {
  101. if ($bcc_list) {
  102. // BCCs is removed from header by sendmail
  103. fputs($fp, "Bcc: $bcc_list\r\n");
  104. }
  105. }
  106. fputs($fp, "X-Mailer: SquirrelMail (version $version)\r\n"); // Identify SquirrelMail
  107. // Do the MIME-stuff
  108. fputs($fp, "MIME-Version: 1.0\n");
  109. if (isMultipart()) {
  110. fputs ($fp, "Content-Type: multipart/mixed; boundary=\"");
  111. fputs ($fp, mimeBoundary());
  112. fputs ($fp, "\"\r\n");
  113. } else {
  114. fputs($fp, "Content-Type: text/plain; charset=ISO-8859-1\r\n");
  115. fputs($fp, "Content-Transfer-Encoding: 8bit\r\n");
  116. }
  117. fputs ($fp, "\r\n");
  118. }
  119. // Send the body
  120. function writeBody ($fp, $body) {
  121. if (isMultipart()) {
  122. fputs ($fp, "--".mimeBoundary()."\r\n");
  123. fputs ($fp, "Content-Type: text/plain; charset=ISO-8859-1\r\n");
  124. fputs ($fp, "Content-Transfer-Encoding: 8bit\r\n\r\n");
  125. fputs ($fp, stripslashes($body) . "\r\n");
  126. attachFiles($fp);
  127. fputs ($fp, "\r\n--".mimeBoundary()."--\r\n");
  128. } else {
  129. fputs ($fp, stripslashes($body) . "\r\n");
  130. }
  131. fputs ($fp, "\r\n");
  132. }
  133. // Send mail using the sendmail command
  134. function sendSendmail($t, $c, $b, $subject, $body) {
  135. global $sendmail_path, $username, $domain;
  136. // open pipe to sendmail
  137. $fp = popen (escapeshellcmd("$sendmail_path -odb -oi -t -f$username@$domain"), "w");
  138. write822Header ($fp, $t, $c, $b, $subject);
  139. writeBody($fp, $body);
  140. pclose($fp);
  141. }
  142. function smtpReadData($smtpConnection) {
  143. $read = fgets($smtpConnection, 1024);
  144. $counter = 0;
  145. while ($read) {
  146. echo $read . "<BR>";
  147. $data[$counter] = $read;
  148. $read = fgets($smtpConnection, 1024);
  149. $counter++;
  150. }
  151. }
  152. function sendSMTP($t, $c, $b, $subject, $body) {
  153. global $username, $domain, $version, $smtpServerAddress, $smtpPort,
  154. $data_dir;
  155. $to = parseAddrs($t);
  156. $cc = parseAddrs($c);
  157. $bcc = parseAddrs($b);
  158. $from_addr = getPref($data_dir, $username, "email_address");
  159. if ($from_addr == "")
  160. $from_addr = "$username@$domain";
  161. $smtpConnection = fsockopen($smtpServerAddress, $smtpPort, $errorNumber, $errorString);
  162. if (!$smtpConnection) {
  163. echo "Error connecting to SMTP Server.<br>";
  164. echo "$errorNumber : $errorString<br>";
  165. exit;
  166. }
  167. $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
  168. errorCheck($tmp);
  169. $to_list = getLineOfAddrs($to);
  170. $cc_list = getLineOfAddrs($cc);
  171. /** Lets introduce ourselves */
  172. fputs($smtpConnection, "HELO $domain\r\n");
  173. $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
  174. errorCheck($tmp);
  175. /** Ok, who is sending the message? */
  176. fputs($smtpConnection, "MAIL FROM:<$from_addr>\r\n");
  177. $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
  178. errorCheck($tmp);
  179. /** send who the recipients are */
  180. for ($i = 0; $i < count($to); $i++) {
  181. fputs($smtpConnection, "RCPT TO:<$to[$i]>\r\n");
  182. $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
  183. errorCheck($tmp);
  184. }
  185. for ($i = 0; $i < count($cc); $i++) {
  186. fputs($smtpConnection, "RCPT TO:<$cc[$i]>\r\n");
  187. $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
  188. errorCheck($tmp);
  189. }
  190. for ($i = 0; $i < count($bcc); $i++) {
  191. fputs($smtpConnection, "RCPT TO:<$bcc[$i]>\r\n");
  192. $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
  193. errorCheck($tmp);
  194. }
  195. /** Lets start sending the actual message */
  196. fputs($smtpConnection, "DATA\r\n");
  197. $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
  198. errorCheck($tmp);
  199. write822Header ($smtpConnection, $t, $c, $b, $subject);
  200. writeBody($smtpConnection, $body); // send the body of the message
  201. fputs($smtpConnection, ".\r\n"); // end the DATA part
  202. $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
  203. $num = errorCheck($tmp);
  204. if ($num != 250) {
  205. echo "<HTML><BODY BGCOLOR=FFFFFF>ERROR<BR>Message not sent!<BR>Reason given: $tmp<BR></BODY></HTML>";
  206. }
  207. fputs($smtpConnection, "QUIT\r\n"); // log off
  208. fclose($smtpConnection);
  209. }
  210. function errorCheck($line) {
  211. // Status: 0 = fatal
  212. // 5 = ok
  213. $err_num = substr($line, 0, strpos($line, " "));
  214. switch ($err_num) {
  215. case 500: $message = "Syntax error; command not recognized";
  216. $status = 0;
  217. break;
  218. case 501: $message = "Syntax error in parameters or arguments";
  219. $status = 0;
  220. break;
  221. case 502: $message = "Command not implemented";
  222. $status = 0;
  223. break;
  224. case 503: $message = "Bad sequence of commands";
  225. $status = 0;
  226. break;
  227. case 504: $message = "Command parameter not implemented";
  228. $status = 0;
  229. break;
  230. case 211: $message = "System status, or system help reply";
  231. $status = 5;
  232. break;
  233. case 214: $message = "Help message";
  234. $status = 5;
  235. break;
  236. case 220: $message = "Service ready";
  237. $status = 5;
  238. break;
  239. case 221: $message = "Service closing transmission channel";
  240. $status = 5;
  241. break;
  242. case 421: $message = "Service not available, closing chanel";
  243. $status = 0;
  244. break;
  245. case 250: $message = "Requested mail action okay, completed";
  246. $status = 5;
  247. break;
  248. case 251: $message = "User not local; will forward";
  249. $status = 5;
  250. break;
  251. case 450: $message = "Requested mail action not taken: mailbox unavailable";
  252. $status = 0;
  253. break;
  254. case 550: $message = "Requested action not taken: mailbox unavailable";
  255. $status = 0;
  256. break;
  257. case 451: $message = "Requested action aborted: error in processing";
  258. $status = 0;
  259. break;
  260. case 551: $message = "User not local; please try forwarding";
  261. $status = 0;
  262. break;
  263. case 452: $message = "Requested action not taken: insufficient system storage";
  264. $status = 0;
  265. break;
  266. case 552: $message = "Requested mail action aborted: exceeding storage allocation";
  267. $status = 0;
  268. break;
  269. case 553: $message = "Requested action not taken: mailbox name not allowed";
  270. $status = 0;
  271. break;
  272. case 354: $message = "Start mail input; end with .";
  273. $status = 5;
  274. break;
  275. case 554: $message = "Transaction failed";
  276. $status = 0;
  277. break;
  278. default: $message = "Unknown response: $line";
  279. $status = 0;
  280. $error_num = "001";
  281. break;
  282. }
  283. if ($status == 0) {
  284. echo "<HTML><BODY BGCOLOR=FFFFFF>";
  285. echo "<TT>";
  286. echo "<BR><B>ERROR</B><BR><BR>";
  287. echo "&nbsp;&nbsp;&nbsp;<B>Error Number: </B>$err_num<BR>";
  288. echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<B>Reason: </B>$message<BR>";
  289. echo "<B>Server Response: </B>$line<BR>";
  290. echo "<BR>MAIL NOT SENT";
  291. echo "</TT></BODY></HTML>";
  292. exit;
  293. }
  294. return $err_num;
  295. }
  296. function sendMessage($t, $c, $b, $subject, $body) {
  297. global $useSendmail;
  298. if ($useSendmail==true) {
  299. sendSendmail($t, $c, $b, $subject, $body);
  300. } else {
  301. sendSMTP($t, $c, $b, $subject, $body);
  302. }
  303. }
  304. ?>