smtp.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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()."\n");
  30. fputs ($fp, "Content-Type: $filetype\n");
  31. fputs ($fp, "Content-Disposition: attachment; filename=\"$remotename\"\n");
  32. fputs ($fp, "Content-Transfer-Encoding: base64\n\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\n");
  92. fputs ($fp, "Subject: $subject\n"); // Subject
  93. fputs ($fp, "From: $from\n"); // Subject
  94. fputs ($fp, "To: $to_list\n"); // Who it's TO
  95. if ($cc_list) {
  96. fputs($fp, "Cc: $cc_list\n"); // Who the CCs are
  97. }
  98. if ($reply_to != "")
  99. fputs($fp, "Reply-To: $reply_to\n");
  100. if ($useSendmail) {
  101. if ($bcc_list) {
  102. // BCCs is removed from header by sendmail
  103. fputs($fp, "Bcc: $bcc_list\n");
  104. }
  105. }
  106. fputs($fp, "X-Mailer: SquirrelMail (version $version)\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, "\"\n");
  113. } else {
  114. fputs($fp, "Content-Type: text/plain; charset=ISO-8859-1\n");
  115. fputs($fp, "Content-Transfer-Encoding: 8bit\n");
  116. }
  117. }
  118. // Send the body
  119. function writeBody ($fp, $body) {
  120. if (isMultipart()) {
  121. fputs ($fp, "--".mimeBoundary()."\n");
  122. fputs ($fp, "Content-Type: text/plain; charset=ISO-8859-1\n");
  123. fputs ($fp, "Content-Transfer-Encoding: 8bit\n\n");
  124. fputs ($fp, stripslashes($body) . "\n");
  125. attachFiles($fp);
  126. fputs ($fp, "\n--".mimeBoundary()."--\n");
  127. } else {
  128. fputs ($fp, stripslashes($body) . "\n");
  129. }
  130. }
  131. // Send mail using the sendmail command
  132. function sendSendmail($t, $c, $b, $subject, $body) {
  133. global $sendmail_path, $username, $domain;
  134. // open pipe to sendmail
  135. $fp = popen (escapeshellcmd("$sendmail_path -odb -oi -t -f$username@$domain"), "w");
  136. write822Header ($fp, $t, $c, $b, $subject);
  137. writeBody($fp, $body);
  138. pclose($fp);
  139. }
  140. function smtpReadData($smtpConnection) {
  141. $read = fgets($smtpConnection, 1024);
  142. $counter = 0;
  143. while ($read) {
  144. echo $read . "<BR>";
  145. $data[$counter] = $read;
  146. $read = fgets($smtpConnection, 1024);
  147. $counter++;
  148. }
  149. }
  150. function sendSMTP($t, $c, $b, $subject, $body) {
  151. global $username, $domain, $version, $smtpServerAddress, $smtpPort,
  152. $data_dir;
  153. $to = parseAddrs($t);
  154. $cc = parseAddrs($c);
  155. $bcc = parseAddrs($b);
  156. $from_addr = getPref($data_dir, $username, "email_address");
  157. if ($from_addr == "")
  158. $from_addr = "$username@$domain";
  159. $smtpConnection = fsockopen($smtpServerAddress, $smtpPort, $errorNumber, $errorString);
  160. if (!$smtpConnection) {
  161. echo "Error connecting to SMTP Server.<br>";
  162. echo "$errorNumber : $errorString<br>";
  163. exit;
  164. }
  165. $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
  166. errorCheck($tmp);
  167. $to_list = getLineOfAddrs($to);
  168. $cc_list = getLineOfAddrs($cc);
  169. /** Lets introduce ourselves */
  170. fputs($smtpConnection, "HELO $domain\n");
  171. $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
  172. errorCheck($tmp);
  173. /** Ok, who is sending the message? */
  174. fputs($smtpConnection, "MAIL FROM:<$from_addr>\n");
  175. $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
  176. errorCheck($tmp);
  177. /** send who the recipients are */
  178. for ($i = 0; $i < count($to); $i++) {
  179. fputs($smtpConnection, "RCPT TO:<$to[$i]>\n");
  180. $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
  181. errorCheck($tmp);
  182. }
  183. for ($i = 0; $i < count($cc); $i++) {
  184. fputs($smtpConnection, "RCPT TO:<$cc[$i]>\n");
  185. $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
  186. errorCheck($tmp);
  187. }
  188. for ($i = 0; $i < count($bcc); $i++) {
  189. fputs($smtpConnection, "RCPT TO:<$bcc[$i]>\n");
  190. $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
  191. errorCheck($tmp);
  192. }
  193. /** Lets start sending the actual message */
  194. fputs($smtpConnection, "DATA\n");
  195. $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
  196. errorCheck($tmp);
  197. write822Header ($smtpConnection, $t, $c, $b, $subject);
  198. writeBody($smtpConnection, $body); // send the body of the message
  199. fputs($smtpConnection, ".\n"); // end the DATA part
  200. $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
  201. $num = errorCheck($tmp);
  202. if ($num != 250) {
  203. echo "<HTML><BODY BGCOLOR=FFFFFF>ERROR<BR>Message not sent!<BR>Reason given: $tmp<BR></BODY></HTML>";
  204. }
  205. fputs($smtpConnection, "QUIT\n"); // log off
  206. fclose($smtpConnection);
  207. }
  208. function errorCheck($line) {
  209. // Status: 0 = fatal
  210. // 5 = ok
  211. $err_num = substr($line, 0, strpos($line, " "));
  212. switch ($err_num) {
  213. case 500: $message = "Syntax error; command not recognized";
  214. $status = 0;
  215. break;
  216. case 501: $message = "Syntax error in parameters or arguments";
  217. $status = 0;
  218. break;
  219. case 502: $message = "Command not implemented";
  220. $status = 0;
  221. break;
  222. case 503: $message = "Bad sequence of commands";
  223. $status = 0;
  224. break;
  225. case 504: $message = "Command parameter not implemented";
  226. $status = 0;
  227. break;
  228. case 211: $message = "System status, or system help reply";
  229. $status = 5;
  230. break;
  231. case 214: $message = "Help message";
  232. $status = 5;
  233. break;
  234. case 220: $message = "Service ready";
  235. $status = 5;
  236. break;
  237. case 221: $message = "Service closing transmission channel";
  238. $status = 5;
  239. break;
  240. case 421: $message = "Service not available, closing chanel";
  241. $status = 0;
  242. break;
  243. case 250: $message = "Requested mail action okay, completed";
  244. $status = 5;
  245. break;
  246. case 251: $message = "User not local; will forward";
  247. $status = 5;
  248. break;
  249. case 450: $message = "Requested mail action not taken: mailbox unavailable";
  250. $status = 0;
  251. break;
  252. case 550: $message = "Requested action not taken: mailbox unavailable";
  253. $status = 0;
  254. break;
  255. case 451: $message = "Requested action aborted: error in processing";
  256. $status = 0;
  257. break;
  258. case 551: $message = "User not local; please try forwarding";
  259. $status = 0;
  260. break;
  261. case 452: $message = "Requested action not taken: insufficient system storage";
  262. $status = 0;
  263. break;
  264. case 552: $message = "Requested mail action aborted: exceeding storage allocation";
  265. $status = 0;
  266. break;
  267. case 553: $message = "Requested action not taken: mailbox name not allowed";
  268. $status = 0;
  269. break;
  270. case 354: $message = "Start mail input; end with .";
  271. $status = 5;
  272. break;
  273. case 554: $message = "Transaction failed";
  274. $status = 0;
  275. break;
  276. default: $message = "Unknown response: $line";
  277. $status = 0;
  278. $error_num = "001";
  279. break;
  280. }
  281. if ($status == 0) {
  282. echo "<HTML><BODY BGCOLOR=FFFFFF>";
  283. echo "<TT>";
  284. echo "<BR><B>ERROR</B><BR><BR>";
  285. echo "&nbsp;&nbsp;&nbsp;<B>Error Number: </B>$err_num<BR>";
  286. echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<B>Reason: </B>$message<BR>";
  287. echo "<B>Server Response: </B>$line<BR>";
  288. echo "<BR>MAIL NOT SENT";
  289. echo "</TT></BODY></HTML>";
  290. exit;
  291. }
  292. return $err_num;
  293. }
  294. function sendMessage($t, $c, $b, $subject, $body) {
  295. global $useSendmail;
  296. if ($useSendmail==true) {
  297. sendSendmail($t, $c, $b, $subject, $body);
  298. } else {
  299. sendSMTP($t, $c, $b, $subject, $body);
  300. }
  301. }
  302. ?>