smtp.php 11 KB

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