smtp.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?
  2. /** smtp.php
  3. **
  4. ** This contains all the functions needed to send messages through
  5. ** an smtp server.
  6. **/
  7. function smtpReadData($smtpConnection) {
  8. $read = fgets($smtpConnection, 1024);
  9. $counter = 0;
  10. while ($read) {
  11. echo $read . "<BR>";
  12. $data[$counter] = $read;
  13. $read = fgets($smtpConnection, 1024);
  14. $counter++;
  15. }
  16. }
  17. function sendMessage($smtpServerAddress, $smtpPort, $username, $domain, $t, $c, $b, $subject, $body, $version) {
  18. include("../config/config.php");
  19. $to = parseAddrs($t);
  20. $cc = parseAddrs($c);
  21. $bcc = parseAddrs($b);
  22. $body = stripslashes($body);
  23. $from_addr = "$username@$domain";
  24. $reply_to = getPref($data_dir, $username, "reply_to");
  25. $from = getPref($data_dir, $username, "full_name");
  26. if ($from == "")
  27. $from = "<$from_addr>";
  28. else
  29. $from = $from . " <$from_addr>";
  30. $smtpConnection = fsockopen($smtpServerAddress, $smtpPort, $errorNumber, $errorString);
  31. if (!$smtpConnection) {
  32. echo "Error connecting to SMTP Server.<br>";
  33. echo "$errorNumber : $errorString<br>";
  34. exit;
  35. }
  36. $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
  37. errorCheck($tmp);
  38. $to_list = getLineOfAddrs($to);
  39. $cc_list = getLineOfAddrs($cc);
  40. /** Lets introduce ourselves */
  41. fputs($smtpConnection, "HELO $domain\n");
  42. $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
  43. errorCheck($tmp);
  44. /** Ok, who is sending the message? */
  45. fputs($smtpConnection, "MAIL FROM:<$from_addr>\n");
  46. $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
  47. errorCheck($tmp);
  48. /** send who the recipients are */
  49. for ($i = 0; $i < count($to); $i++) {
  50. fputs($smtpConnection, "RCPT TO:<$to[$i]>\n");
  51. $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
  52. errorCheck($tmp);
  53. }
  54. for ($i = 0; $i < count($cc); $i++) {
  55. fputs($smtpConnection, "RCPT TO:<$cc[$i]>\n");
  56. $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
  57. errorCheck($tmp);
  58. }
  59. for ($i = 0; $i < count($bcc); $i++) {
  60. fputs($smtpConnection, "RCPT TO:<$bcc[$i]>\n");
  61. $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
  62. errorCheck($tmp);
  63. }
  64. /** Lets start sending the actual message */
  65. fputs($smtpConnection, "DATA\n");
  66. $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
  67. errorCheck($tmp);
  68. fputs($smtpConnection, "Subject: $subject\n"); // Subject
  69. fputs($smtpConnection, "From: $from\n"); // Subject
  70. fputs($smtpConnection, "To: <$to_list>\n"); // Who it's TO
  71. if ($cc_list) {
  72. fputs($smtpConnection, "Cc: <$cc_list>\n"); // Who the CCs are
  73. }
  74. fputs($smtpConnection, "X-Mailer: SquirrelMail (version $version)\n"); // Identify SquirrelMail
  75. fputs($smtpConnection, "MIME-Version: 1.0\n");
  76. fputs($smtpConnection, "Content-Type: text/plain\n");
  77. if ($reply_to != "")
  78. fputs($smtpConnection, "Reply-To: $reply_to\n");
  79. fputs($smtpConnection, "$body\n"); // send the body of the message
  80. fputs($smtpConnection, ".\n"); // end the DATA part
  81. $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
  82. $num = errorCheck($tmp);
  83. if ($num != 250) {
  84. echo "<HTML><BODY BGCOLOR=FFFFFF>ERROR<BR>Message not sent!<BR>Reason given: $tmp<BR></BODY></HTML>";
  85. }
  86. fputs($smtpConnection, "QUIT\n"); // log off
  87. fclose($smtpConnection);
  88. }
  89. function errorCheck($line) {
  90. // Status: 0 = fatal
  91. // 5 = ok
  92. $err_num = substr($line, 0, strpos($line, " "));
  93. switch ($err_num) {
  94. case 500: $message = "Syntax error; command not recognized";
  95. $status = 0;
  96. break;
  97. case 501: $message = "Syntax error in parameters or arguments";
  98. $status = 0;
  99. break;
  100. case 502: $message = "Command not implemented";
  101. $status = 0;
  102. break;
  103. case 503: $message = "Bad sequence of commands";
  104. $status = 0;
  105. break;
  106. case 504: $message = "Command parameter not implemented";
  107. $status = 0;
  108. break;
  109. case 211: $message = "System status, or system help reply";
  110. $status = 5;
  111. break;
  112. case 214: $message = "Help message";
  113. $status = 5;
  114. break;
  115. case 220: $message = "Service ready";
  116. $status = 5;
  117. break;
  118. case 221: $message = "Service closing transmission channel";
  119. $status = 5;
  120. break;
  121. case 421: $message = "Service not available, closing chanel";
  122. $status = 0;
  123. break;
  124. case 250: $message = "Requested mail action okay, completed";
  125. $status = 5;
  126. break;
  127. case 251: $message = "User not local; will forward";
  128. $status = 5;
  129. break;
  130. case 450: $message = "Requested mail action not taken: mailbox unavailable";
  131. $status = 0;
  132. break;
  133. case 550: $message = "Requested action not taken: mailbox unavailable";
  134. $status = 0;
  135. break;
  136. case 451: $message = "Requested action aborted: error in processing";
  137. $status = 0;
  138. break;
  139. case 551: $message = "User not local; please try forwarding";
  140. $status = 0;
  141. break;
  142. case 452: $message = "Requested action not taken: insufficient system storage";
  143. $status = 0;
  144. break;
  145. case 552: $message = "Requested mail action aborted: exceeding storage allocation";
  146. $status = 0;
  147. break;
  148. case 553: $message = "Requested action not taken: mailbox name not allowed";
  149. $status = 0;
  150. break;
  151. case 354: $message = "Start mail input; end with .";
  152. $status = 5;
  153. break;
  154. case 554: $message = "Transaction failed";
  155. $status = 0;
  156. break;
  157. default: $message = "Unknown response: $line";
  158. $status = 0;
  159. $error_num = "001";
  160. break;
  161. }
  162. if ($status == 0) {
  163. echo "<HTML><BODY BGCOLOR=FFFFFF>";
  164. echo "<TT>";
  165. echo "<BR><B>ERROR</B><BR><BR>";
  166. echo "&nbsp;&nbsp;&nbsp;<B>Error Number: </B>$err_num<BR>";
  167. echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<B>Reason: </B>$message<BR>";
  168. echo "<B>Server Response: </B>$line<BR>";
  169. echo "<BR>MAIL NOT SENT";
  170. echo "</TT></BODY></HTML>";
  171. exit;
  172. }
  173. return $err_num;
  174. }
  175. ?>