smtp.php 12 KB

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