smtp.php 11 KB

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