Deliver_SMTP.class.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. <?php
  2. /**
  3. * Deliver_SMTP.class.php
  4. *
  5. * Copyright (c) 1999-2004 The SquirrelMail Project Team
  6. * Licensed under the GNU GPL. For full terms see the file COPYING.
  7. *
  8. * Delivery backend for the Deliver class.
  9. *
  10. * @version $Id$
  11. * @package squirrelmail
  12. */
  13. /** This of course depends upon Deliver */
  14. require_once(SM_PATH . 'class/deliver/Deliver.class.php');
  15. /**
  16. * Deliver messages using SMTP
  17. * @package squirrelmail
  18. */
  19. class Deliver_SMTP extends Deliver {
  20. function preWriteToStream(&$s) {
  21. if ($s) {
  22. if ($s{0} == '.') $s = '.' . $s;
  23. $s = str_replace("\n.","\n..",$s);
  24. }
  25. }
  26. function initStream($message, $domain, $length=0, $host='', $port='', $user='', $pass='', $authpop=false) {
  27. global $use_smtp_tls,$smtp_auth_mech;
  28. if ($authpop) {
  29. $this->authPop($host, '', $user, $pass);
  30. }
  31. $rfc822_header = $message->rfc822_header;
  32. $from = $rfc822_header->from[0];
  33. $to = $rfc822_header->to;
  34. $cc = $rfc822_header->cc;
  35. $bcc = $rfc822_header->bcc;
  36. $content_type = $rfc822_header->content_type;
  37. // MAIL FROM: <from address> MUST be empty in cae of MDN (RFC2298)
  38. if ($content_type->type0 == 'multipart' &&
  39. $content_type->type1 == 'report' &&
  40. isset($content_type->properties['report-type']) &&
  41. $content_type->properties['report-type']=='disposition-notification') {
  42. $from->host = '';
  43. $from->mailbox = '';
  44. }
  45. if (($use_smtp_tls == true) and (check_php_version(4,3)) and (extension_loaded('openssl'))) {
  46. $stream = fsockopen('tls://' . $host, $port, $errorNumber, $errorString);
  47. } else {
  48. $stream = fsockopen($host, $port, $errorNumber, $errorString);
  49. }
  50. if (!$stream) {
  51. $this->dlv_msg = $errorString;
  52. $this->dlv_ret_nr = $errorNumber;
  53. return(0);
  54. }
  55. $tmp = fgets($stream, 1024);
  56. if ($this->errorCheck($tmp, $stream)) {
  57. return(0);
  58. }
  59. /*
  60. * If $_SERVER['HTTP_HOST'] is set, use that in our HELO to the SMTP
  61. * server. This should fix the DNS issues some people have had
  62. */
  63. if (sqgetGlobalVar('HTTP_HOST', $HTTP_HOST, SQ_SERVER)) { // HTTP_HOST is set
  64. // optionally trim off port number
  65. if($p = strrpos($HTTP_HOST, ':')) {
  66. $HTTP_HOST = substr($HTTP_HOST, 0, $p);
  67. }
  68. $helohost = $HTTP_HOST;
  69. } else { // For some reason, HTTP_HOST is not set - revert to old behavior
  70. $helohost = $domain;
  71. }
  72. /* Lets introduce ourselves */
  73. fputs($stream, "EHLO $helohost\r\n");
  74. $tmp = fgets($stream,1024);
  75. if ($this->errorCheck($tmp,$stream)) {
  76. // fall back to HELO if EHLO is not supported
  77. if ($this->dlv_ret_nr == '500') {
  78. fputs($stream, "HELO $helohost\r\n");
  79. $tmp = fgets($stream,1024);
  80. if ($this->errorCheck($tmp,$stream)) {
  81. return(0);
  82. }
  83. } else {
  84. return(0);
  85. }
  86. }
  87. if (( $smtp_auth_mech == 'cram-md5') or ( $smtp_auth_mech == 'digest-md5' )) {
  88. // Doing some form of non-plain auth
  89. if ($smtp_auth_mech == 'cram-md5') {
  90. fputs($stream, "AUTH CRAM-MD5\r\n");
  91. } elseif ($smtp_auth_mech == 'digest-md5') {
  92. fputs($stream, "AUTH DIGEST-MD5\r\n");
  93. }
  94. $tmp = fgets($stream,1024);
  95. if ($this->errorCheck($tmp,$stream)) {
  96. return(0);
  97. }
  98. // At this point, $tmp should hold "334 <challenge string>"
  99. $chall = substr($tmp,4);
  100. // Depending on mechanism, generate response string
  101. if ($smtp_auth_mech == 'cram-md5') {
  102. $response = cram_md5_response($user,$pass,$chall);
  103. } elseif ($smtp_auth_mech == 'digest-md5') {
  104. $response = digest_md5_response($user,$pass,$chall,'smtp',$host);
  105. }
  106. fputs($stream, $response);
  107. // Let's see what the server had to say about that
  108. $tmp = fgets($stream,1024);
  109. if ($this->errorCheck($tmp,$stream)) {
  110. return(0);
  111. }
  112. // CRAM-MD5 is done at this point. If DIGEST-MD5, there's a bit more to go
  113. if ($smtp_auth_mech == 'digest-md5') {
  114. // $tmp contains rspauth, but I don't store that yet. (No need yet)
  115. fputs($stream,"\r\n");
  116. $tmp = fgets($stream,1024);
  117. if ($this->errorCheck($tmp,$stream)) {
  118. return(0);
  119. }
  120. }
  121. // CRAM-MD5 and DIGEST-MD5 code ends here
  122. } elseif ($smtp_auth_mech == 'none') {
  123. // No auth at all, just send helo and then send the mail
  124. // We already said hi earlier, nothing more is needed.
  125. } elseif ($smtp_auth_mech == 'login') {
  126. // The LOGIN method
  127. fputs($stream, "AUTH LOGIN\r\n");
  128. $tmp = fgets($stream, 1024);
  129. if ($this->errorCheck($tmp, $stream)) {
  130. return(0);
  131. }
  132. fputs($stream, base64_encode ($user) . "\r\n");
  133. $tmp = fgets($stream, 1024);
  134. if ($this->errorCheck($tmp, $stream)) {
  135. return(0);
  136. }
  137. fputs($stream, base64_encode($pass) . "\r\n");
  138. $tmp = fgets($stream, 1024);
  139. if ($this->errorCheck($tmp, $stream)) {
  140. return(0);
  141. }
  142. } elseif ($smtp_auth_mech == "plain") {
  143. /* SASL Plain */
  144. $auth = base64_encode("$user\0$user\0$pass");
  145. $query = "AUTH PLAIN\r\n";
  146. fputs($stream, $query);
  147. $read=fgets($stream, 1024);
  148. if (substr($read,0,3) == '334') { // OK so far..
  149. fputs($stream, "$auth\r\n");
  150. $read = fgets($stream, 1024);
  151. }
  152. $results=explode(" ",$read,3);
  153. $response=$results[1];
  154. $message=$results[2];
  155. } else {
  156. /* Right here, they've reached an unsupported auth mechanism.
  157. This is the ugliest hack I've ever done, but it'll do till I can fix
  158. things up better tomorrow. So tired... */
  159. if ($this->errorCheck("535 Unable to use this auth type",$stream)) {
  160. return(0);
  161. }
  162. }
  163. /* Ok, who is sending the message? */
  164. $fromaddress = ($from->mailbox && $from->host) ?
  165. $from->mailbox.'@'.$from->host : '';
  166. fputs($stream, 'MAIL FROM:<'.$fromaddress.">\r\n");
  167. $tmp = fgets($stream, 1024);
  168. if ($this->errorCheck($tmp, $stream)) {
  169. return(0);
  170. }
  171. /* send who the recipients are */
  172. for ($i = 0, $cnt = count($to); $i < $cnt; $i++) {
  173. if (!$to[$i]->host) $to[$i]->host = $domain;
  174. if ($to[$i]->mailbox) {
  175. fputs($stream, 'RCPT TO:<'.$to[$i]->mailbox.'@'.$to[$i]->host.">\r\n");
  176. $tmp = fgets($stream, 1024);
  177. if ($this->errorCheck($tmp, $stream)) {
  178. return(0);
  179. }
  180. }
  181. }
  182. for ($i = 0, $cnt = count($cc); $i < $cnt; $i++) {
  183. if (!$cc[$i]->host) $cc[$i]->host = $domain;
  184. if ($cc[$i]->mailbox) {
  185. fputs($stream, 'RCPT TO:<'.$cc[$i]->mailbox.'@'.$cc[$i]->host.">\r\n");
  186. $tmp = fgets($stream, 1024);
  187. if ($this->errorCheck($tmp, $stream)) {
  188. return(0);
  189. }
  190. }
  191. }
  192. for ($i = 0, $cnt = count($bcc); $i < $cnt; $i++) {
  193. if (!$bcc[$i]->host) $bcc[$i]->host = $domain;
  194. if ($bcc[$i]->mailbox) {
  195. fputs($stream, 'RCPT TO:<'.$bcc[$i]->mailbox.'@'.$bcc[$i]->host.">\r\n");
  196. $tmp = fgets($stream, 1024);
  197. if ($this->errorCheck($tmp, $stream)) {
  198. return(0);
  199. }
  200. }
  201. }
  202. /* Lets start sending the actual message */
  203. fputs($stream, "DATA\r\n");
  204. $tmp = fgets($stream, 1024);
  205. if ($this->errorCheck($tmp, $stream)) {
  206. return(0);
  207. }
  208. return $stream;
  209. }
  210. function finalizeStream($stream) {
  211. fputs($stream, "\r\n.\r\n"); /* end the DATA part */
  212. $tmp = fgets($stream, 1024);
  213. $this->errorCheck($tmp, $stream);
  214. if ($this->dlv_ret_nr != 250) {
  215. return(0);
  216. }
  217. fputs($stream, "QUIT\r\n"); /* log off */
  218. fclose($stream);
  219. return true;
  220. }
  221. /* check if an SMTP reply is an error and set an error message) */
  222. function errorCheck($line, $smtpConnection) {
  223. $err_num = substr($line, 0, 3);
  224. $this->dlv_ret_nr = $err_num;
  225. $server_msg = substr($line, 4);
  226. while(substr($line, 0, 4) == ($err_num.'-')) {
  227. $line = fgets($smtpConnection, 1024);
  228. $server_msg .= substr($line, 4);
  229. }
  230. if ( ((int) $err_num{0}) < 4) {
  231. return false;
  232. }
  233. switch ($err_num) {
  234. case '421': $message = _("Service not available, closing channel");
  235. break;
  236. case '432': $message = _("A password transition is needed");
  237. break;
  238. case '450': $message = _("Requested mail action not taken: mailbox unavailable");
  239. break;
  240. case '451': $message = _("Requested action aborted: error in processing");
  241. break;
  242. case '452': $message = _("Requested action not taken: insufficient system storage");
  243. break;
  244. case '454': $message = _("Temporary authentication failure");
  245. break;
  246. case '500': $message = _("Syntax error; command not recognized");
  247. break;
  248. case '501': $message = _("Syntax error in parameters or arguments");
  249. break;
  250. case '502': $message = _("Command not implemented");
  251. break;
  252. case '503': $message = _("Bad sequence of commands");
  253. break;
  254. case '504': $message = _("Command parameter not implemented");
  255. break;
  256. case '530': $message = _("Authentication required");
  257. break;
  258. case '534': $message = _("Authentication mechanism is too weak");
  259. break;
  260. case '535': $message = _("Authentication failed");
  261. break;
  262. case '538': $message = _("Encryption required for requested authentication mechanism");
  263. break;
  264. case '550': $message = _("Requested action not taken: mailbox unavailable");
  265. break;
  266. case '551': $message = _("User not local; please try forwarding");
  267. break;
  268. case '552': $message = _("Requested mail action aborted: exceeding storage allocation");
  269. break;
  270. case '553': $message = _("Requested action not taken: mailbox name not allowed");
  271. break;
  272. case '554': $message = _("Transaction failed");
  273. break;
  274. default: $message = _("Unknown response");
  275. break;
  276. }
  277. $this->dlv_msg = $message;
  278. $this->dlv_server_msg = nl2br(htmlspecialchars($server_msg));
  279. return true;
  280. }
  281. function authPop($pop_server='', $pop_port='', $user, $pass) {
  282. if (!$pop_port) {
  283. $pop_port = 110;
  284. }
  285. if (!$pop_server) {
  286. $pop_server = 'localhost';
  287. }
  288. $popConnection = fsockopen($pop_server, $pop_port, $err_no, $err_str);
  289. if (!$popConnection) {
  290. error_log("Error connecting to POP Server ($pop_server:$pop_port)"
  291. . " $err_no : $err_str");
  292. } else {
  293. $tmp = fgets($popConnection, 1024); /* banner */
  294. if (substr($tmp, 0, 3) != '+OK') {
  295. return(0);
  296. }
  297. fputs($popConnection, "USER $user\r\n");
  298. $tmp = fgets($popConnection, 1024);
  299. if (substr($tmp, 0, 3) != '+OK') {
  300. return(0);
  301. }
  302. fputs($popConnection, 'PASS ' . $pass . "\r\n");
  303. $tmp = fgets($popConnection, 1024);
  304. if (substr($tmp, 0, 3) != '+OK') {
  305. return(0);
  306. }
  307. fputs($popConnection, "QUIT\r\n"); /* log off */
  308. fclose($popConnection);
  309. }
  310. }
  311. }
  312. ?>