00001 <?php
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00015
00024 class SMTP
00025 {
00030 var $SMTP_PORT = 25;
00031
00036 var $CRLF = "\r\n";
00037
00042 var $do_debug; # the level of debug to perform
00043
00047 var $smtp_conn; # the socket to the server
00048 var $error; # error if any on the last call
00049 var $helo_rply; # the reply the server sent to us for HELO
00057 function SMTP() {
00058 $this->smtp_conn = 0;
00059 $this->error = null;
00060 $this->helo_rply = null;
00061
00062 $this->do_debug = 0;
00063 }
00064
00065
00066
00067
00068
00082 function Connect($host,$port=0,$tval=30) {
00083 # set the error val to null so there is no confusion
00084 $this->error = null;
00085
00086 # make sure we are __not__ connected
00087 if($this->connected()) {
00088 # ok we are connected! what should we do?
00089 # for now we will just give an error saying we
00090 # are already connected
00091 $this->error =
00092 array("error" => "Already connected to a server");
00093 return false;
00094 }
00095
00096 if(empty($port)) {
00097 $port = $this->SMTP_PORT;
00098 }
00099
00100 #connect to the smtp server
00101 $this->smtp_conn = fsockopen($host, # the host of the server
00102 $port, # the port to use
00103 $errno, # error number if any
00104 $errstr, # error message if any
00105 $tval); # give up after ? secs
00106 # verify we connected properly
00107 if(empty($this->smtp_conn)) {
00108 $this->error = array("error" => "Failed to connect to server",
00109 "errno" => $errno,
00110 "errstr" => $errstr);
00111 if($this->do_debug >= 1) {
00112 echo "SMTP -> ERROR: " . $this->error["error"] .
00113 ": $errstr ($errno)" . $this->CRLF;
00114 }
00115 return false;
00116 }
00117
00118 # sometimes the SMTP server takes a little longer to respond
00119 # so we will give it a longer timeout for the first read
00120
00121 if(substr(PHP_OS, 0, 3) != "WIN")
00122 socket_set_timeout($this->smtp_conn, $tval, 0);
00123
00124 # get any announcement stuff
00125 $announce = $this->get_lines();
00126
00127 # set the timeout of any socket functions at 1/10 of a second
00128
00129
00130
00131 if($this->do_debug >= 2) {
00132 echo "SMTP -> FROM SERVER:" . $this->CRLF . $announce;
00133 }
00134
00135 return true;
00136 }
00137
00144 function Authenticate($username, $password) {
00145
00146 fputs($this->smtp_conn,"AUTH LOGIN" . $this->CRLF);
00147
00148 $rply = $this->get_lines();
00149 $code = substr($rply,0,3);
00150
00151 if($code != 334) {
00152 $this->error =
00153 array("error" => "AUTH not accepted from server",
00154 "smtp_code" => $code,
00155 "smtp_msg" => substr($rply,4));
00156 if($this->do_debug >= 1) {
00157 echo "SMTP -> ERROR: " . $this->error["error"] .
00158 ": " . $rply . $this->CRLF;
00159 }
00160 return false;
00161 }
00162
00163
00164 fputs($this->smtp_conn, base64_encode($username) . $this->CRLF);
00165
00166 $rply = $this->get_lines();
00167 $code = substr($rply,0,3);
00168
00169 if($code != 334) {
00170 $this->error =
00171 array("error" => "Username not accepted from server",
00172 "smtp_code" => $code,
00173 "smtp_msg" => substr($rply,4));
00174 if($this->do_debug >= 1) {
00175 echo "SMTP -> ERROR: " . $this->error["error"] .
00176 ": " . $rply . $this->CRLF;
00177 }
00178 return false;
00179 }
00180
00181
00182 fputs($this->smtp_conn, base64_encode($password) . $this->CRLF);
00183
00184 $rply = $this->get_lines();
00185 $code = substr($rply,0,3);
00186
00187 if($code != 235) {
00188 $this->error =
00189 array("error" => "Password not accepted from server",
00190 "smtp_code" => $code,
00191 "smtp_msg" => substr($rply,4));
00192 if($this->do_debug >= 1) {
00193 echo "SMTP -> ERROR: " . $this->error["error"] .
00194 ": " . $rply . $this->CRLF;
00195 }
00196 return false;
00197 }
00198
00199 return true;
00200 }
00201
00207 function Connected() {
00208 if(!empty($this->smtp_conn)) {
00209 $sock_status = socket_get_status($this->smtp_conn);
00210 if($sock_status["eof"]) {
00211 # hmm this is an odd situation... the socket is
00212 # valid but we aren't connected anymore
00213 if($this->do_debug >= 1) {
00214 echo "SMTP -> NOTICE:" . $this->CRLF .
00215 "EOF caught while checking if connected";
00216 }
00217 $this->Close();
00218 return false;
00219 }
00220 return true; # everything looks good
00221 }
00222 return false;
00223 }
00224
00232 function Close() {
00233 $this->error = null; # so there is no confusion
00234 $this->helo_rply = null;
00235 if(!empty($this->smtp_conn)) {
00236 # close the connection and cleanup
00237 fclose($this->smtp_conn);
00238 $this->smtp_conn = 0;
00239 }
00240 }
00241
00242
00243
00244
00245
00246
00266 function Data($msg_data) {
00267 $this->error = null; # so no confusion is caused
00268
00269 if(!$this->connected()) {
00270 $this->error = array(
00271 "error" => "Called Data() without being connected");
00272 return false;
00273 }
00274
00275 fputs($this->smtp_conn,"DATA" . $this->CRLF);
00276
00277 $rply = $this->get_lines();
00278 $code = substr($rply,0,3);
00279
00280 if($this->do_debug >= 2) {
00281 echo "SMTP -> FROM SERVER:" . $this->CRLF . $rply;
00282 }
00283
00284 if($code != 354) {
00285 $this->error =
00286 array("error" => "DATA command not accepted from server",
00287 "smtp_code" => $code,
00288 "smtp_msg" => substr($rply,4));
00289 if($this->do_debug >= 1) {
00290 echo "SMTP -> ERROR: " . $this->error["error"] .
00291 ": " . $rply . $this->CRLF;
00292 }
00293 return false;
00294 }
00295
00296 # the server is ready to accept data!
00297 # according to rfc 821 we should not send more than 1000
00298 # including the CRLF
00299 # characters on a single line so we will break the data up
00300 # into lines by \r and/or \n then if needed we will break
00301 # each of those into smaller lines to fit within the limit.
00302 # in addition we will be looking for lines that start with
00303 # a period '.' and append and additional period '.' to that
00304 # line. NOTE: this does not count towards are limit.
00305
00306 # normalize the line breaks so we know the explode works
00307 $msg_data = str_replace("\r\n","\n",$msg_data);
00308 $msg_data = str_replace("\r","\n",$msg_data);
00309 $lines = explode("\n",$msg_data);
00310
00311 # we need to find a good way to determine is headers are
00312 # in the msg_data or if it is a straight msg body
00313 # currently I'm assuming rfc 822 definitions of msg headers
00314 # and if the first field of the first line (':' sperated)
00315 # does not contain a space then it _should_ be a header
00316 # and we can process all lines before a blank "" line as
00317 # headers.
00318 $field = substr($lines[0],0,strpos($lines[0],":"));
00319 $in_headers = false;
00320 if(!empty($field) && !strstr($field," ")) {
00321 $in_headers = true;
00322 }
00323
00324 $max_line_length = 998; # used below; set here for ease in change
00325
00326 while(list(,$line) = @each($lines)) {
00327 $lines_out = null;
00328 if($line == "" && $in_headers) {
00329 $in_headers = false;
00330 }
00331 # ok we need to break this line up into several
00332 # smaller lines
00333 while(strlen($line) > $max_line_length) {
00334 $pos = strrpos(substr($line,0,$max_line_length)," ");
00335
00336 # Patch to fix DOS attack
00337 if(!$pos) {
00338 $pos = $max_line_length - 1;
00339 }
00340
00341 $lines_out[] = substr($line,0,$pos);
00342 $line = substr($line,$pos + 1);
00343 # if we are processing headers we need to
00344 # add a LWSP-char to the front of the new line
00345 # rfc 822 on long msg headers
00346 if($in_headers) {
00347 $line = "\t" . $line;
00348 }
00349 }
00350 $lines_out[] = $line;
00351
00352 # now send the lines to the server
00353 while(list(,$line_out) = @each($lines_out)) {
00354 if(strlen($line_out) > 0)
00355 {
00356 if(substr($line_out, 0, 1) == ".") {
00357 $line_out = "." . $line_out;
00358 }
00359 }
00360 fputs($this->smtp_conn,$line_out . $this->CRLF);
00361 }
00362 }
00363
00364 # ok all the message data has been sent so lets get this
00365 # over with aleady
00366 fputs($this->smtp_conn, $this->CRLF . "." . $this->CRLF);
00367
00368 $rply = $this->get_lines();
00369 $code = substr($rply,0,3);
00370
00371 if($this->do_debug >= 2) {
00372 echo "SMTP -> FROM SERVER:" . $this->CRLF . $rply;
00373 }
00374
00375 if($code != 250) {
00376 $this->error =
00377 array("error" => "DATA not accepted from server",
00378 "smtp_code" => $code,
00379 "smtp_msg" => substr($rply,4));
00380 if($this->do_debug >= 1) {
00381 echo "SMTP -> ERROR: " . $this->error["error"] .
00382 ": " . $rply . $this->CRLF;
00383 }
00384 return false;
00385 }
00386 return true;
00387 }
00388
00405 function Expand($name) {
00406 $this->error = null; # so no confusion is caused
00407
00408 if(!$this->connected()) {
00409 $this->error = array(
00410 "error" => "Called Expand() without being connected");
00411 return false;
00412 }
00413
00414 fputs($this->smtp_conn,"EXPN " . $name . $this->CRLF);
00415
00416 $rply = $this->get_lines();
00417 $code = substr($rply,0,3);
00418
00419 if($this->do_debug >= 2) {
00420 echo "SMTP -> FROM SERVER:" . $this->CRLF . $rply;
00421 }
00422
00423 if($code != 250) {
00424 $this->error =
00425 array("error" => "EXPN not accepted from server",
00426 "smtp_code" => $code,
00427 "smtp_msg" => substr($rply,4));
00428 if($this->do_debug >= 1) {
00429 echo "SMTP -> ERROR: " . $this->error["error"] .
00430 ": " . $rply . $this->CRLF;
00431 }
00432 return false;
00433 }
00434
00435 # parse the reply and place in our array to return to user
00436 $entries = explode($this->CRLF,$rply);
00437 while(list(,$l) = @each($entries)) {
00438 $list[] = substr($l,4);
00439 }
00440
00441 return $list;
00442 }
00443
00456 function Hello($host="") {
00457 $this->error = null; # so no confusion is caused
00458
00459 if(!$this->connected()) {
00460 $this->error = array(
00461 "error" => "Called Hello() without being connected");
00462 return false;
00463 }
00464
00465 # if a hostname for the HELO wasn't specified determine
00466 # a suitable one to send
00467 if(empty($host)) {
00468 # we need to determine some sort of appopiate default
00469 # to send to the server
00470 $host = "localhost";
00471 }
00472
00473
00474 if(!$this->SendHello("EHLO", $host))
00475 {
00476 if(!$this->SendHello("HELO", $host))
00477 return false;
00478 }
00479
00480 return true;
00481 }
00482
00488 function SendHello($hello, $host) {
00489 fputs($this->smtp_conn, $hello . " " . $host . $this->CRLF);
00490
00491 $rply = $this->get_lines();
00492 $code = substr($rply,0,3);
00493
00494 if($this->do_debug >= 2) {
00495 echo "SMTP -> FROM SERVER: " . $this->CRLF . $rply;
00496 }
00497
00498 if($code != 250) {
00499 $this->error =
00500 array("error" => $hello . " not accepted from server",
00501 "smtp_code" => $code,
00502 "smtp_msg" => substr($rply,4));
00503 if($this->do_debug >= 1) {
00504 echo "SMTP -> ERROR: " . $this->error["error"] .
00505 ": " . $rply . $this->CRLF;
00506 }
00507 return false;
00508 }
00509
00510 $this->helo_rply = $rply;
00511
00512 return true;
00513 }
00514
00530 function Help($keyword="") {
00531 $this->error = null; # to avoid confusion
00532
00533 if(!$this->connected()) {
00534 $this->error = array(
00535 "error" => "Called Help() without being connected");
00536 return false;
00537 }
00538
00539 $extra = "";
00540 if(!empty($keyword)) {
00541 $extra = " " . $keyword;
00542 }
00543
00544 fputs($this->smtp_conn,"HELP" . $extra . $this->CRLF);
00545
00546 $rply = $this->get_lines();
00547 $code = substr($rply,0,3);
00548
00549 if($this->do_debug >= 2) {
00550 echo "SMTP -> FROM SERVER:" . $this->CRLF . $rply;
00551 }
00552
00553 if($code != 211 && $code != 214) {
00554 $this->error =
00555 array("error" => "HELP not accepted from server",
00556 "smtp_code" => $code,
00557 "smtp_msg" => substr($rply,4));
00558 if($this->do_debug >= 1) {
00559 echo "SMTP -> ERROR: " . $this->error["error"] .
00560 ": " . $rply . $this->CRLF;
00561 }
00562 return false;
00563 }
00564
00565 return $rply;
00566 }
00567
00582 function Mail($from) {
00583 $this->error = null; # so no confusion is caused
00584
00585 if(!$this->connected()) {
00586 $this->error = array(
00587 "error" => "Called Mail() without being connected");
00588 return false;
00589 }
00590
00591 fputs($this->smtp_conn,"MAIL FROM:<" . $from . ">" . $this->CRLF);
00592
00593 $rply = $this->get_lines();
00594 $code = substr($rply,0,3);
00595
00596 if($this->do_debug >= 2) {
00597 echo "SMTP -> FROM SERVER:" . $this->CRLF . $rply;
00598 }
00599
00600 if($code != 250) {
00601 $this->error =
00602 array("error" => "MAIL not accepted from server",
00603 "smtp_code" => $code,
00604 "smtp_msg" => substr($rply,4));
00605 if($this->do_debug >= 1) {
00606 echo "SMTP -> ERROR: " . $this->error["error"] .
00607 ": " . $rply . $this->CRLF;
00608 }
00609 return false;
00610 }
00611 return true;
00612 }
00613
00624 function Noop() {
00625 $this->error = null; # so no confusion is caused
00626
00627 if(!$this->connected()) {
00628 $this->error = array(
00629 "error" => "Called Noop() without being connected");
00630 return false;
00631 }
00632
00633 fputs($this->smtp_conn,"NOOP" . $this->CRLF);
00634
00635 $rply = $this->get_lines();
00636 $code = substr($rply,0,3);
00637
00638 if($this->do_debug >= 2) {
00639 echo "SMTP -> FROM SERVER:" . $this->CRLF . $rply;
00640 }
00641
00642 if($code != 250) {
00643 $this->error =
00644 array("error" => "NOOP not accepted from server",
00645 "smtp_code" => $code,
00646 "smtp_msg" => substr($rply,4));
00647 if($this->do_debug >= 1) {
00648 echo "SMTP -> ERROR: " . $this->error["error"] .
00649 ": " . $rply . $this->CRLF;
00650 }
00651 return false;
00652 }
00653 return true;
00654 }
00655
00667 function Quit($close_on_error=true) {
00668 $this->error = null; # so there is no confusion
00669
00670 if(!$this->connected()) {
00671 $this->error = array(
00672 "error" => "Called Quit() without being connected");
00673 return false;
00674 }
00675
00676 # send the quit command to the server
00677 fputs($this->smtp_conn,"quit" . $this->CRLF);
00678
00679 # get any good-bye messages
00680 $byemsg = $this->get_lines();
00681
00682 if($this->do_debug >= 2) {
00683 echo "SMTP -> FROM SERVER:" . $this->CRLF . $byemsg;
00684 }
00685
00686 $rval = true;
00687 $e = null;
00688
00689 $code = substr($byemsg,0,3);
00690 if($code != 221) {
00691 # use e as a tmp var cause Close will overwrite $this->error
00692 $e = array("error" => "SMTP server rejected quit command",
00693 "smtp_code" => $code,
00694 "smtp_rply" => substr($byemsg,4));
00695 $rval = false;
00696 if($this->do_debug >= 1) {
00697 echo "SMTP -> ERROR: " . $e["error"] . ": " .
00698 $byemsg . $this->CRLF;
00699 }
00700 }
00701
00702 if(empty($e) || $close_on_error) {
00703 $this->Close();
00704 }
00705
00706 return $rval;
00707 }
00708
00721 function Recipient($to) {
00722 $this->error = null; # so no confusion is caused
00723
00724 if(!$this->connected()) {
00725 $this->error = array(
00726 "error" => "Called Recipient() without being connected");
00727 return false;
00728 }
00729
00730 fputs($this->smtp_conn,"RCPT TO:<" . $to . ">" . $this->CRLF);
00731
00732 $rply = $this->get_lines();
00733 $code = substr($rply,0,3);
00734
00735 if($this->do_debug >= 2) {
00736 echo "SMTP -> FROM SERVER:" . $this->CRLF . $rply;
00737 }
00738
00739 if($code != 250 && $code != 251) {
00740 $this->error =
00741 array("error" => "RCPT not accepted from server",
00742 "smtp_code" => $code,
00743 "smtp_msg" => substr($rply,4));
00744 if($this->do_debug >= 1) {
00745 echo "SMTP -> ERROR: " . $this->error["error"] .
00746 ": " . $rply . $this->CRLF;
00747 }
00748 return false;
00749 }
00750 return true;
00751 }
00752
00765 function Reset() {
00766 $this->error = null; # so no confusion is caused
00767
00768 if(!$this->connected()) {
00769 $this->error = array(
00770 "error" => "Called Reset() without being connected");
00771 return false;
00772 }
00773
00774 fputs($this->smtp_conn,"RSET" . $this->CRLF);
00775
00776 $rply = $this->get_lines();
00777 $code = substr($rply,0,3);
00778
00779 if($this->do_debug >= 2) {
00780 echo "SMTP -> FROM SERVER:" . $this->CRLF . $rply;
00781 }
00782
00783 if($code != 250) {
00784 $this->error =
00785 array("error" => "RSET failed",
00786 "smtp_code" => $code,
00787 "smtp_msg" => substr($rply,4));
00788 if($this->do_debug >= 1) {
00789 echo "SMTP -> ERROR: " . $this->error["error"] .
00790 ": " . $rply . $this->CRLF;
00791 }
00792 return false;
00793 }
00794
00795 return true;
00796 }
00797
00814 function Send($from) {
00815 $this->error = null; # so no confusion is caused
00816
00817 if(!$this->connected()) {
00818 $this->error = array(
00819 "error" => "Called Send() without being connected");
00820 return false;
00821 }
00822
00823 fputs($this->smtp_conn,"SEND FROM:" . $from . $this->CRLF);
00824
00825 $rply = $this->get_lines();
00826 $code = substr($rply,0,3);
00827
00828 if($this->do_debug >= 2) {
00829 echo "SMTP -> FROM SERVER:" . $this->CRLF . $rply;
00830 }
00831
00832 if($code != 250) {
00833 $this->error =
00834 array("error" => "SEND not accepted from server",
00835 "smtp_code" => $code,
00836 "smtp_msg" => substr($rply,4));
00837 if($this->do_debug >= 1) {
00838 echo "SMTP -> ERROR: " . $this->error["error"] .
00839 ": " . $rply . $this->CRLF;
00840 }
00841 return false;
00842 }
00843 return true;
00844 }
00845
00862 function SendAndMail($from) {
00863 $this->error = null; # so no confusion is caused
00864
00865 if(!$this->connected()) {
00866 $this->error = array(
00867 "error" => "Called SendAndMail() without being connected");
00868 return false;
00869 }
00870
00871 fputs($this->smtp_conn,"SAML FROM:" . $from . $this->CRLF);
00872
00873 $rply = $this->get_lines();
00874 $code = substr($rply,0,3);
00875
00876 if($this->do_debug >= 2) {
00877 echo "SMTP -> FROM SERVER:" . $this->CRLF . $rply;
00878 }
00879
00880 if($code != 250) {
00881 $this->error =
00882 array("error" => "SAML not accepted from server",
00883 "smtp_code" => $code,
00884 "smtp_msg" => substr($rply,4));
00885 if($this->do_debug >= 1) {
00886 echo "SMTP -> ERROR: " . $this->error["error"] .
00887 ": " . $rply . $this->CRLF;
00888 }
00889 return false;
00890 }
00891 return true;
00892 }
00893
00910 function SendOrMail($from) {
00911 $this->error = null; # so no confusion is caused
00912
00913 if(!$this->connected()) {
00914 $this->error = array(
00915 "error" => "Called SendOrMail() without being connected");
00916 return false;
00917 }
00918
00919 fputs($this->smtp_conn,"SOML FROM:" . $from . $this->CRLF);
00920
00921 $rply = $this->get_lines();
00922 $code = substr($rply,0,3);
00923
00924 if($this->do_debug >= 2) {
00925 echo "SMTP -> FROM SERVER:" . $this->CRLF . $rply;
00926 }
00927
00928 if($code != 250) {
00929 $this->error =
00930 array("error" => "SOML not accepted from server",
00931 "smtp_code" => $code,
00932 "smtp_msg" => substr($rply,4));
00933 if($this->do_debug >= 1) {
00934 echo "SMTP -> ERROR: " . $this->error["error"] .
00935 ": " . $rply . $this->CRLF;
00936 }
00937 return false;
00938 }
00939 return true;
00940 }
00941
00955 function Turn() {
00956 $this->error = array("error" => "This method, TURN, of the SMTP ".
00957 "is not implemented");
00958 if($this->do_debug >= 1) {
00959 echo "SMTP -> NOTICE: " . $this->error["error"] . $this->CRLF;
00960 }
00961 return false;
00962 }
00963
00977 function Verify($name) {
00978 $this->error = null; # so no confusion is caused
00979
00980 if(!$this->connected()) {
00981 $this->error = array(
00982 "error" => "Called Verify() without being connected");
00983 return false;
00984 }
00985
00986 fputs($this->smtp_conn,"VRFY " . $name . $this->CRLF);
00987
00988 $rply = $this->get_lines();
00989 $code = substr($rply,0,3);
00990
00991 if($this->do_debug >= 2) {
00992 echo "SMTP -> FROM SERVER:" . $this->CRLF . $rply;
00993 }
00994
00995 if($code != 250 && $code != 251) {
00996 $this->error =
00997 array("error" => "VRFY failed on name '$name'",
00998 "smtp_code" => $code,
00999 "smtp_msg" => substr($rply,4));
01000 if($this->do_debug >= 1) {
01001 echo "SMTP -> ERROR: " . $this->error["error"] .
01002 ": " . $rply . $this->CRLF;
01003 }
01004 return false;
01005 }
01006 return $rply;
01007 }
01008
01009
01010
01011
01012
01022 function get_lines() {
01023 $data = "";
01024 while($str = fgets($this->smtp_conn,515)) {
01025 if($this->do_debug >= 4) {
01026 echo "SMTP -> get_lines(): \$data was \"$data\"" .
01027 $this->CRLF;
01028 echo "SMTP -> get_lines(): \$str is \"$str\"" .
01029 $this->CRLF;
01030 }
01031 $data .= $str;
01032 if($this->do_debug >= 4) {
01033 echo "SMTP -> get_lines(): \$data is \"$data\"" . $this->CRLF;
01034 }
01035 # if the 4th character is a space then we are done reading
01036 # so just break the loop
01037 if(substr($str,3,1) == " ") { break; }
01038 }
01039 return $data;
01040 }
01041
01042 }
01043
01044
01045 ?>