If you are can't figure out the answer to your problem, I had to made an SMTP script by hand (don't ask) and it works just fine for sending mail here it is.
Code: Select all
<?php
/**************************************************************************************
*
* Title: SMTP Library
* Version: 1.1
* Author: Francesco DeSensi
*
* Version Notes:
* See file /docs/history.txt for further information
*
**************************************************************************************/
class SMTP
{
var $SMTP_PORT; // the default SMTP PORT
var $CRLF; // CRLF pair
var $smtp_conn; // the socket to the server
var $error; // error if any on the last call
var $helo_rply; // the reply the server sent to us for HELO
var $do_debug; // the level of debug to perform
var $usr_header; // allow user to specify header
var $confirm; // confirm a mailing has been sent
var $host; // host to be used to send
var $port; // smtp port
var $timeout; // set timeout for the connection
var $user; // set the user for the server
var $password; // set the user password for the server
/****************************************************************
* SMTP()
*
* Return:
* N/A
*
* Desc:
* This is the constructor
****************************************************************/
function SMTP()
{
//connection vars
$this->smtp_conn = 0;
$this->helo_rply = null;
$this->SMTP_PORT = 25;
$this->CRLF = "\r\n";
//debug information
$this->do_debug = 0;
$this->confirm = 0;
$this->error = null;
//connection information
$this->host = "";
$this->port = 25;
$this->timeout = 30;
$this->user = "";
$this->password = "";
}
/****************************************************************
* SendMail()
*
* Return:
* N/A
*
* Desc:
* Pull everthing together and send an email
****************************************************************/
function SendMail()
{
//Connect
$this->Connect( $this->host,$this->port,$this->timeout);
$this->Hello($this->host);
$this->Authenticate($this->user, $this->password);
$args = func_get_args();
$arg_count = count($args);
//user has provided the data
if( $arg_count == 3)
{
$to = $args[0];
$from = $args[1];
$data = $args[2];
//Mailing
$this->MailFrom($from);
$this->MailTo($to);
$this->Data($data);
}
elseif( $arg_count == 4 )
{
$to = $args[0];
$from = $args[1];
$subject = $args[2];
$message = $args[3];
//Mailing
$this->MailFrom($from);
$this->MailTo($to);
//Use the default header
$data = "To: $to\nFrom: $from\nMIME-Version: 1.0\nContent-type: text/html; charset=iso-8859-1\nSubject: $subject\n\n" . $message;
$this->Data($data);
}
else
{
if($this->do_debug >= 1 )
{
print("<br><br><strong>You have supplied an invalid number of arguments for SendMail()</strong><br>" .
"If you are providing your own data values use this format: SendMail([to],[from],[data])<br>" .
"If you are are using the standard usage use this format: SendMail([to],[from],[subject],[message])<br><br>");
}
$SendError = true;
}
//Cleanup
$this->Quit();
$this->Close();
//If confirm is set to 1 then print out a confirmation if it was
//sent or not
if($this->confirm == 1)
{
if( $SendError != true )
{
print("An email has been sent to: $to<br>");
}
else
{
print("The email was not sent as there are errors. If you do not see them set $do_debug=1");
}
}
}
/**********************************************************************************
*
* CONNECTION FUNCTIONS
*
* Functions to for connecting to and sending smtp servers
*
* Listing:
* - SMTP
* - Connect
*
* Note:
* SMTP CODE SUCCESS: 220
* SMTP CODE FAILURE: 421
**********************************************************************************/
/****************************************************************
* Connect ( $host, $port, $tval=30 )
*
* Return:
* boolean
*
* Desc:
* Connec to the specified host
****************************************************************/
function Connect( $host,$port=0,$tval=30 )
{
// Esnure there is no error handling confusion
$this->error = null;
// Make sure we are not already connected
if($this->connected())
{
$this->error =
array("error" => "Already connected to a server");
return false;
}
// If the port is empty then set it to the default
if(empty($port))
{
$port = $this->SMTP_PORT;
}
// Connect to the smtp server
$this->smtp_conn = fsockopen($host, $port, $errno, $errstr, $tval);
// Verify we connected properly, if not set up errors
if(empty($this->smtp_conn))
{
$this->error = array("error" => "Failed to connect to server",
"errno" => $errno,
"errstr" => $errstr);
if($this->do_debug >= 1)
{
echo "SMTP -> ERROR: " . $this->error["error"] .
": $errstr ($errno)" . $this->CRLF;
}
return false;
}
// The SMTP server may take a little longer to respond
// so we will give it a longer timeout for the first try
// NOTE: Windows still does not have support for this timeout
// function
if(substr(PHP_OS, 0, 3) != "WIN")
{
socket_set_timeout($this->smtp_conn, 1, 0);
}
// get any announcement stuff
$announce = $this->get_lines();
if($this->do_debug >= 2)
{
echo "SMTP -> FROM SERVER:" . $this->CRLF . $announce;
}
return true;
}
/****************************************************************
* Authenticate( $username, $password )
*
* Return:
* boolean
*
* Desc:
* Connec to the specified host
****************************************************************/
function Authenticate($username, $password)
{
// Start authentication
fputs($this->smtp_conn,"AUTH LOGIN" . $this->CRLF);
$rply = $this->get_lines();
$code = substr($rply,0,3);
if($code != 334)
{
$this->error =
array("error" => "AUTH 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;
}
// Send encoded username
fputs($this->smtp_conn, base64_encode($username) . $this->CRLF);
$rply = $this->get_lines();
$code = substr($rply,0,3);
if($code != 334)
{
$this->error =
array("error" => "Username 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;
}
// Send encoded password
fputs($this->smtp_conn, base64_encode($password) . $this->CRLF);
$rply = $this->get_lines();
$code = substr($rply,0,3);
if($code != 235)
{
$this->error =
array("error" => "Password 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;
}
return true;
}
/****************************************************************
* Connected()
*
* Return:
* boolean
*
* Desc:
* Determine if we a truly connected to the server. If not
* close the connection and clean up.
****************************************************************/
function Connected()
{
if(!empty($this->smtp_conn))
{
$sock_status = socket_get_status($this->smtp_conn);
if($sock_status["eof"])
{
// Socket is valid but we aren't connected anymore
if($this->do_debug >= 1)
{
echo "SMTP -> NOTICE:" . $this->CRLF .
"EOF caught while checking if connected";
}
$this->Close();
return false;
}
return true;
}
return false;
}
/****************************************************************
* Close()
*
* Return:
* N/A
*
* Desc:
* This will clean up the connection
****************************************************************/
function Close()
{
// Ensure there is no variable confusion
$this->error = null;
$this->helo_rply = null;
if(!empty($this->smtp_conn))
{
// close the connection and cleanup
fclose($this->smtp_conn);
$this->smtp_conn = 0;
}
}
/**********************************************************************************
*
* MAIL FUNCTIONS
*
* Functions to for connecting to and sending smtp servers
*
* Listing:
* - DATA
* - Reciepent
*
* Note:
* Implements rfc 821: DATA <CRLF>
*
* SMTP CODE INTERMEDIATE: 354
* [data]
* <CRLF>.<CRLF>
* SMTP CODE SUCCESS: 250
* SMTP CODE FAILURE: 552,554,451,452
* SMTP CODE FAILURE: 451,554
* SMTP CODE ERROR : 500,501,503,421
*
**********************************************************************************/
/****************************************************************
* Hello()
*
* Return:
* N/A
*
* Desc:
* Identify our client to the SMTP server
****************************************************************/
function Hello($host="")
{
$this->error = null;
if(!$this->connected())
{
$this->error = array(
"error" => "Called Hello() without being connected");
return false;
}
// if a hostname for the HELO wasn't specified determine
// a suitable one to send
if(empty($host))
{
// we need to determine some sort of appopiate default
// to send to the server
$host = "localhost";
}
fputs($this->smtp_conn,"HELO " . $host . $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 != 250)
{
$this->error =
array("error" => "HELO 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;
}
$this->helo_rply = $rply;
return true;
}
/****************************************************************
* Data()
*
* Return:
* N/A
*
* Desc:
* Accepts data and will clean up various elements to ensure
* proper sending.
****************************************************************/
function Data($msg_data)
{
// Ensure there is no confusion
$this->error = null;
if(!$this->connected())
{
$this->error = array(
"error" => "Called Data() without being connected");
return false;
}
fputs($this->smtp_conn,"DATA" . $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 != 354)
{
$this->error =
array("error" => "DATA command 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;
}
// 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",$msg_data);
$lines = explode("\n",$msg_data);
// Assuming rfc 822 definitions of msg headers
// and if the first field of the first line (':' sperated)
// does not contain a space then it _should_ be a header
// and we can process all lines before a blank "" line as
// headers.
$field = substr($lines[0],0,strpos($lines[0],":"));
$in_headers = false;
if(!empty($field) && !strstr($field," "))
{
$in_headers = true;
}
$max_line_length = 998; // used below; set here for ease in change
while(list(,$line) = @each($lines))
{
$lines_out = null;
if($line == "" && $in_headers)
{
$in_headers = false;
}
// ok we need to break this line up into several
// smaller lines
while(strlen($line) > $max_line_length)
{
$pos = strrpos(substr($line,0,$max_line_length)," ");
$lines_out[] = substr($line,0,$pos);
$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($in_headers)
{
$line = "\t" . $line;
}
}
$lines_out[] = $line;
// now send the lines to the server
while(list(,$line_out) = @each($lines_out))
{
if(strlen($line_out) > 0)
{
if(substr($line_out, 0, 1) == ".")
{
$line_out = "." . $line_out;
}
}
fputs($this->smtp_conn,$line_out . $this->CRLF);
}
}
// ok all the message data has been sent so lets get this
// over with aleady
fputs($this->smtp_conn, $this->CRLF . "." . $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 != 250)
{
$this->error =
array("error" => "DATA 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;
}
return true;
}
/****************************************************************
* MailFrom()
*
* Return:
* N/A
*
* Desc:
* Specify the MAIL FROM: clause
****************************************************************/
function MailFrom($from,$to,$message)
{
$this->error = null;
if(!$this->connected())
{
$this->error = array(
"error" => "Called Mail() without being connected");
return false;
}
$strDate = date();
fputs($this->smtp_conn,"MAIL From: postmaster@domain.com" . $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 != 250) {
$this->error =
array("error" => "MAIL 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;
}
return true;
}
/****************************************************************
* MailTo()
*
* Return:
* N/A
*
* Desc:
* Specify the RCPT TO: Clause
****************************************************************/
function MailTo($to)
{
$this->error = null;
if(!$this->connected()) {
$this->error = array(
"error" => "Called Recipient() without being connected");
return false;
}
fputs($this->smtp_conn,"RCPT TO:" . $to . $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 != 250 && $code != 251) {
$this->error =
array("error" => "RCPT 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;
}
return true;
}
/****************************************************************
* Quit()
*
* Return:
* N/A
*
* Desc:
* Send termniation to the server
****************************************************************/
function Quit($close_on_error=true)
{
$this->error = null;
if(!$this->connected()) {
$this->error = array(
"error" => "Called Quit() without being connected");
return false;
}
// send the quit command to the server
fputs($this->smtp_conn,"quit" . $this->CRLF);
// get any good-bye messages
$byemsg = $this->get_lines();
if($this->do_debug >= 2) {
echo "SMTP -> FROM SERVER:" . $this->CRLF . $byemsg;
}
$rval = true;
$e = null;
$code = substr($byemsg,0,3);
if($code != 221) {
// use e as a tmp var cause Close will overwrite $this->error
$e = array("error" => "SMTP server rejected quit command",
"smtp_code" => $code,
"smtp_rply" => substr($byemsg,4));
$rval = false;
if($this->do_debug >= 1) {
echo "SMTP -> ERROR: " . $e["error"] . ": " .
$byemsg . $this->CRLF;
}
}
if(empty($e) || $close_on_error) {
$this->Close();
}
return $rval;
}
/**********************************************************************************
*
* UTILITY FUNCTIONS
*
* Functions used by the SMTP functions to more easily handle
* information.
*
* Listing:
* - get_lines
*
**********************************************************************************/
/****************************************************************
* get_lines()
*
* Return:
* $data - value gathered from socket using fgets(...)
*
* Desc:
* Gather responses form the server using smtp_conn
****************************************************************/
function get_lines()
{
$data = "";
while($str = fgets($this->smtp_conn,515))
{
if($this->do_debug >= 4)
{
print("SMTP -> get_lines(): \$data was \"$data\"" . $this->CRLF . "<br>");
print("SMTP -> get_lines(): \$str is \"$str\"" . $this->CRLF . "<br>");
}
$data .= $str;
if($this->do_debug >= 4)
{
print("SMTP -> get_lines(): \$data is \"$data\"" . $this->CRLF . "<br>");
}
// if the 4th character is a space then we are done reading
// so just break the loop
if(substr($str,3,1) == " ")
{
break;
}
}
return $data;
}
}
?>