importing the new class.smtp.php v2.1 for PHP5 and adapting eGroupWare specific changes

This commit is contained in:
Klaus Leithoff 2008-06-19 16:21:54 +00:00
parent 23c473690e
commit a43593c431

View File

@ -1,17 +1,28 @@
<?php <?php
//////////////////////////////////////////////////// /*~ class.smtp.php
// SMTP - PHP SMTP class .---------------------------------------------------------------------------.
// | Software: PHPMailer - PHP email class |
// Version 1.02 | Version: 2.1 |
// | Contact: via sourceforge.net support pages (also www.codeworxtech.com) |
// Define an SMTP class that can be used to connect | Info: http://phpmailer.sourceforge.net |
// and communicate with any SMTP server. It implements | Support: http://sourceforge.net/projects/phpmailer/ |
// all the SMTP functions defined in RFC821 except TURN. | ------------------------------------------------------------------------- |
// | Author: Andy Prevost (project admininistrator) |
// Author: Chris Ryan | Author: Brent R. Matzelle (original founder) |
// | Copyright (c) 2004-2007, Andy Prevost. All Rights Reserved. |
// License: LGPL, see LICENSE | Copyright (c) 2001-2003, Brent R. Matzelle |
//////////////////////////////////////////////////// | ------------------------------------------------------------------------- |
| License: Distributed under the Lesser General Public License (LGPL) |
| http://www.gnu.org/copyleft/lesser.html |
| This program is distributed in the hope that it will be useful - WITHOUT |
| ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| FITNESS FOR A PARTICULAR PURPOSE. |
| ------------------------------------------------------------------------- |
| We offer a number of paid services (www.codeworxtech.com): |
| - Web Hosting on highly optimized fast and secure servers |
| - Technology Consulting |
| - Oursourcing (highly qualified programmers and graphic designers) |
'---------------------------------------------------------------------------'
/** /**
* SMTP is rfc 821 compliant and implements all the rfc 821 SMTP * SMTP is rfc 821 compliant and implements all the rfc 821 SMTP
@ -21,32 +32,38 @@
* @package PHPMailer * @package PHPMailer
* @author Chris Ryan * @author Chris Ryan
*/ */
class SMTP
{ class SMTP {
/** /**
* SMTP server port * SMTP server port
* @var int * @var int
*/ */
var $SMTP_PORT = 25; public $SMTP_PORT = 25;
/** /**
* SMTP reply line ending * SMTP reply line ending
* @var string * @var string
*/ */
var $CRLF = "\r\n"; public $CRLF = "\r\n";
/** /**
* Sets whether debugging is turned on * Sets whether debugging is turned on
* @var bool * @var bool
*/ */
var $do_debug; # the level of debug to perform public $do_debug; // the level of debug to perform
/**
* Sets VERP use on/off (default is off)
* @var bool
*/
public $do_verp = false;
/**#@+ /**#@+
* @access private * @access private
*/ */
var $smtp_conn; # the socket to the server private $smtp_conn; // the socket to the server
var $error; # error if any on the last call private $error; // error if any on the last call
var $helo_rply; # the reply the server sent to us for HELO private $helo_rply; // the reply the server sent to us for HELO
/**#@-*/ /**#@-*/
/** /**
@ -54,7 +71,7 @@ class SMTP
* @access public * @access public
* @return void * @return void
*/ */
function SMTP() { public function __construct() {
$this->smtp_conn = 0; $this->smtp_conn = 0;
$this->error = null; $this->error = null;
$this->helo_rply = null; $this->helo_rply = null;
@ -79,17 +96,17 @@ class SMTP
* @access public * @access public
* @return bool * @return bool
*/ */
function Connect($host,$port=0,$tval=30) { public function Connect($host,$port=0,$tval=30) {
# set the error val to null so there is no confusion /* set the error val to null so there is no confusion */
$this->error = null; $this->error = null;
# make sure we are __not__ connected /* make sure we are __not__ connected */
if($this->connected()) { if($this->connected()) {
# ok we are connected! what should we do? /* ok we are connected! what should we do?
# for now we will just give an error saying we * for now we will just give an error saying we
# are already connected * are already connected
$this->error = */
array("error" => "Already connected to a server"); $this->error = array("error" => "Already connected to a server");
return false; return false;
} }
@ -97,13 +114,13 @@ class SMTP
$port = $this->SMTP_PORT; $port = $this->SMTP_PORT;
} }
#connect to the smtp server /* connect to the smtp server */
$this->smtp_conn = fsockopen($host, # the host of the server $this->smtp_conn = fsockopen($host, // the host of the server
$port, # the port to use $port, // the port to use
$errno, # error number if any $errno, // error number if any
$errstr, # error message if any $errstr, // error message if any
$tval); # give up after ? secs $tval); // give up after ? secs
# verify we connected properly /* verify we connected properly */
if(empty($this->smtp_conn)) { if(empty($this->smtp_conn)) {
$this->error = array("error" => "Failed to connect to server", $this->error = array("error" => "Failed to connect to server",
"errno" => $errno, "errno" => $errno,
@ -115,16 +132,17 @@ class SMTP
return false; return false;
} }
# sometimes the SMTP server takes a little longer to respond /* sometimes the SMTP server takes a little longer to respond
# so we will give it a longer timeout for the first read * so we will give it a longer timeout for the first read
// Windows still does not have support for this timeout function * - Windows still does not have support for this timeout function
*/
if(substr(PHP_OS, 0, 3) != "WIN") if(substr(PHP_OS, 0, 3) != "WIN")
socket_set_timeout($this->smtp_conn, $tval, 0); socket_set_timeout($this->smtp_conn, $tval, 0);
# get any announcement stuff /* get any announcement stuff */
$announce = $this->get_lines(); $announce = $this->get_lines();
# set the timeout of any socket functions at 1/10 of a second /* set the timeout of any socket functions at 1/10 of a second */
//if(function_exists("socket_set_timeout")) //if(function_exists("socket_set_timeout"))
// socket_set_timeout($this->smtp_conn, 0, 100000); // socket_set_timeout($this->smtp_conn, 0, 100000);
@ -135,13 +153,58 @@ class SMTP
return true; return true;
} }
/**
* Initiate a TSL communication with the server.
*
* SMTP CODE 220 Ready to start TLS
* SMTP CODE 501 Syntax error (no parameters allowed)
* SMTP CODE 454 TLS not available due to temporary reason
* @access public
* @return bool success
*/
public function StartTLS() {
$this->error = null; # to avoid confusion
if(!$this->connected()) {
$this->error = array("error" => "Called StartTLS() without being connected");
return false;
}
fputs($this->smtp_conn,"STARTTLS" . $extra . $this->CRLF);
$rply = $this->get_lines();
$code = substr($rply,0,3);
if($this->do_debug >= 2) {
echo "SMTP -> FROM SERVER:" . $this->CRLF . $rply;
}
if($code != 220) {
$this->error =
array("error" => "STARTTLS not accepted from server",
"smtp_code" => $code,
"smtp_msg" => substr($rply,4));
if($this->do_debug >= 1) {
echo "SMTP -> ERROR: " . $this->error["error"] . ": " . $rply . $this->CRLF;
}
return false;
}
//Begin encrypted connection
if(!stream_socket_enable_crypto($this->smtp_conn, true, STREAM_CRYPTO_METHOD_TLS_CLIENT)) {
return false;
}
return true;
}
/** /**
* Performs SMTP authentication. Must be run after running the * Performs SMTP authentication. Must be run after running the
* Hello() method. Returns true if successfully authenticated. * Hello() method. Returns true if successfully authenticated.
* @access public * @access public
* @return bool * @return bool
*/ */
function Authenticate($username, $password) { public function Authenticate($username, $password) {
// Start authentication // Start authentication
fputs($this->smtp_conn,"AUTH LOGIN" . $this->CRLF); fputs($this->smtp_conn,"AUTH LOGIN" . $this->CRLF);
@ -201,15 +264,15 @@ class SMTP
/** /**
* Returns true if connected to a server otherwise false * Returns true if connected to a server otherwise false
* @access private * @access public
* @return bool * @return bool
*/ */
function Connected() { public function Connected() {
if(!empty($this->smtp_conn)) { if(!empty($this->smtp_conn)) {
$sock_status = socket_get_status($this->smtp_conn); $sock_status = socket_get_status($this->smtp_conn);
if($sock_status["eof"]) { if($sock_status["eof"]) {
# hmm this is an odd situation... the socket is // hmm this is an odd situation... the socket is
# valid but we aren't connected anymore // valid but we are not connected anymore
if($this->do_debug >= 1) { if($this->do_debug >= 1) {
echo "SMTP -> NOTICE:" . $this->CRLF . echo "SMTP -> NOTICE:" . $this->CRLF .
"EOF caught while checking if connected"; "EOF caught while checking if connected";
@ -217,7 +280,7 @@ class SMTP
$this->Close(); $this->Close();
return false; return false;
} }
return true; # everything looks good return true; // everything looks good
} }
return false; return false;
} }
@ -229,17 +292,16 @@ class SMTP
* @access public * @access public
* @return void * @return void
*/ */
function Close() { public function Close() {
$this->error = null; # so there is no confusion $this->error = null; // so there is no confusion
$this->helo_rply = null; $this->helo_rply = null;
if(!empty($this->smtp_conn)) { if(!empty($this->smtp_conn)) {
# close the connection and cleanup // close the connection and cleanup
fclose($this->smtp_conn); fclose($this->smtp_conn);
$this->smtp_conn = 0; $this->smtp_conn = 0;
} }
} }
/*************************************************************** /***************************************************************
* SMTP COMMANDS * * SMTP COMMANDS *
*************************************************************/ *************************************************************/
@ -263,8 +325,8 @@ class SMTP
* @access public * @access public
* @return bool * @return bool
*/ */
function Data($msg_data) { public function Data($msg_data) {
$this->error = null; # so no confusion is caused $this->error = null; // so no confusion is caused
if(!$this->connected()) { if(!$this->connected()) {
$this->error = array( $this->error = array(
@ -293,63 +355,68 @@ class SMTP
return false; return false;
} }
# the server is ready to accept data! /* the server is ready to accept data!
# according to rfc 821 we should not send more than 1000 * according to rfc 821 we should not send more than 1000
# including the CRLF * including the CRLF
# characters on a single line so we will break the data up * characters on a single line so we will break the data up
# into lines by \r and/or \n then if needed we will break * into lines by \r and/or \n then if needed we will break
# each of those into smaller lines to fit within the limit. * each of those into smaller lines to fit within the limit.
# in addition we will be looking for lines that start with * in addition we will be looking for lines that start with
# a period '.' and append and additional period '.' to that * a period '.' and append and additional period '.' to that
# line. NOTE: this does not count towards are limit. * line. NOTE: this does not count towards are limit.
*/
# normalize the line breaks so we know the explode works // normalize the line breaks so we know the explode works
$msg_data = str_replace("\r\n","\n",$msg_data); $msg_data = str_replace("\r\n","\n",$msg_data);
$msg_data = str_replace("\r","\n",$msg_data); $msg_data = str_replace("\r","\n",$msg_data);
$lines = explode("\n",$msg_data); $lines = explode("\n",$msg_data);
# we need to find a good way to determine is headers are /* we need to find a good way to determine is headers are
# in the msg_data or if it is a straight msg body * in the msg_data or if it is a straight msg body
# currently I'm assuming rfc 822 definitions of msg headers * currently I am assuming rfc 822 definitions of msg headers
# and if the first field of the first line (':' sperated) * and if the first field of the first line (':' sperated)
# does not contain a space then it _should_ be a header * does not contain a space then it _should_ be a header
# and we can process all lines before a blank "" line as * and we can process all lines before a blank "" line as
# headers. * headers.
*/
$field = substr($lines[0],0,strpos($lines[0],":")); $field = substr($lines[0],0,strpos($lines[0],":"));
$in_headers = false; $in_headers = false;
if(!empty($field) && strpos($field," ") === false) { if(!empty($field) && strpos($field," ") === false) {
$in_headers = true; $in_headers = true;
} }
$max_line_length = 998; # used below; set here for ease in change $max_line_length = 998; // used below; set here for ease in change
while(list(,$line) = @each($lines)) { while(list(,$line) = @each($lines)) {
$lines_out = null; $lines_out = null;
if($line == "" && $in_headers) { if($line == "" && $in_headers) {
$in_headers = false; $in_headers = false;
} }
# ok we need to break this line up into several // ok we need to break this line up into several smaller lines
# smaller lines
while(strlen($line) > $max_line_length) { while(strlen($line) > $max_line_length) {
$pos = strrpos(substr($line,0,$max_line_length)," "); $pos = strrpos(substr($line,0,$max_line_length)," ");
# Patch to fix DOS attack // Patch to fix DOS attack
if(!$pos) { if(!$pos) {
$pos = $max_line_length - 1; $pos = $max_line_length - 1;
} $lines_out[] = substr($line,0,$pos);
$line = substr($line,$pos);
} else {
$lines_out[] = substr($line,0,$pos); $lines_out[] = substr($line,0,$pos);
$line = substr($line,$pos + 1); $line = substr($line,$pos + 1);
# if we are processing headers we need to }
# add a LWSP-char to the front of the new line
# rfc 822 on long msg headers /* if we are processing headers we need to
* add a LWSP-char to the front of the new line
* rfc 822 on long msg headers
*/
if($in_headers) { if($in_headers) {
$line = "\t" . $line; $line = "\t" . $line;
} }
} }
$lines_out[] = $line; $lines_out[] = $line;
# now send the lines to the server // now send the lines to the server
while(list(,$line_out) = @each($lines_out)) { while(list(,$line_out) = @each($lines_out)) {
if(strlen($line_out) > 0) if(strlen($line_out) > 0)
{ {
@ -361,13 +428,11 @@ class SMTP
} }
} }
# ok all the message data has been sent so lets get this // ok all the message data has been sent so lets get this
# over with aleady // over with aleady
fputs($this->smtp_conn, $this->CRLF . "." . $this->CRLF); fputs($this->smtp_conn, $this->CRLF . "." . $this->CRLF);
// get server response
# get server response
$rply = $this->get_lines(); $rply = $this->get_lines();
# if the server is slow try to get an answer within 30 seconds # if the server is slow try to get an answer within 30 seconds
$timeout_counter = 0; $timeout_counter = 0;
while(($rply=="") && ($timeout_counter<30)) while(($rply=="") && ($timeout_counter<30))
@ -389,7 +454,6 @@ class SMTP
} }
return false; return false;
} }
$code = substr($rply,0,3); $code = substr($rply,0,3);
if($this->do_debug >= 2) { if($this->do_debug >= 2) {
@ -426,8 +490,8 @@ class SMTP
* @access public * @access public
* @return string array * @return string array
*/ */
function Expand($name) { public function Expand($name) {
$this->error = null; # so no confusion is caused $this->error = null; // so no confusion is caused
if(!$this->connected()) { if(!$this->connected()) {
$this->error = array( $this->error = array(
@ -456,7 +520,7 @@ class SMTP
return false; return false;
} }
# parse the reply and place in our array to return to user // parse the reply and place in our array to return to user
$entries = explode($this->CRLF,$rply); $entries = explode($this->CRLF,$rply);
while(list(,$l) = @each($entries)) { while(list(,$l) = @each($entries)) {
$list[] = substr($l,4); $list[] = substr($l,4);
@ -477,8 +541,8 @@ class SMTP
* @access public * @access public
* @return bool * @return bool
*/ */
function Hello($host="") { public function Hello($host="") {
$this->error = null; # so no confusion is caused $this->error = null; // so no confusion is caused
if(!$this->connected()) { if(!$this->connected()) {
$this->error = array( $this->error = array(
@ -486,11 +550,11 @@ class SMTP
return false; return false;
} }
# if a hostname for the HELO wasn't specified determine // if a hostname for the HELO was not specified determine
# a suitable one to send //a suitable one to send
if(empty($host)) { if(empty($host)) {
# we need to determine some sort of appopiate default // we need to determine some sort of appopiate default
# to send to the server // to send to the server
$host = "localhost"; $host = "localhost";
} }
@ -509,7 +573,7 @@ class SMTP
* @access private * @access private
* @return bool * @return bool
*/ */
function SendHello($hello, $host) { private function SendHello($hello, $host) {
fputs($this->smtp_conn, $hello . " " . $host . $this->CRLF); fputs($this->smtp_conn, $hello . " " . $host . $this->CRLF);
$rply = $this->get_lines(); $rply = $this->get_lines();
@ -551,8 +615,8 @@ class SMTP
* @access public * @access public
* @return string * @return string
*/ */
function Help($keyword="") { public function Help($keyword="") {
$this->error = null; # to avoid confusion $this->error = null; // to avoid confusion
if(!$this->connected()) { if(!$this->connected()) {
$this->error = array( $this->error = array(
@ -603,8 +667,8 @@ class SMTP
* @access public * @access public
* @return bool * @return bool
*/ */
function Mail($from) { public function Mail($from) {
$this->error = null; # so no confusion is caused $this->error = null; // so no confusion is caused
if(!$this->connected()) { if(!$this->connected()) {
$this->error = array( $this->error = array(
@ -612,7 +676,8 @@ class SMTP
return false; return false;
} }
fputs($this->smtp_conn,"MAIL FROM:<" . $from . ">" . $this->CRLF); $useVerp = ($this->do_verp ? "XVERP" : "");
fputs($this->smtp_conn,"MAIL FROM:<" . $from . ">" . $useVerp . $this->CRLF);
$rply = $this->get_lines(); $rply = $this->get_lines();
$code = substr($rply,0,3); $code = substr($rply,0,3);
@ -645,8 +710,8 @@ class SMTP
* @access public * @access public
* @return bool * @return bool
*/ */
function Noop() { public function Noop() {
$this->error = null; # so no confusion is caused $this->error = null; // so no confusion is caused
if(!$this->connected()) { if(!$this->connected()) {
$this->error = array( $this->error = array(
@ -688,8 +753,8 @@ class SMTP
* @access public * @access public
* @return bool * @return bool
*/ */
function Quit($close_on_error=true) { public function Quit($close_on_error=true) {
$this->error = null; # so there is no confusion $this->error = null; // so there is no confusion
if(!$this->connected()) { if(!$this->connected()) {
$this->error = array( $this->error = array(
@ -697,10 +762,10 @@ class SMTP
return false; return false;
} }
# send the quit command to the server // send the quit command to the server
fputs($this->smtp_conn,"quit" . $this->CRLF); fputs($this->smtp_conn,"quit" . $this->CRLF);
# get any good-bye messages // get any good-bye messages
$byemsg = $this->get_lines(); $byemsg = $this->get_lines();
if($this->do_debug >= 2) { if($this->do_debug >= 2) {
@ -712,7 +777,7 @@ class SMTP
$code = substr($byemsg,0,3); $code = substr($byemsg,0,3);
if($code != 221) { if($code != 221) {
# use e as a tmp var cause Close will overwrite $this->error // use e as a tmp var cause Close will overwrite $this->error
$e = array("error" => "SMTP server rejected quit command", $e = array("error" => "SMTP server rejected quit command",
"smtp_code" => $code, "smtp_code" => $code,
"smtp_rply" => substr($byemsg,4)); "smtp_rply" => substr($byemsg,4));
@ -742,8 +807,8 @@ class SMTP
* @access public * @access public
* @return bool * @return bool
*/ */
function Recipient($to) { public function Recipient($to) {
$this->error = null; # so no confusion is caused $this->error = null; // so no confusion is caused
if(!$this->connected()) { if(!$this->connected()) {
$this->error = array( $this->error = array(
@ -786,8 +851,8 @@ class SMTP
* @access public * @access public
* @return bool * @return bool
*/ */
function Reset() { public function Reset() {
$this->error = null; # so no confusion is caused $this->error = null; // so no confusion is caused
if(!$this->connected()) { if(!$this->connected()) {
$this->error = array( $this->error = array(
@ -835,8 +900,8 @@ class SMTP
* @access public * @access public
* @return bool * @return bool
*/ */
function Send($from) { public function Send($from) {
$this->error = null; # so no confusion is caused $this->error = null; // so no confusion is caused
if(!$this->connected()) { if(!$this->connected()) {
$this->error = array( $this->error = array(
@ -883,8 +948,8 @@ class SMTP
* @access public * @access public
* @return bool * @return bool
*/ */
function SendAndMail($from) { public function SendAndMail($from) {
$this->error = null; # so no confusion is caused $this->error = null; // so no confusion is caused
if(!$this->connected()) { if(!$this->connected()) {
$this->error = array( $this->error = array(
@ -931,8 +996,8 @@ class SMTP
* @access public * @access public
* @return bool * @return bool
*/ */
function SendOrMail($from) { public function SendOrMail($from) {
$this->error = null; # so no confusion is caused $this->error = null; // so no confusion is caused
if(!$this->connected()) { if(!$this->connected()) {
$this->error = array( $this->error = array(
@ -976,7 +1041,7 @@ class SMTP
* @access public * @access public
* @return bool * @return bool
*/ */
function Turn() { public function Turn() {
$this->error = array("error" => "This method, TURN, of the SMTP ". $this->error = array("error" => "This method, TURN, of the SMTP ".
"is not implemented"); "is not implemented");
if($this->do_debug >= 1) { if($this->do_debug >= 1) {
@ -998,8 +1063,8 @@ class SMTP
* @access public * @access public
* @return int * @return int
*/ */
function Verify($name) { public function Verify($name) {
$this->error = null; # so no confusion is caused $this->error = null; // so no confusion is caused
if(!$this->connected()) { if(!$this->connected()) {
$this->error = array( $this->error = array(
@ -1043,9 +1108,9 @@ class SMTP
* @access private * @access private
* @return string * @return string
*/ */
function get_lines() { private function get_lines() {
$data = ""; $data = "";
while($str = fgets($this->smtp_conn,515)) { while($str = @fgets($this->smtp_conn,515)) {
if($this->do_debug >= 4) { if($this->do_debug >= 4) {
echo "SMTP -> get_lines(): \$data was \"$data\"" . echo "SMTP -> get_lines(): \$data was \"$data\"" .
$this->CRLF; $this->CRLF;
@ -1056,8 +1121,8 @@ class SMTP
if($this->do_debug >= 4) { if($this->do_debug >= 4) {
echo "SMTP -> get_lines(): \$data is \"$data\"" . $this->CRLF; echo "SMTP -> get_lines(): \$data is \"$data\"" . $this->CRLF;
} }
# if the 4th character is a space then we are done reading // if the 4th character is a space then we are done reading
# so just break the loop // so just break the loop
if(substr($str,3,1) == " ") { break; } if(substr($str,3,1) == " ") { break; }
} }
return $data; return $data;
@ -1065,4 +1130,5 @@ class SMTP
} }
?>
?>