topical media & game development
professional-ajax-09-AjaxMail-inc-phpmailer-class.smtp.php / php
<?php
/////////////////////////////////////////////////
// SMTP - PHP SMTP class
//
// Version 1.02
//
// Define an SMTP class that can be used to connect
// and communicate with any SMTP server. It implements
// all the SMTP functions defined in RFC821 except TURN.
//
// Author: Chris Ryan
//
// License: LGPL, see LICENSE
/////////////////////////////////////////////////
SMTP is rfc 821 compliant and implements all the rfc 821 SMTP
commands except TURN which will always return a not implemented
error. SMTP also provides some utility methods for sending mail
to an SMTP server.
@package PHPMailer
author: Chris Ryan
class SMTP
{
SMTP server port
@var int
var
SMTP reply line ending
@var string
var CRLF = "\r\n";
Sets whether debugging is turned on
@var bool
var
#@+
@access private
var smtp_conn; # the socket to the server
var helo_rply; # the reply the server sent to us for HELO
#@-
Initialize the class so that the data is in a known state.
@access public
returns: void
function SMTP() {
this->error = null;
this->do_debug = 0;
}
***********************************************************
CONNECTION FUNCTIONS *
*********************************************************
Connect to the server specified on the port specified.
If the port is not specified use the default SMTP_PORT.
If tval is specified then a connection will try and be
established with the server for that number of seconds.
If tval is not specified the default is 30 seconds to
try on the connection.
SMTP CODE SUCCESS: 220
SMTP CODE FAILURE: 421
@access public
returns: bool
function Connect(port=0,this->error = null;
# make sure we are __not__ connected
if(this->error =
array("error" => "Already connected to a server");
return false;
}
if(empty(port = this->smtp_conn = fsockopen(port, # the port to use
errstr, # error message if any
this->smtp_conn)) {
errno,
"errstr" => this->do_debug >= 1) {
echo "SMTP -> ERROR: " . errstr (this->CRLF;
}
return false;
}
# sometimes the SMTP server takes a little longer to respond
# so we will give it a longer timeout for the first read
// Windows still does not have support for this timeout function
if(substr(PHP_OS, 0, 3) != "WIN")
socket_set_timeout(tval, 0);
# get any announcement stuff
this->get_lines();
# set the timeout of any socket functions at 1/10 of a second
//if(function_exists("socket_set_timeout"))
// socket_set_timeout(
if(this->do_debug >= 2) {
echo "SMTP -> FROM SERVER:" . announce;
}
return true;
}
Performs SMTP authentication. Must be run after running the
Hello() method. Returns true if successfully authenticated.
@access public
returns: bool
function Authenticate(password) {
// Start authentication
fputs(this->CRLF);
this->get_lines();
rply,0,3);
if(this->error =
array("error" => "AUTH not accepted from server",
"smtp_code" => rply,4));
if(this->error["error"] .
": " . this->CRLF;
}
return false;
}
// Send encoded username
fputs(username) . rply = code = substr(code != 334) {
code,
"smtp_msg" => substr(this->do_debug >= 1) {
echo "SMTP -> ERROR: " . rply . this->smtp_conn, base64_encode(this->CRLF);
this->get_lines();
rply,0,3);
if(this->error =
array("error" => "Password not accepted from server",
"smtp_code" => rply,4));
if(this->error["error"] .
": " . this->CRLF;
}
return false;
}
return true;
}
Returns true if connected to a server otherwise false
@access private
returns: bool
function Connected() {
if(!empty(sock_status = socket_get_status(sock_status["eof"]) {
# hmm this is an odd situation... the socket is
# valid but we aren't connected anymore
if(this->CRLF .
"EOF caught while checking if connected";
}
Closes the socket and cleans up the state of the class.
It is not considered good to use this function without
first trying to use QUIT.
@access public
returns: void
function Close() {
this->error = null; # so there is no confusion
this->smtp_conn)) {
# close the connection and cleanup
fclose(this->smtp_conn = 0;
}
}
*************************************************************
SMTP COMMANDS *
***********************************************************
Issues a data command and sends the msg_data to the server
finializing the mail transaction.
function Data(msg_data) {
this->connected()) {
this->smtp_conn,"DATA" . rply = code = substr(this->do_debug >= 2) {
echo "SMTP -> FROM SERVER:" . rply;
}
if(this->error =
array("error" => "DATA command not accepted from server",
"smtp_code" => rply,4));
if(this->error["error"] .
": " . this->CRLF;
}
return false;
}
# the server is ready to accept data!
# according to rfc 821 we should not send more than 1000
# including the CRLF
# 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
# each of those into smaller lines to fit within the limit.
# in addition we will be looking for lines that start with
# a period '.' and append and additional period '.' to that
# line. NOTE: this does not count towards are limit.
# normalize the line breaks so we know the explode works
msg_data);
msg_data);
msg_data);
# we need to find a good way to determine is headers are
# in the msg_data or if it is a straight msg body
# currently I'm 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.
lines[0],0,strpos(in_headers = false;
if(!empty(field," ")) {
max_line_length = 998; # used below; set here for ease in change
while(list(,lines)) {
line == "" && in_headers = false;
}
# ok we need to break this line up into several
# smaller lines
while(strlen(max_line_length) {
line,0,pos) {
max_line_length - 1;
}
line,0,line = substr(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(line = "\t" . lines_out[] = line_out) = @each(line_out) > 0)
{
if(substr(line_out = "." . this->smtp_conn,this->CRLF);
}
}
# ok all the message data has been sent so lets get this
# over with aleady
fputs(this->CRLF . "." . rply = code = substr(this->do_debug >= 2) {
echo "SMTP -> FROM SERVER:" . rply;
}
if(this->error =
array("error" => "DATA not accepted from server",
"smtp_code" => rply,4));
if(this->error["error"] .
": " . this->CRLF;
}
return false;
}
return true;
}
Expand takes the name and asks the server to list all the
people who are members of the _list_. Expand will return
back and array of the result or false if an error occurs.
Each value in the array returned has the format of:
[ <full-name> <sp> ] <path>
The definition of <path> is defined in rfc 821
Implements rfc 821: EXPN <SP> <string> <CRLF>
SMTP CODE SUCCESS: 250
SMTP CODE FAILURE: 550
SMTP CODE ERROR : 500,501,502,504,421
@access public
returns: string array
function Expand(this->error = null; # so no confusion is caused
if(!this->error = array(
"error" => "Called Expand() without being connected");
return false;
}
fputs(name . rply = code = substr(this->do_debug >= 2) {
echo "SMTP -> FROM SERVER:" . rply;
}
if(this->error =
array("error" => "EXPN not accepted from server",
"smtp_code" => rply,4));
if(this->error["error"] .
": " . this->CRLF;
}
return false;
}
# parse the reply and place in our array to return to user
this->CRLF,l) = @each(list[] = substr(list;
}
Sends the HELO command to the smtp server.
This makes sure that we and the server are in
the same known state.
Implements from rfc 821: HELO <SP> <domain> <CRLF>
SMTP CODE SUCCESS: 250
SMTP CODE ERROR : 500, 501, 504, 421
@access public
returns: bool
function Hello(this->error = null; # so no confusion is caused
if(!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 = "localhost";
}
// Send extended hello first (RFC 2821)
if(!host))
{
if(!host))
return false;
}
return true;
}
Sends a HELO/EHLO command.
@access private
returns: bool
function SendHello(host) {
fputs(hello . " " . this->CRLF);
this->get_lines();
rply,0,3);
if(this->CRLF . code != 250) {
hello . " not accepted from server",
"smtp_code" => rply,4));
if(this->error["error"] .
": " . this->CRLF;
}
return false;
}
rply;
return true;
}
Gets help information on the keyword specified. If the keyword
is not specified then returns generic help, ussually contianing
A list of keywords that help is available on. This function
returns the results back to the user. It is up to the user to
handle the returned data. If an error occurs then false is
returned with
function Help(keyword="") {
this->connected()) {
extra = "";
if(!empty(extra = " " . this->smtp_conn,"HELP" . this->CRLF);
this->get_lines();
rply,0,3);
if(this->CRLF . code != 211 && this->error =
array("error" => "HELP not accepted from server",
"smtp_code" => rply,4));
if(this->error["error"] .
": " . this->CRLF;
}
return false;
}
return
Starts a mail transaction from the email address specified in
from. Returns true if successful or false otherwise. If True
the mail transaction is started and then one or more Recipient
commands may be called followed by a Data command.
Implements rfc 821: MAIL <SP> FROM:<reverse-path> <CRLF>
SMTP CODE SUCCESS: 250
SMTP CODE SUCCESS: 552,451,452
SMTP CODE SUCCESS: 500,501,421
@access public
returns: bool
function Mail(this->error = null; # so no confusion is caused
if(!this->error = array(
"error" => "Called Mail() without being connected");
return false;
}
fputs(from . ">" . rply = code = substr(this->do_debug >= 2) {
echo "SMTP -> FROM SERVER:" . rply;
}
if(this->error =
array("error" => "MAIL not accepted from server",
"smtp_code" => rply,4));
if(this->error["error"] .
": " . this->CRLF;
}
return false;
}
return true;
}
Sends the command NOOP to the SMTP server.
Implements from rfc 821: NOOP <CRLF>
SMTP CODE SUCCESS: 250
SMTP CODE ERROR : 500, 421
@access public
returns: bool
function Noop() {
this->connected()) {
this->smtp_conn,"NOOP" . rply = code = substr(this->do_debug >= 2) {
echo "SMTP -> FROM SERVER:" . rply;
}
if(this->error =
array("error" => "NOOP not accepted from server",
"smtp_code" => rply,4));
if(this->error["error"] .
": " . this->CRLF;
}
return false;
}
return true;
}
Sends the quit command to the server and then closes the socket
if there is no error or the
function Quit(close_on_error=true) {
this->connected()) {
this->smtp_conn,"quit" . byemsg = this->do_debug >= 2) {
echo "SMTP -> FROM SERVER:" . byemsg;
}
e = null;
byemsg,0,3);
if(this->error
code,
"smtp_rply" => substr(rval = false;
if(e["error"] . ": " .
this->CRLF;
}
}
if(empty(close_on_error) {
rval;
}
Sends the command RCPT to the SMTP server with the TO: argument of
function Recipient(to) {
this->connected()) {
this->smtp_conn,"RCPT TO:<" . this->CRLF);
this->get_lines();
rply,0,3);
if(this->CRLF . code != 250 && this->error =
array("error" => "RCPT not accepted from server",
"smtp_code" => rply,4));
if(this->error["error"] .
": " . this->CRLF;
}
return false;
}
return true;
}
Sends the RSET command to abort and transaction that is
currently in progress. Returns true if successful false
otherwise.
Implements rfc 821: RSET <CRLF>
SMTP CODE SUCCESS: 250
SMTP CODE ERROR : 500,501,504,421
@access public
returns: bool
function Reset() {
this->connected()) {
this->smtp_conn,"RSET" . rply = code = substr(this->do_debug >= 2) {
echo "SMTP -> FROM SERVER:" . rply;
}
if(this->error =
array("error" => "RSET failed",
"smtp_code" => rply,4));
if(this->error["error"] .
": " . this->CRLF;
}
return false;
}
return true;
}
Starts a mail transaction from the email address specified in
function Send(from) {
this->connected()) {
this->smtp_conn,"SEND FROM:" . this->CRLF);
this->get_lines();
rply,0,3);
if(this->CRLF . code != 250) {
code,
"smtp_msg" => substr(this->do_debug >= 1) {
echo "SMTP -> ERROR: " . rply .
Starts a mail transaction from the email address specified in
from. Returns true if successful or false otherwise. If True
the mail transaction is started and then one or more Recipient
commands may be called followed by a Data command. This command
will send the message to the users terminal if they are logged
in and send them an email.
Implements rfc 821: SAML <SP> FROM:<reverse-path> <CRLF>
SMTP CODE SUCCESS: 250
SMTP CODE SUCCESS: 552,451,452
SMTP CODE SUCCESS: 500,501,502,421
@access public
returns: bool
function SendAndMail(this->error = null; # so no confusion is caused
if(!this->error = array(
"error" => "Called SendAndMail() without being connected");
return false;
}
fputs(from . rply = code = substr(this->do_debug >= 2) {
echo "SMTP -> FROM SERVER:" . rply;
}
if(this->error =
array("error" => "SAML not accepted from server",
"smtp_code" => rply,4));
if(this->error["error"] .
": " . this->CRLF;
}
return false;
}
return true;
}
Starts a mail transaction from the email address specified in
function SendOrMail(from) {
this->connected()) {
this->smtp_conn,"SOML FROM:" . this->CRLF);
this->get_lines();
rply,0,3);
if(this->CRLF . code != 250) {
code,
"smtp_msg" => substr(this->do_debug >= 1) {
echo "SMTP -> ERROR: " . rply .
This is an optional command for SMTP that this class does not
support. This method is here to make the RFC821 Definition
complete for this class and __may__ be implimented in the future
Implements from rfc 821: TURN <CRLF>
SMTP CODE SUCCESS: 250
SMTP CODE FAILURE: 502
SMTP CODE ERROR : 500, 503
@access public
returns: bool
function Turn() {
this->error = array("error" => "This method, TURN, of the SMTP ".
"is not implemented");
if(this->error["error"] .
Verifies that the name is recognized by the server.
Returns false if the name could not be verified otherwise
the response from the server is returned.
Implements rfc 821: VRFY <SP> <string> <CRLF>
SMTP CODE SUCCESS: 250,251
SMTP CODE FAILURE: 550,551,553
SMTP CODE ERROR : 500,501,502,421
@access public
returns: int
function Verify(name) {
this->connected()) {
this->smtp_conn,"VRFY " . this->CRLF);
this->get_lines();
rply,0,3);
if(this->CRLF . code != 250 && this->error =
array("error" => "VRFY failed on name 'code,
"smtp_msg" => substr(this->do_debug >= 1) {
echo "SMTP -> ERROR: " . rply . rply;
}
*****************************************************************
INTERNAL FUNCTIONS *
****************************************************************
Read in as many lines as possible
either before eof or socket timeout occurs on the operation.
With SMTP we can tell if we have more lines to read if the
4th character is '-' symbol. If it is a space then we don't
need to read anything else.
@access private
returns: string
function get_lines() {
str = fgets(this->do_debug >= 4) {
echo "SMTP -> get_lines(): $data was \"this->CRLF;
echo "SMTP -> get_lines(): $str is \"this->CRLF;
}
str;
if(data\"" . str,3,1) == " ") { break; }
}
return
(C) Æliens
20/2/2008
You may not copy or print any of this material without explicit permission of the author or the publisher.
In case of other copyright issues, contact the author.