[ Index ] |
|
Code source de vtiger CRM 5.0.2 |
1 <?php 2 //////////////////////////////////////////////////// 3 // SMTP - PHP SMTP class 4 // 5 // Version 1.02 6 // 7 // Define an SMTP class that can be used to connect 8 // and communicate with any SMTP server. It implements 9 // all the SMTP functions defined in RFC821 except TURN. 10 // 11 // Author: Chris Ryan 12 // 13 // License: LGPL, see LICENSE 14 //////////////////////////////////////////////////// 15 16 /** 17 * SMTP is rfc 821 compliant and implements all the rfc 821 SMTP 18 * commands except TURN which will always return a not implemented 19 * error. SMTP also provides some utility methods for sending mail 20 * to an SMTP server. 21 * @package PHPMailer 22 * @author Chris Ryan 23 */ 24 class SMTP 25 { 26 /** 27 * SMTP server port 28 * @var int 29 */ 30 var $SMTP_PORT = 25; 31 32 /** 33 * SMTP reply line ending 34 * @var string 35 */ 36 var $CRLF = "\r\n"; 37 38 /** 39 * Sets whether debugging is turned on 40 * @var bool 41 */ 42 var $do_debug; # the level of debug to perform 43 44 /**#@+ 45 * @access private 46 */ 47 var $smtp_conn; # the socket to the server 48 var $error; # error if any on the last call 49 var $helo_rply; # the reply the server sent to us for HELO 50 /**#@-*/ 51 52 /** 53 * Initialize the class so that the data is in a known state. 54 * @access public 55 * @return void 56 */ 57 function SMTP() { 58 $this->smtp_conn = 0; 59 $this->error = null; 60 $this->helo_rply = null; 61 62 $this->do_debug = 0; 63 } 64 65 /************************************************************* 66 * CONNECTION FUNCTIONS * 67 ***********************************************************/ 68 69 /** 70 * Connect to the server specified on the port specified. 71 * If the port is not specified use the default SMTP_PORT. 72 * If tval is specified then a connection will try and be 73 * established with the server for that number of seconds. 74 * If tval is not specified the default is 30 seconds to 75 * try on the connection. 76 * 77 * SMTP CODE SUCCESS: 220 78 * SMTP CODE FAILURE: 421 79 * @access public 80 * @return bool 81 */ 82 function Connect($host,$port=0,$tval=30) { 83 # set the error val to null so there is no confusion 84 $this->error = null; 85 86 # make sure we are __not__ connected 87 if($this->connected()) { 88 # ok we are connected! what should we do? 89 # for now we will just give an error saying we 90 # are already connected 91 $this->error = 92 array("error" => "Already connected to a server"); 93 return false; 94 } 95 96 if(empty($port)) { 97 $port = $this->SMTP_PORT; 98 } 99 100 #connect to the smtp server 101 if(!@ $this->smtp_conn = fsockopen($host, # the host of the server 102 $port, # the port to use 103 $errno, # error number if any 104 $errstr, # error message if any 105 $tval)) # give up after ? secs 106 { 107 // echo '<b><center>Could Not connect to Mail Server</b><br><br>'; 108 return false; 109 } 110 111 # verify we connected properly 112 if(empty($this->smtp_conn)) { 113 $this->error = array("error" => "Failed to connect to server", 114 "errno" => $errno, 115 "errstr" => $errstr); 116 if($this->do_debug >= 1) { 117 echo "SMTP -> ERROR: " . $this->error["error"] . 118 ": $errstr ($errno)" . $this->CRLF; 119 } 120 return false; 121 } 122 123 # sometimes the SMTP server takes a little longer to respond 124 # so we will give it a longer timeout for the first read 125 // Windows still does not have support for this timeout function 126 if(substr(PHP_OS, 0, 3) != "WIN") 127 socket_set_timeout($this->smtp_conn, $tval, 0); 128 129 # get any announcement stuff 130 $announce = $this->get_lines(); 131 132 # set the timeout of any socket functions at 1/10 of a second 133 //if(function_exists("socket_set_timeout")) 134 // socket_set_timeout($this->smtp_conn, 0, 100000); 135 136 if($this->do_debug >= 2) { 137 echo "SMTP -> FROM SERVER:" . $this->CRLF . $announce; 138 } 139 140 return true; 141 } 142 143 /** 144 * Performs SMTP authentication. Must be run after running the 145 * Hello() method. Returns true if successfully authenticated. 146 * @access public 147 * @return bool 148 */ 149 function Authenticate($username, $password) { 150 // Start authentication 151 fputs($this->smtp_conn,"AUTH LOGIN" . $this->CRLF); 152 153 $rply = $this->get_lines(); 154 $code = substr($rply,0,3); 155 156 if($code != 334) { 157 $this->error = 158 array("error" => "AUTH not accepted from server", 159 "smtp_code" => $code, 160 "smtp_msg" => substr($rply,4)); 161 if($this->do_debug >= 1) { 162 echo "SMTP -> ERROR: " . $this->error["error"] . 163 ": " . $rply . $this->CRLF; 164 } 165 return false; 166 } 167 168 // Send encoded username 169 fputs($this->smtp_conn, base64_encode($username) . $this->CRLF); 170 171 $rply = $this->get_lines(); 172 $code = substr($rply,0,3); 173 174 if($code != 334) { 175 $this->error = 176 array("error" => "Username not accepted from server", 177 "smtp_code" => $code, 178 "smtp_msg" => substr($rply,4)); 179 if($this->do_debug >= 1) { 180 echo "SMTP -> ERROR: " . $this->error["error"] . 181 ": " . $rply . $this->CRLF; 182 } 183 return false; 184 } 185 186 // Send encoded password 187 fputs($this->smtp_conn, base64_encode($password) . $this->CRLF); 188 189 $rply = $this->get_lines(); 190 $code = substr($rply,0,3); 191 192 if($code != 235) { 193 $this->error = 194 array("error" => "Password not accepted from server", 195 "smtp_code" => $code, 196 "smtp_msg" => substr($rply,4)); 197 if($this->do_debug >= 1) { 198 echo "SMTP -> ERROR: " . $this->error["error"] . 199 ": " . $rply . $this->CRLF; 200 } 201 return false; 202 } 203 204 return true; 205 } 206 207 /** 208 * Returns true if connected to a server otherwise false 209 * @access private 210 * @return bool 211 */ 212 function Connected() { 213 if(!empty($this->smtp_conn)) { 214 $sock_status = socket_get_status($this->smtp_conn); 215 if($sock_status["eof"]) { 216 # hmm this is an odd situation... the socket is 217 # valid but we aren't connected anymore 218 if($this->do_debug >= 1) { 219 echo "SMTP -> NOTICE:" . $this->CRLF . 220 "EOF caught while checking if connected"; 221 } 222 $this->Close(); 223 return false; 224 } 225 return true; # everything looks good 226 } 227 return false; 228 } 229 230 /** 231 * Closes the socket and cleans up the state of the class. 232 * It is not considered good to use this function without 233 * first trying to use QUIT. 234 * @access public 235 * @return void 236 */ 237 function Close() { 238 $this->error = null; # so there is no confusion 239 $this->helo_rply = null; 240 if(!empty($this->smtp_conn)) { 241 # close the connection and cleanup 242 fclose($this->smtp_conn); 243 $this->smtp_conn = 0; 244 } 245 } 246 247 248 /*************************************************************** 249 * SMTP COMMANDS * 250 *************************************************************/ 251 252 /** 253 * Issues a data command and sends the msg_data to the server 254 * finializing the mail transaction. $msg_data is the message 255 * that is to be send with the headers. Each header needs to be 256 * on a single line followed by a <CRLF> with the message headers 257 * and the message body being seperated by and additional <CRLF>. 258 * 259 * Implements rfc 821: DATA <CRLF> 260 * 261 * SMTP CODE INTERMEDIATE: 354 262 * [data] 263 * <CRLF>.<CRLF> 264 * SMTP CODE SUCCESS: 250 265 * SMTP CODE FAILURE: 552,554,451,452 266 * SMTP CODE FAILURE: 451,554 267 * SMTP CODE ERROR : 500,501,503,421 268 * @access public 269 * @return bool 270 */ 271 function Data($msg_data) { 272 $this->error = null; # so no confusion is caused 273 274 if(!$this->connected()) { 275 $this->error = array( 276 "error" => "Called Data() without being connected"); 277 return false; 278 } 279 280 fputs($this->smtp_conn,"DATA" . $this->CRLF); 281 282 $rply = $this->get_lines(); 283 $code = substr($rply,0,3); 284 285 if($this->do_debug >= 2) { 286 echo "SMTP -> FROM SERVER:" . $this->CRLF . $rply; 287 } 288 289 if($code != 354) { 290 $this->error = 291 array("error" => "DATA command not accepted from server", 292 "smtp_code" => $code, 293 "smtp_msg" => substr($rply,4)); 294 if($this->do_debug >= 1) { 295 echo "SMTP -> ERROR: " . $this->error["error"] . 296 ": " . $rply . $this->CRLF; 297 } 298 return false; 299 } 300 301 # the server is ready to accept data! 302 # according to rfc 821 we should not send more than 1000 303 # including the CRLF 304 # characters on a single line so we will break the data up 305 # into lines by \r and/or \n then if needed we will break 306 # each of those into smaller lines to fit within the limit. 307 # in addition we will be looking for lines that start with 308 # a period '.' and append and additional period '.' to that 309 # line. NOTE: this does not count towards are limit. 310 311 # normalize the line breaks so we know the explode works 312 $msg_data = str_replace("\r\n","\n",$msg_data); 313 $msg_data = str_replace("\r","\n",$msg_data); 314 $lines = explode("\n",$msg_data); 315 316 # we need to find a good way to determine is headers are 317 # in the msg_data or if it is a straight msg body 318 # currently I'm assuming rfc 822 definitions of msg headers 319 # and if the first field of the first line (':' sperated) 320 # does not contain a space then it _should_ be a header 321 # and we can process all lines before a blank "" line as 322 # headers. 323 $field = substr($lines[0],0,strpos($lines[0],":")); 324 $in_headers = false; 325 if(!empty($field) && !strstr($field," ")) { 326 $in_headers = true; 327 } 328 329 $max_line_length = 998; # used below; set here for ease in change 330 331 while(list(,$line) = @each($lines)) { 332 $lines_out = null; 333 if($line == "" && $in_headers) { 334 $in_headers = false; 335 } 336 # ok we need to break this line up into several 337 # smaller lines 338 while(strlen($line) > $max_line_length) { 339 $pos = strrpos(substr($line,0,$max_line_length)," "); 340 $lines_out[] = substr($line,0,$pos); 341 $line = substr($line,$pos + 1); 342 # if we are processing headers we need to 343 # add a LWSP-char to the front of the new line 344 # rfc 822 on long msg headers 345 if($in_headers) { 346 $line = "\t" . $line; 347 } 348 } 349 $lines_out[] = $line; 350 351 # now send the lines to the server 352 while(list(,$line_out) = @each($lines_out)) { 353 if(strlen($line_out) > 0) 354 { 355 if(substr($line_out, 0, 1) == ".") { 356 $line_out = "." . $line_out; 357 } 358 } 359 fputs($this->smtp_conn,$line_out . $this->CRLF); 360 } 361 } 362 363 # ok all the message data has been sent so lets get this 364 # over with aleady 365 fputs($this->smtp_conn, $this->CRLF . "." . $this->CRLF); 366 367 $rply = $this->get_lines(); 368 $code = substr($rply,0,3); 369 370 if($this->do_debug >= 2) { 371 echo "SMTP -> FROM SERVER:" . $this->CRLF . $rply; 372 } 373 374 if($code != 250) { 375 $this->error = 376 array("error" => "DATA not accepted from server", 377 "smtp_code" => $code, 378 "smtp_msg" => substr($rply,4)); 379 if($this->do_debug >= 1) { 380 echo "SMTP -> ERROR: " . $this->error["error"] . 381 ": " . $rply . $this->CRLF; 382 } 383 return false; 384 } 385 return true; 386 } 387 388 /** 389 * Expand takes the name and asks the server to list all the 390 * people who are members of the _list_. Expand will return 391 * back and array of the result or false if an error occurs. 392 * Each value in the array returned has the format of: 393 * [ <full-name> <sp> ] <path> 394 * The definition of <path> is defined in rfc 821 395 * 396 * Implements rfc 821: EXPN <SP> <string> <CRLF> 397 * 398 * SMTP CODE SUCCESS: 250 399 * SMTP CODE FAILURE: 550 400 * SMTP CODE ERROR : 500,501,502,504,421 401 * @access public 402 * @return string array 403 */ 404 function Expand($name) { 405 $this->error = null; # so no confusion is caused 406 407 if(!$this->connected()) { 408 $this->error = array( 409 "error" => "Called Expand() without being connected"); 410 return false; 411 } 412 413 fputs($this->smtp_conn,"EXPN " . $name . $this->CRLF); 414 415 $rply = $this->get_lines(); 416 $code = substr($rply,0,3); 417 418 if($this->do_debug >= 2) { 419 echo "SMTP -> FROM SERVER:" . $this->CRLF . $rply; 420 } 421 422 if($code != 250) { 423 $this->error = 424 array("error" => "EXPN not accepted from server", 425 "smtp_code" => $code, 426 "smtp_msg" => substr($rply,4)); 427 if($this->do_debug >= 1) { 428 echo "SMTP -> ERROR: " . $this->error["error"] . 429 ": " . $rply . $this->CRLF; 430 } 431 return false; 432 } 433 434 # parse the reply and place in our array to return to user 435 $entries = explode($this->CRLF,$rply); 436 while(list(,$l) = @each($entries)) { 437 $list[] = substr($l,4); 438 } 439 440 return $list; 441 } 442 443 /** 444 * Sends the HELO command to the smtp server. 445 * This makes sure that we and the server are in 446 * the same known state. 447 * 448 * Implements from rfc 821: HELO <SP> <domain> <CRLF> 449 * 450 * SMTP CODE SUCCESS: 250 451 * SMTP CODE ERROR : 500, 501, 504, 421 452 * @access public 453 * @return bool 454 */ 455 function Hello($host="") { 456 $this->error = null; # so no confusion is caused 457 458 if(!$this->connected()) { 459 $this->error = array( 460 "error" => "Called Hello() without being connected"); 461 return false; 462 } 463 464 # if a hostname for the HELO wasn't specified determine 465 # a suitable one to send 466 if(empty($host)) { 467 # we need to determine some sort of appopiate default 468 # to send to the server 469 $host = "localhost"; 470 } 471 472 // Send extended hello first (RFC 2821) 473 if(!$this->SendHello("EHLO", $host)) 474 { 475 if(!$this->SendHello("HELO", $host)) 476 return false; 477 } 478 479 return true; 480 } 481 482 /** 483 * Sends a HELO/EHLO command. 484 * @access private 485 * @return bool 486 */ 487 function SendHello($hello, $host) { 488 fputs($this->smtp_conn, $hello . " " . $host . $this->CRLF); 489 490 $rply = $this->get_lines(); 491 $code = substr($rply,0,3); 492 493 if($this->do_debug >= 2) { 494 echo "SMTP -> FROM SERVER: " . $this->CRLF . $rply; 495 } 496 497 if($code != 250) { 498 $this->error = 499 array("error" => $hello . " not accepted from server", 500 "smtp_code" => $code, 501 "smtp_msg" => substr($rply,4)); 502 if($this->do_debug >= 1) { 503 echo "SMTP -> ERROR: " . $this->error["error"] . 504 ": " . $rply . $this->CRLF; 505 } 506 return false; 507 } 508 509 $this->helo_rply = $rply; 510 511 return true; 512 } 513 514 /** 515 * Gets help information on the keyword specified. If the keyword 516 * is not specified then returns generic help, ussually contianing 517 * A list of keywords that help is available on. This function 518 * returns the results back to the user. It is up to the user to 519 * handle the returned data. If an error occurs then false is 520 * returned with $this->error set appropiately. 521 * 522 * Implements rfc 821: HELP [ <SP> <string> ] <CRLF> 523 * 524 * SMTP CODE SUCCESS: 211,214 525 * SMTP CODE ERROR : 500,501,502,504,421 526 * @access public 527 * @return string 528 */ 529 function Help($keyword="") { 530 $this->error = null; # to avoid confusion 531 532 if(!$this->connected()) { 533 $this->error = array( 534 "error" => "Called Help() without being connected"); 535 return false; 536 } 537 538 $extra = ""; 539 if(!empty($keyword)) { 540 $extra = " " . $keyword; 541 } 542 543 fputs($this->smtp_conn,"HELP" . $extra . $this->CRLF); 544 545 $rply = $this->get_lines(); 546 $code = substr($rply,0,3); 547 548 if($this->do_debug >= 2) { 549 echo "SMTP -> FROM SERVER:" . $this->CRLF . $rply; 550 } 551 552 if($code != 211 && $code != 214) { 553 $this->error = 554 array("error" => "HELP not accepted from server", 555 "smtp_code" => $code, 556 "smtp_msg" => substr($rply,4)); 557 if($this->do_debug >= 1) { 558 echo "SMTP -> ERROR: " . $this->error["error"] . 559 ": " . $rply . $this->CRLF; 560 } 561 return false; 562 } 563 564 return $rply; 565 } 566 567 /** 568 * Starts a mail transaction from the email address specified in 569 * $from. Returns true if successful or false otherwise. If True 570 * the mail transaction is started and then one or more Recipient 571 * commands may be called followed by a Data command. 572 * 573 * Implements rfc 821: MAIL <SP> FROM:<reverse-path> <CRLF> 574 * 575 * SMTP CODE SUCCESS: 250 576 * SMTP CODE SUCCESS: 552,451,452 577 * SMTP CODE SUCCESS: 500,501,421 578 * @access public 579 * @return bool 580 */ 581 function Mail($from) { 582 $this->error = null; # so no confusion is caused 583 584 if(!$this->connected()) { 585 $this->error = array( 586 "error" => "Called Mail() without being connected"); 587 return false; 588 } 589 590 fputs($this->smtp_conn,"MAIL FROM:<" . $from . ">" . $this->CRLF); 591 592 $rply = $this->get_lines(); 593 $code = substr($rply,0,3); 594 595 if($this->do_debug >= 2) { 596 echo "SMTP -> FROM SERVER:" . $this->CRLF . $rply; 597 } 598 599 if($code != 250) { 600 $this->error = 601 array("error" => "MAIL not accepted from server", 602 "smtp_code" => $code, 603 "smtp_msg" => substr($rply,4)); 604 if($this->do_debug >= 1) { 605 echo "SMTP -> ERROR: " . $this->error["error"] . 606 ": " . $rply . $this->CRLF; 607 } 608 return false; 609 } 610 return true; 611 } 612 613 /** 614 * Sends the command NOOP to the SMTP server. 615 * 616 * Implements from rfc 821: NOOP <CRLF> 617 * 618 * SMTP CODE SUCCESS: 250 619 * SMTP CODE ERROR : 500, 421 620 * @access public 621 * @return bool 622 */ 623 function Noop() { 624 $this->error = null; # so no confusion is caused 625 626 if(!$this->connected()) { 627 $this->error = array( 628 "error" => "Called Noop() without being connected"); 629 return false; 630 } 631 632 fputs($this->smtp_conn,"NOOP" . $this->CRLF); 633 634 $rply = $this->get_lines(); 635 $code = substr($rply,0,3); 636 637 if($this->do_debug >= 2) { 638 echo "SMTP -> FROM SERVER:" . $this->CRLF . $rply; 639 } 640 641 if($code != 250) { 642 $this->error = 643 array("error" => "NOOP not accepted from server", 644 "smtp_code" => $code, 645 "smtp_msg" => substr($rply,4)); 646 if($this->do_debug >= 1) { 647 echo "SMTP -> ERROR: " . $this->error["error"] . 648 ": " . $rply . $this->CRLF; 649 } 650 return false; 651 } 652 return true; 653 } 654 655 /** 656 * Sends the quit command to the server and then closes the socket 657 * if there is no error or the $close_on_error argument is true. 658 * 659 * Implements from rfc 821: QUIT <CRLF> 660 * 661 * SMTP CODE SUCCESS: 221 662 * SMTP CODE ERROR : 500 663 * @access public 664 * @return bool 665 */ 666 function Quit($close_on_error=true) { 667 $this->error = null; # so there is no confusion 668 669 if(!$this->connected()) { 670 $this->error = array( 671 "error" => "Called Quit() without being connected"); 672 return false; 673 } 674 675 # send the quit command to the server 676 fputs($this->smtp_conn,"quit" . $this->CRLF); 677 678 # get any good-bye messages 679 $byemsg = $this->get_lines(); 680 681 if($this->do_debug >= 2) { 682 echo "SMTP -> FROM SERVER:" . $this->CRLF . $byemsg; 683 } 684 685 $rval = true; 686 $e = null; 687 688 $code = substr($byemsg,0,3); 689 if($code != 221) { 690 # use e as a tmp var cause Close will overwrite $this->error 691 $e = array("error" => "SMTP server rejected quit command", 692 "smtp_code" => $code, 693 "smtp_rply" => substr($byemsg,4)); 694 $rval = false; 695 if($this->do_debug >= 1) { 696 echo "SMTP -> ERROR: " . $e["error"] . ": " . 697 $byemsg . $this->CRLF; 698 } 699 } 700 701 if(empty($e) || $close_on_error) { 702 $this->Close(); 703 } 704 705 return $rval; 706 } 707 708 /** 709 * Sends the command RCPT to the SMTP server with the TO: argument of $to. 710 * Returns true if the recipient was accepted false if it was rejected. 711 * 712 * Implements from rfc 821: RCPT <SP> TO:<forward-path> <CRLF> 713 * 714 * SMTP CODE SUCCESS: 250,251 715 * SMTP CODE FAILURE: 550,551,552,553,450,451,452 716 * SMTP CODE ERROR : 500,501,503,421 717 * @access public 718 * @return bool 719 */ 720 function Recipient($to) { 721 $this->error = null; # so no confusion is caused 722 723 if(!$this->connected()) { 724 $this->error = array( 725 "error" => "Called Recipient() without being connected"); 726 return false; 727 } 728 729 fputs($this->smtp_conn,"RCPT TO:<" . $to . ">" . $this->CRLF); 730 731 $rply = $this->get_lines(); 732 $code = substr($rply,0,3); 733 734 if($this->do_debug >= 2) { 735 echo "SMTP -> FROM SERVER:" . $this->CRLF . $rply; 736 } 737 738 if($code != 250 && $code != 251) { 739 $this->error = 740 array("error" => "RCPT not accepted from server", 741 "smtp_code" => $code, 742 "smtp_msg" => substr($rply,4)); 743 if($this->do_debug >= 1) { 744 echo "SMTP -> ERROR: " . $this->error["error"] . 745 ": " . $rply . $this->CRLF; 746 } 747 return false; 748 } 749 return true; 750 } 751 752 /** 753 * Sends the RSET command to abort and transaction that is 754 * currently in progress. Returns true if successful false 755 * otherwise. 756 * 757 * Implements rfc 821: RSET <CRLF> 758 * 759 * SMTP CODE SUCCESS: 250 760 * SMTP CODE ERROR : 500,501,504,421 761 * @access public 762 * @return bool 763 */ 764 function Reset() { 765 $this->error = null; # so no confusion is caused 766 767 if(!$this->connected()) { 768 $this->error = array( 769 "error" => "Called Reset() without being connected"); 770 return false; 771 } 772 773 fputs($this->smtp_conn,"RSET" . $this->CRLF); 774 775 $rply = $this->get_lines(); 776 $code = substr($rply,0,3); 777 778 if($this->do_debug >= 2) { 779 echo "SMTP -> FROM SERVER:" . $this->CRLF . $rply; 780 } 781 782 if($code != 250) { 783 $this->error = 784 array("error" => "RSET failed", 785 "smtp_code" => $code, 786 "smtp_msg" => substr($rply,4)); 787 if($this->do_debug >= 1) { 788 echo "SMTP -> ERROR: " . $this->error["error"] . 789 ": " . $rply . $this->CRLF; 790 } 791 return false; 792 } 793 794 return true; 795 } 796 797 /** 798 * Starts a mail transaction from the email address specified in 799 * $from. Returns true if successful or false otherwise. If True 800 * the mail transaction is started and then one or more Recipient 801 * commands may be called followed by a Data command. This command 802 * will send the message to the users terminal if they are logged 803 * in. 804 * 805 * Implements rfc 821: SEND <SP> FROM:<reverse-path> <CRLF> 806 * 807 * SMTP CODE SUCCESS: 250 808 * SMTP CODE SUCCESS: 552,451,452 809 * SMTP CODE SUCCESS: 500,501,502,421 810 * @access public 811 * @return bool 812 */ 813 function Send($from) { 814 $this->error = null; # so no confusion is caused 815 816 if(!$this->connected()) { 817 $this->error = array( 818 "error" => "Called Send() without being connected"); 819 return false; 820 } 821 822 fputs($this->smtp_conn,"SEND FROM:" . $from . $this->CRLF); 823 824 $rply = $this->get_lines(); 825 $code = substr($rply,0,3); 826 827 if($this->do_debug >= 2) { 828 echo "SMTP -> FROM SERVER:" . $this->CRLF . $rply; 829 } 830 831 if($code != 250) { 832 $this->error = 833 array("error" => "SEND not accepted from server", 834 "smtp_code" => $code, 835 "smtp_msg" => substr($rply,4)); 836 if($this->do_debug >= 1) { 837 echo "SMTP -> ERROR: " . $this->error["error"] . 838 ": " . $rply . $this->CRLF; 839 } 840 return false; 841 } 842 return true; 843 } 844 845 /** 846 * Starts a mail transaction from the email address specified in 847 * $from. Returns true if successful or false otherwise. If True 848 * the mail transaction is started and then one or more Recipient 849 * commands may be called followed by a Data command. This command 850 * will send the message to the users terminal if they are logged 851 * in and send them an email. 852 * 853 * Implements rfc 821: SAML <SP> FROM:<reverse-path> <CRLF> 854 * 855 * SMTP CODE SUCCESS: 250 856 * SMTP CODE SUCCESS: 552,451,452 857 * SMTP CODE SUCCESS: 500,501,502,421 858 * @access public 859 * @return bool 860 */ 861 function SendAndMail($from) { 862 $this->error = null; # so no confusion is caused 863 864 if(!$this->connected()) { 865 $this->error = array( 866 "error" => "Called SendAndMail() without being connected"); 867 return false; 868 } 869 870 fputs($this->smtp_conn,"SAML FROM:" . $from . $this->CRLF); 871 872 $rply = $this->get_lines(); 873 $code = substr($rply,0,3); 874 875 if($this->do_debug >= 2) { 876 echo "SMTP -> FROM SERVER:" . $this->CRLF . $rply; 877 } 878 879 if($code != 250) { 880 $this->error = 881 array("error" => "SAML not accepted from server", 882 "smtp_code" => $code, 883 "smtp_msg" => substr($rply,4)); 884 if($this->do_debug >= 1) { 885 echo "SMTP -> ERROR: " . $this->error["error"] . 886 ": " . $rply . $this->CRLF; 887 } 888 return false; 889 } 890 return true; 891 } 892 893 /** 894 * Starts a mail transaction from the email address specified in 895 * $from. Returns true if successful or false otherwise. If True 896 * the mail transaction is started and then one or more Recipient 897 * commands may be called followed by a Data command. This command 898 * will send the message to the users terminal if they are logged 899 * in or mail it to them if they are not. 900 * 901 * Implements rfc 821: SOML <SP> FROM:<reverse-path> <CRLF> 902 * 903 * SMTP CODE SUCCESS: 250 904 * SMTP CODE SUCCESS: 552,451,452 905 * SMTP CODE SUCCESS: 500,501,502,421 906 * @access public 907 * @return bool 908 */ 909 function SendOrMail($from) { 910 $this->error = null; # so no confusion is caused 911 912 if(!$this->connected()) { 913 $this->error = array( 914 "error" => "Called SendOrMail() without being connected"); 915 return false; 916 } 917 918 fputs($this->smtp_conn,"SOML FROM:" . $from . $this->CRLF); 919 920 $rply = $this->get_lines(); 921 $code = substr($rply,0,3); 922 923 if($this->do_debug >= 2) { 924 echo "SMTP -> FROM SERVER:" . $this->CRLF . $rply; 925 } 926 927 if($code != 250) { 928 $this->error = 929 array("error" => "SOML not accepted from server", 930 "smtp_code" => $code, 931 "smtp_msg" => substr($rply,4)); 932 if($this->do_debug >= 1) { 933 echo "SMTP -> ERROR: " . $this->error["error"] . 934 ": " . $rply . $this->CRLF; 935 } 936 return false; 937 } 938 return true; 939 } 940 941 /** 942 * This is an optional command for SMTP that this class does not 943 * support. This method is here to make the RFC821 Definition 944 * complete for this class and __may__ be implimented in the future 945 * 946 * Implements from rfc 821: TURN <CRLF> 947 * 948 * SMTP CODE SUCCESS: 250 949 * SMTP CODE FAILURE: 502 950 * SMTP CODE ERROR : 500, 503 951 * @access public 952 * @return bool 953 */ 954 function Turn() { 955 $this->error = array("error" => "This method, TURN, of the SMTP ". 956 "is not implemented"); 957 if($this->do_debug >= 1) { 958 echo "SMTP -> NOTICE: " . $this->error["error"] . $this->CRLF; 959 } 960 return false; 961 } 962 963 /** 964 * Verifies that the name is recognized by the server. 965 * Returns false if the name could not be verified otherwise 966 * the response from the server is returned. 967 * 968 * Implements rfc 821: VRFY <SP> <string> <CRLF> 969 * 970 * SMTP CODE SUCCESS: 250,251 971 * SMTP CODE FAILURE: 550,551,553 972 * SMTP CODE ERROR : 500,501,502,421 973 * @access public 974 * @return int 975 */ 976 function Verify($name) { 977 $this->error = null; # so no confusion is caused 978 979 if(!$this->connected()) { 980 $this->error = array( 981 "error" => "Called Verify() without being connected"); 982 return false; 983 } 984 985 fputs($this->smtp_conn,"VRFY " . $name . $this->CRLF); 986 987 $rply = $this->get_lines(); 988 $code = substr($rply,0,3); 989 990 if($this->do_debug >= 2) { 991 echo "SMTP -> FROM SERVER:" . $this->CRLF . $rply; 992 } 993 994 if($code != 250 && $code != 251) { 995 $this->error = 996 array("error" => "VRFY failed on name '$name'", 997 "smtp_code" => $code, 998 "smtp_msg" => substr($rply,4)); 999 if($this->do_debug >= 1) { 1000 echo "SMTP -> ERROR: " . $this->error["error"] . 1001 ": " . $rply . $this->CRLF; 1002 } 1003 return false; 1004 } 1005 return $rply; 1006 } 1007 1008 /******************************************************************* 1009 * INTERNAL FUNCTIONS * 1010 ******************************************************************/ 1011 1012 /** 1013 * Read in as many lines as possible 1014 * either before eof or socket timeout occurs on the operation. 1015 * With SMTP we can tell if we have more lines to read if the 1016 * 4th character is '-' symbol. If it is a space then we don't 1017 * need to read anything else. 1018 * @access private 1019 * @return string 1020 */ 1021 function get_lines() { 1022 $data = ""; 1023 while($str = fgets($this->smtp_conn,515)) { 1024 if($this->do_debug >= 4) { 1025 echo "SMTP -> get_lines(): \$data was \"$data\"" . 1026 $this->CRLF; 1027 echo "SMTP -> get_lines(): \$str is \"$str\"" . 1028 $this->CRLF; 1029 } 1030 $data .= $str; 1031 if($this->do_debug >= 4) { 1032 echo "SMTP -> get_lines(): \$data is \"$data\"" . $this->CRLF; 1033 } 1034 # if the 4th character is a space then we are done reading 1035 # so just break the loop 1036 if(substr($str,3,1) == " ") { break; } 1037 } 1038 return $data; 1039 } 1040 1041 } 1042 1043 1044 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Sun Feb 25 10:22:19 2007 | par Balluche grâce à PHPXref 0.7 |