| [ 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 vtiger_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 vtiger_headers. Each header needs to be 256 * on a single line followed by a <CRLF> with the message vtiger_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 vtiger_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 vtiger_headers 319 # and if the first vtiger_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 # vtiger_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 341 # Patch to fix DOS attack 342 if(!$pos) { 343 $pos = $max_line_length - 1; 344 } 345 346 $lines_out[] = substr($line,0,$pos); 347 $line = substr($line,$pos + 1); 348 # if we are processing vtiger_headers we need to 349 # add a LWSP-char to the front of the new line 350 # rfc 822 on long msg vtiger_headers 351 if($in_headers) { 352 $line = "\t" . $line; 353 } 354 } 355 $lines_out[] = $line; 356 357 # now send the lines to the server 358 while(list(,$line_out) = @each($lines_out)) { 359 if(strlen($line_out) > 0) 360 { 361 if(substr($line_out, 0, 1) == ".") { 362 $line_out = "." . $line_out; 363 } 364 } 365 fputs($this->smtp_conn,$line_out . $this->CRLF); 366 } 367 } 368 369 # ok all the message data has been sent so lets get this 370 # over with aleady 371 fputs($this->smtp_conn, $this->CRLF . "." . $this->CRLF); 372 373 $rply = $this->get_lines(); 374 $code = substr($rply,0,3); 375 376 if($this->do_debug >= 2) { 377 echo "SMTP -> FROM SERVER:" . $this->CRLF . $rply; 378 } 379 380 if($code != 250) { 381 $this->error = 382 array("error" => "DATA not accepted from server", 383 "smtp_code" => $code, 384 "smtp_msg" => substr($rply,4)); 385 if($this->do_debug >= 1) { 386 echo "SMTP -> ERROR: " . $this->error["error"] . 387 ": " . $rply . $this->CRLF; 388 } 389 return false; 390 } 391 return true; 392 } 393 394 /** 395 * Expand takes the name and asks the server to list all the 396 * people who are members of the _list_. Expand will return 397 * back and array of the result or false if an error occurs. 398 * Each value in the array returned has the format of: 399 * [ <full-name> <sp> ] <path> 400 * The definition of <path> is defined in rfc 821 401 * 402 * Implements rfc 821: EXPN <SP> <string> <CRLF> 403 * 404 * SMTP CODE SUCCESS: 250 405 * SMTP CODE FAILURE: 550 406 * SMTP CODE ERROR : 500,501,502,504,421 407 * @access public 408 * @return string array 409 */ 410 function Expand($name) { 411 $this->error = null; # so no confusion is caused 412 413 if(!$this->connected()) { 414 $this->error = array( 415 "error" => "Called Expand() without being connected"); 416 return false; 417 } 418 419 fputs($this->smtp_conn,"EXPN " . $name . $this->CRLF); 420 421 $rply = $this->get_lines(); 422 $code = substr($rply,0,3); 423 424 if($this->do_debug >= 2) { 425 echo "SMTP -> FROM SERVER:" . $this->CRLF . $rply; 426 } 427 428 if($code != 250) { 429 $this->error = 430 array("error" => "EXPN not accepted from server", 431 "smtp_code" => $code, 432 "smtp_msg" => substr($rply,4)); 433 if($this->do_debug >= 1) { 434 echo "SMTP -> ERROR: " . $this->error["error"] . 435 ": " . $rply . $this->CRLF; 436 } 437 return false; 438 } 439 440 # parse the reply and place in our array to return to user 441 $entries = explode($this->CRLF,$rply); 442 while(list(,$l) = @each($entries)) { 443 $list[] = substr($l,4); 444 } 445 446 return $list; 447 } 448 449 /** 450 * Sends the HELO command to the smtp server. 451 * This makes sure that we and the server are in 452 * the same known state. 453 * 454 * Implements from rfc 821: HELO <SP> <domain> <CRLF> 455 * 456 * SMTP CODE SUCCESS: 250 457 * SMTP CODE ERROR : 500, 501, 504, 421 458 * @access public 459 * @return bool 460 */ 461 function Hello($host="") { 462 $this->error = null; # so no confusion is caused 463 464 if(!$this->connected()) { 465 $this->error = array( 466 "error" => "Called Hello() without being connected"); 467 return false; 468 } 469 470 # if a hostname for the HELO wasn't specified determine 471 # a suitable one to send 472 if(empty($host)) { 473 # we need to determine some sort of appopiate default 474 # to send to the server 475 $host = "localhost"; 476 } 477 478 // Send extended hello first (RFC 2821) 479 if(!$this->SendHello("EHLO", $host)) 480 { 481 if(!$this->SendHello("HELO", $host)) 482 return false; 483 } 484 485 return true; 486 } 487 488 /** 489 * Sends a HELO/EHLO command. 490 * @access private 491 * @return bool 492 */ 493 function SendHello($hello, $host) { 494 fputs($this->smtp_conn, $hello . " " . $host . $this->CRLF); 495 496 $rply = $this->get_lines(); 497 $code = substr($rply,0,3); 498 499 if($this->do_debug >= 2) { 500 echo "SMTP -> FROM SERVER: " . $this->CRLF . $rply; 501 } 502 503 if($code != 250) { 504 $this->error = 505 array("error" => $hello . " not accepted from server", 506 "smtp_code" => $code, 507 "smtp_msg" => substr($rply,4)); 508 if($this->do_debug >= 1) { 509 echo "SMTP -> ERROR: " . $this->error["error"] . 510 ": " . $rply . $this->CRLF; 511 } 512 return false; 513 } 514 515 $this->helo_rply = $rply; 516 517 return true; 518 } 519 520 /** 521 * Gets help information on the keyword specified. If the keyword 522 * is not specified then returns generic help, ussually contianing 523 * A list of keywords that help is available on. This function 524 * returns the results back to the user. It is up to the user to 525 * handle the returned data. If an error occurs then false is 526 * returned with $this->error set appropiately. 527 * 528 * Implements rfc 821: HELP [ <SP> <string> ] <CRLF> 529 * 530 * SMTP CODE SUCCESS: 211,214 531 * SMTP CODE ERROR : 500,501,502,504,421 532 * @access public 533 * @return string 534 */ 535 function Help($keyword="") { 536 $this->error = null; # to avoid confusion 537 538 if(!$this->connected()) { 539 $this->error = array( 540 "error" => "Called Help() without being connected"); 541 return false; 542 } 543 544 $extra = ""; 545 if(!empty($keyword)) { 546 $extra = " " . $keyword; 547 } 548 549 fputs($this->smtp_conn,"HELP" . $extra . $this->CRLF); 550 551 $rply = $this->get_lines(); 552 $code = substr($rply,0,3); 553 554 if($this->do_debug >= 2) { 555 echo "SMTP -> FROM SERVER:" . $this->CRLF . $rply; 556 } 557 558 if($code != 211 && $code != 214) { 559 $this->error = 560 array("error" => "HELP not accepted from server", 561 "smtp_code" => $code, 562 "smtp_msg" => substr($rply,4)); 563 if($this->do_debug >= 1) { 564 echo "SMTP -> ERROR: " . $this->error["error"] . 565 ": " . $rply . $this->CRLF; 566 } 567 return false; 568 } 569 570 return $rply; 571 } 572 573 /** 574 * Starts a mail transaction from the email address specified in 575 * $from. Returns true if successful or false otherwise. If True 576 * the mail transaction is started and then one or more Recipient 577 * commands may be called followed by a Data command. 578 * 579 * Implements rfc 821: MAIL <SP> FROM:<reverse-path> <CRLF> 580 * 581 * SMTP CODE SUCCESS: 250 582 * SMTP CODE SUCCESS: 552,451,452 583 * SMTP CODE SUCCESS: 500,501,421 584 * @access public 585 * @return bool 586 */ 587 function Mail($from) { 588 $this->error = null; # so no confusion is caused 589 590 if(!$this->connected()) { 591 $this->error = array( 592 "error" => "Called Mail() without being connected"); 593 return false; 594 } 595 596 fputs($this->smtp_conn,"MAIL FROM:<" . $from . ">" . $this->CRLF); 597 598 $rply = $this->get_lines(); 599 $code = substr($rply,0,3); 600 601 if($this->do_debug >= 2) { 602 echo "SMTP -> FROM SERVER:" . $this->CRLF . $rply; 603 } 604 605 if($code != 250) { 606 $this->error = 607 array("error" => "MAIL not accepted from server", 608 "smtp_code" => $code, 609 "smtp_msg" => substr($rply,4)); 610 if($this->do_debug >= 1) { 611 echo "SMTP -> ERROR: " . $this->error["error"] . 612 ": " . $rply . $this->CRLF; 613 } 614 return false; 615 } 616 return true; 617 } 618 619 /** 620 * Sends the command NOOP to the SMTP server. 621 * 622 * Implements from rfc 821: NOOP <CRLF> 623 * 624 * SMTP CODE SUCCESS: 250 625 * SMTP CODE ERROR : 500, 421 626 * @access public 627 * @return bool 628 */ 629 function Noop() { 630 $this->error = null; # so no confusion is caused 631 632 if(!$this->connected()) { 633 $this->error = array( 634 "error" => "Called Noop() without being connected"); 635 return false; 636 } 637 638 fputs($this->smtp_conn,"NOOP" . $this->CRLF); 639 640 $rply = $this->get_lines(); 641 $code = substr($rply,0,3); 642 643 if($this->do_debug >= 2) { 644 echo "SMTP -> FROM SERVER:" . $this->CRLF . $rply; 645 } 646 647 if($code != 250) { 648 $this->error = 649 array("error" => "NOOP not accepted from server", 650 "smtp_code" => $code, 651 "smtp_msg" => substr($rply,4)); 652 if($this->do_debug >= 1) { 653 echo "SMTP -> ERROR: " . $this->error["error"] . 654 ": " . $rply . $this->CRLF; 655 } 656 return false; 657 } 658 return true; 659 } 660 661 /** 662 * Sends the quit command to the server and then closes the socket 663 * if there is no error or the $close_on_error argument is true. 664 * 665 * Implements from rfc 821: QUIT <CRLF> 666 * 667 * SMTP CODE SUCCESS: 221 668 * SMTP CODE ERROR : 500 669 * @access public 670 * @return bool 671 */ 672 function Quit($close_on_error=true) { 673 $this->error = null; # so there is no confusion 674 675 if(!$this->connected()) { 676 $this->error = array( 677 "error" => "Called Quit() without being connected"); 678 return false; 679 } 680 681 # send the quit command to the server 682 fputs($this->smtp_conn,"quit" . $this->CRLF); 683 684 # get any good-bye messages 685 $byemsg = $this->get_lines(); 686 687 if($this->do_debug >= 2) { 688 echo "SMTP -> FROM SERVER:" . $this->CRLF . $byemsg; 689 } 690 691 $rval = true; 692 $e = null; 693 694 $code = substr($byemsg,0,3); 695 if($code != 221) { 696 # use e as a tmp var cause Close will overwrite $this->error 697 $e = array("error" => "SMTP server rejected quit command", 698 "smtp_code" => $code, 699 "smtp_rply" => substr($byemsg,4)); 700 $rval = false; 701 if($this->do_debug >= 1) { 702 echo "SMTP -> ERROR: " . $e["error"] . ": " . 703 $byemsg . $this->CRLF; 704 } 705 } 706 707 if(empty($e) || $close_on_error) { 708 $this->Close(); 709 } 710 711 return $rval; 712 } 713 714 /** 715 * Sends the command RCPT to the SMTP server with the TO: argument of $to. 716 * Returns true if the recipient was accepted false if it was rejected. 717 * 718 * Implements from rfc 821: RCPT <SP> TO:<forward-path> <CRLF> 719 * 720 * SMTP CODE SUCCESS: 250,251 721 * SMTP CODE FAILURE: 550,551,552,553,450,451,452 722 * SMTP CODE ERROR : 500,501,503,421 723 * @access public 724 * @return bool 725 */ 726 function Recipient($to) { 727 $this->error = null; # so no confusion is caused 728 729 if(!$this->connected()) { 730 $this->error = array( 731 "error" => "Called Recipient() without being connected"); 732 return false; 733 } 734 735 fputs($this->smtp_conn,"RCPT TO:<" . $to . ">" . $this->CRLF); 736 737 $rply = $this->get_lines(); 738 $code = substr($rply,0,3); 739 740 if($this->do_debug >= 2) { 741 echo "SMTP -> FROM SERVER:" . $this->CRLF . $rply; 742 } 743 744 if($code != 250 && $code != 251) { 745 $this->error = 746 array("error" => "RCPT not accepted from server", 747 "smtp_code" => $code, 748 "smtp_msg" => substr($rply,4)); 749 if($this->do_debug >= 1) { 750 echo "SMTP -> ERROR: " . $this->error["error"] . 751 ": " . $rply . $this->CRLF; 752 } 753 return false; 754 } 755 return true; 756 } 757 758 /** 759 * Sends the RSET command to abort and transaction that is 760 * currently in progress. Returns true if successful false 761 * otherwise. 762 * 763 * Implements rfc 821: RSET <CRLF> 764 * 765 * SMTP CODE SUCCESS: 250 766 * SMTP CODE ERROR : 500,501,504,421 767 * @access public 768 * @return bool 769 */ 770 function Reset() { 771 $this->error = null; # so no confusion is caused 772 773 if(!$this->connected()) { 774 $this->error = array( 775 "error" => "Called Reset() without being connected"); 776 return false; 777 } 778 779 fputs($this->smtp_conn,"RSET" . $this->CRLF); 780 781 $rply = $this->get_lines(); 782 $code = substr($rply,0,3); 783 784 if($this->do_debug >= 2) { 785 echo "SMTP -> FROM SERVER:" . $this->CRLF . $rply; 786 } 787 788 if($code != 250) { 789 $this->error = 790 array("error" => "RSET failed", 791 "smtp_code" => $code, 792 "smtp_msg" => substr($rply,4)); 793 if($this->do_debug >= 1) { 794 echo "SMTP -> ERROR: " . $this->error["error"] . 795 ": " . $rply . $this->CRLF; 796 } 797 return false; 798 } 799 800 return true; 801 } 802 803 /** 804 * Starts a mail transaction from the email address specified in 805 * $from. Returns true if successful or false otherwise. If True 806 * the mail transaction is started and then one or more Recipient 807 * commands may be called followed by a Data command. This command 808 * will send the message to the vtiger_users terminal if they are logged 809 * in. 810 * 811 * Implements rfc 821: SEND <SP> FROM:<reverse-path> <CRLF> 812 * 813 * SMTP CODE SUCCESS: 250 814 * SMTP CODE SUCCESS: 552,451,452 815 * SMTP CODE SUCCESS: 500,501,502,421 816 * @access public 817 * @return bool 818 */ 819 function Send($from) { 820 $this->error = null; # so no confusion is caused 821 822 if(!$this->connected()) { 823 $this->error = array( 824 "error" => "Called Send() without being connected"); 825 return false; 826 } 827 828 fputs($this->smtp_conn,"SEND FROM:" . $from . $this->CRLF); 829 830 $rply = $this->get_lines(); 831 $code = substr($rply,0,3); 832 833 if($this->do_debug >= 2) { 834 echo "SMTP -> FROM SERVER:" . $this->CRLF . $rply; 835 } 836 837 if($code != 250) { 838 $this->error = 839 array("error" => "SEND not accepted from server", 840 "smtp_code" => $code, 841 "smtp_msg" => substr($rply,4)); 842 if($this->do_debug >= 1) { 843 echo "SMTP -> ERROR: " . $this->error["error"] . 844 ": " . $rply . $this->CRLF; 845 } 846 return false; 847 } 848 return true; 849 } 850 851 /** 852 * Starts a mail transaction from the email address specified in 853 * $from. Returns true if successful or false otherwise. If True 854 * the mail transaction is started and then one or more Recipient 855 * commands may be called followed by a Data command. This command 856 * will send the message to the vtiger_users terminal if they are logged 857 * in and send them an email. 858 * 859 * Implements rfc 821: SAML <SP> FROM:<reverse-path> <CRLF> 860 * 861 * SMTP CODE SUCCESS: 250 862 * SMTP CODE SUCCESS: 552,451,452 863 * SMTP CODE SUCCESS: 500,501,502,421 864 * @access public 865 * @return bool 866 */ 867 function SendAndMail($from) { 868 $this->error = null; # so no confusion is caused 869 870 if(!$this->connected()) { 871 $this->error = array( 872 "error" => "Called SendAndMail() without being connected"); 873 return false; 874 } 875 876 fputs($this->smtp_conn,"SAML FROM:" . $from . $this->CRLF); 877 878 $rply = $this->get_lines(); 879 $code = substr($rply,0,3); 880 881 if($this->do_debug >= 2) { 882 echo "SMTP -> FROM SERVER:" . $this->CRLF . $rply; 883 } 884 885 if($code != 250) { 886 $this->error = 887 array("error" => "SAML not accepted from server", 888 "smtp_code" => $code, 889 "smtp_msg" => substr($rply,4)); 890 if($this->do_debug >= 1) { 891 echo "SMTP -> ERROR: " . $this->error["error"] . 892 ": " . $rply . $this->CRLF; 893 } 894 return false; 895 } 896 return true; 897 } 898 899 /** 900 * Starts a mail transaction from the email address specified in 901 * $from. Returns true if successful or false otherwise. If True 902 * the mail transaction is started and then one or more Recipient 903 * commands may be called followed by a Data command. This command 904 * will send the message to the vtiger_users terminal if they are logged 905 * in or mail it to them if they are not. 906 * 907 * Implements rfc 821: SOML <SP> FROM:<reverse-path> <CRLF> 908 * 909 * SMTP CODE SUCCESS: 250 910 * SMTP CODE SUCCESS: 552,451,452 911 * SMTP CODE SUCCESS: 500,501,502,421 912 * @access public 913 * @return bool 914 */ 915 function SendOrMail($from) { 916 $this->error = null; # so no confusion is caused 917 918 if(!$this->connected()) { 919 $this->error = array( 920 "error" => "Called SendOrMail() without being connected"); 921 return false; 922 } 923 924 fputs($this->smtp_conn,"SOML FROM:" . $from . $this->CRLF); 925 926 $rply = $this->get_lines(); 927 $code = substr($rply,0,3); 928 929 if($this->do_debug >= 2) { 930 echo "SMTP -> FROM SERVER:" . $this->CRLF . $rply; 931 } 932 933 if($code != 250) { 934 $this->error = 935 array("error" => "SOML not accepted from server", 936 "smtp_code" => $code, 937 "smtp_msg" => substr($rply,4)); 938 if($this->do_debug >= 1) { 939 echo "SMTP -> ERROR: " . $this->error["error"] . 940 ": " . $rply . $this->CRLF; 941 } 942 return false; 943 } 944 return true; 945 } 946 947 /** 948 * This is an optional command for SMTP that this class does not 949 * support. This method is here to make the RFC821 Definition 950 * complete for this class and __may__ be implimented in the future 951 * 952 * Implements from rfc 821: TURN <CRLF> 953 * 954 * SMTP CODE SUCCESS: 250 955 * SMTP CODE FAILURE: 502 956 * SMTP CODE ERROR : 500, 503 957 * @access public 958 * @return bool 959 */ 960 function Turn() { 961 $this->error = array("error" => "This method, TURN, of the SMTP ". 962 "is not implemented"); 963 if($this->do_debug >= 1) { 964 echo "SMTP -> NOTICE: " . $this->error["error"] . $this->CRLF; 965 } 966 return false; 967 } 968 969 /** 970 * Verifies that the name is recognized by the server. 971 * Returns false if the name could not be verified otherwise 972 * the response from the server is returned. 973 * 974 * Implements rfc 821: VRFY <SP> <string> <CRLF> 975 * 976 * SMTP CODE SUCCESS: 250,251 977 * SMTP CODE FAILURE: 550,551,553 978 * SMTP CODE ERROR : 500,501,502,421 979 * @access public 980 * @return int 981 */ 982 function Verify($name) { 983 $this->error = null; # so no confusion is caused 984 985 if(!$this->connected()) { 986 $this->error = array( 987 "error" => "Called Verify() without being connected"); 988 return false; 989 } 990 991 fputs($this->smtp_conn,"VRFY " . $name . $this->CRLF); 992 993 $rply = $this->get_lines(); 994 $code = substr($rply,0,3); 995 996 if($this->do_debug >= 2) { 997 echo "SMTP -> FROM SERVER:" . $this->CRLF . $rply; 998 } 999 1000 if($code != 250 && $code != 251) { 1001 $this->error = 1002 array("error" => "VRFY failed on name '$name'", 1003 "smtp_code" => $code, 1004 "smtp_msg" => substr($rply,4)); 1005 if($this->do_debug >= 1) { 1006 echo "SMTP -> ERROR: " . $this->error["error"] . 1007 ": " . $rply . $this->CRLF; 1008 } 1009 return false; 1010 } 1011 return $rply; 1012 } 1013 1014 /******************************************************************* 1015 * INTERNAL FUNCTIONS * 1016 ******************************************************************/ 1017 1018 /** 1019 * Read in as many lines as possible 1020 * either before eof or socket timeout occurs on the operation. 1021 * With SMTP we can tell if we have more lines to read if the 1022 * 4th character is '-' symbol. If it is a space then we don't 1023 * need to read anything else. 1024 * @access private 1025 * @return string 1026 */ 1027 function get_lines() { 1028 $data = ""; 1029 while($str = fgets($this->smtp_conn,515)) { 1030 if($this->do_debug >= 4) { 1031 echo "SMTP -> get_lines(): \$data was \"$data\"" . 1032 $this->CRLF; 1033 echo "SMTP -> get_lines(): \$str is \"$str\"" . 1034 $this->CRLF; 1035 } 1036 $data .= $str; 1037 if($this->do_debug >= 4) { 1038 echo "SMTP -> get_lines(): \$data is \"$data\"" . $this->CRLF; 1039 } 1040 # if the 4th character is a space then we are done reading 1041 # so just break the loop 1042 if(substr($str,3,1) == " ") { break; } 1043 } 1044 return $data; 1045 } 1046 1047 } 1048 1049 1050 ?>
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 |