SMTP PHP Code, for checking mail. Almost works

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
TelL83
Forum Newbie
Posts: 1
Joined: Thu Apr 22, 2010 5:38 pm

SMTP PHP Code, for checking mail. Almost works

Post by TelL83 »

Hello, I am working with this piece of code to check and display e-
mails from my STMP server. The way it is setup now just for testing,
is to retrieve and display the first 40 lines of the first un-read
message in a SMTP mailbox.

It works -- but the most critical element of it, the EMAIL address of
the sender & receiver, ... seems to be missing? I don't seem to be able
to understand what is wrong. I am hoping someone with a good eye can
see where I might not be setting things up correctly, or what
additional steps are needed for this information.

$MsgOne variable seems to contain almost the entire message except.
Line 6 or 7 of the test-echo below, shows FROM & TO fields, ... but no
e-mail addresses from the sender message? Is anyone else getting these
results? :)

The code below should run as is with no additional libraries, ... of
course with the necessary login details for testing will need to be
filled in. Can someone please help? :) Have a wonderful day!

<?php

global $timeout, $error, $buffer;

$timeout = 90;
$error = "";
$Count = -1;
$buffer = 512;
$server = "yoursmtpservername.com";

$msg_list_array = array();

$RFC1939 = true;

$login = 'smtp_username_here';
$pass = 'smtp_password_here';

set_time_limit($timeout);

$fp = connect ($server, $port = 110);

$Count = login($login,$pass, $fp);

for ($i=1; $i <= $Count; $i++){
set_time_limit($timeout);
$MsgOne = get($i, $fp);

// Just Check MESSAGE ONE in queue
$which=1;

IF ($i==$which)
message_details_view($MsgOne, $i, $fp);

}

quit($fp);

//-----------------------------------------------------------------------------
// Get the Message Details
//-----------------------------------------------------------------------------

function message_details_view($MsgOne, $msgNo, $fp)
{

$body = ''; // get
the body of the message into 1 variable
$subjects = ''; // get the subject of
the email
$dates = ''; // get
the date of the email
$body_start_key = true; // body starts at blank
line, blank line is separator for from headers to body
$TIRSFlag = false; // flag for seeing if
the email is really to be processed by the app
$base64Flag = false; // flag to handle base 64
encoding by email systems.

foreach ($MsgOne as $key => $value)
{

//if (trim($value) == "Content-Transfer-Encoding: base64"){
// $base64Flag = true;
//}//end if

//get the subject line of the email
if (strlen(stristr($value, "Subject"))>1){
$subjects = trim(stristr($value, " "));

//look for IncidentNo in the subject to see if we need to
attempt to process the email
if (strlen(stristr($subjects, "IncidentNo:"))>1){
$TIRSFlag = true;
}// end if
}// end if

//get the date of the email
if (strlen(stristr($value, "Date"))>1){
$dates = trim(stristr($value, " "));
$date_key = $key;
}// end if

//the body
if (strlen(trim($value))==0){
if ($body_start_key == false){ $body_start_key = true; } //
set the start key for the body
}//end if

if ($body_start_key == true){
$body .= trim($value);
if ($base64Flag == false){ $body .="<br />"; }
}// end if

}// end foreach

//if ($TIRSFlag == false){

//delete the message
//delete the message
//delete the message
//delete the message
//delete the message
//delete the message
//delete the message
//delete the message

//delete($msgNo, $fp);

//}else{

//decode the message if its base64 encoded
if ($base64Flag == true) {
$body = base64_decode($body);
}//end if

echo " 1| " . $MsgOne[0] . "<br>";
echo " 2| " . $MsgOne[1] . "<br>";
echo " 3| " . $MsgOne[2] . "<br>";
echo " 4| " . $MsgOne[3] . "<br>";
echo " 5| " . $MsgOne[4] . "<br>";
echo " 6| " . $MsgOne[5] . "<br>";
echo " 7| " . $MsgOne[6] . "<br>";
echo " 8| " . $MsgOne[7] . "<br>";
echo " 9| " . $MsgOne[8] . "<br>";
echo " 10| " . $MsgOne[9] . "<br>";
echo " 11| " . $MsgOne[10] . "<br>";
echo " 12| " . $MsgOne[11] . "<br>";
echo " 13| " . $MsgOne[12] . "<br>";
echo " 14| " . $MsgOne[13] . "<br>";
echo " 15| " . $MsgOne[14] . "<br>";
echo " 16| " . $MsgOne[15] . "<br>";
echo " 17| " . $MsgOne[16] . "<br>";
echo " 18| " . $MsgOne[17] . "<br>";
echo " 19| " . $MsgOne[18] . "<br>";
echo " 20| " . $MsgOne[19] . "<br>";
echo " 21| " . $MsgOne[20] . "<br>";
echo " 22| " . $MsgOne[21] . "<br>";
echo " 23| " . $MsgOne[22] . "<br>";
echo " 24| " . $MsgOne[23] . "<br>";
echo " 25| " . $MsgOne[24] . "<br>";
echo " 26| " . $MsgOne[25] . "<br>";
echo " 27| " . $MsgOne[26] . "<br>";
echo " 28| " . $MsgOne[27] . "<br>";
echo " 29| " . $MsgOne[28] . "<br>";
echo " 30| " . $MsgOne[29] . "<br>";
echo " 31| " . $MsgOne[30] . "<br>";
echo " 32| " . $MsgOne[31] . "<br>";
echo " 33| " . $MsgOne[32] . "<br>";
echo " 34| " . $MsgOne[33] . "<br>";
echo " 35| " . $MsgOne[34] . "<br>";
echo " 36| " . $MsgOne[35] . "<br>";
echo " 37| " . $MsgOne[36] . "<br>";
echo " 38| " . $MsgOne[37] . "<br>";
echo " 39| " . $MsgOne[38] . "<br>";
echo " 40| " . $MsgOne[39] . "<br>";

}// end function

//-----------------------------------------------------------------------------
// Connect To MailServer
//-----------------------------------------------------------------------------

function connect ($server, $port)
{
global $buffer;
// Opens a socket to the specified server. Unless
overridden,
// port defaults to 110. Returns true on success, false on
fail

// If MAILSERVER is set, override $server with it's value
$e_server = $server;

if(!$fp = fsockopen($e_server, $port, &$errno, &$errstr))
{
$error = "POP3 connect: Error [$errno] [$errstr]";
return false;
}

stream_set_blocking($fp,true);
update_timer();
$reply = fgets($fp,$buffer);
$reply = strip_clf($reply);
if(!is_ok($reply))
{
$error = "POP3 connect: Error [$reply]";
return false;
}

$BANNER = parse_banner($reply);
$RFC1939 = noop($fp);
if($RFC1939)
{
$error = "POP3: premature NOOP OK, NOT an RFC 1939
Compliant server";
quit($fp);
return false;
}
return $fp;
}// end function

function login ($login = "", $pass = "", $fp)
{
// Sends both user and pass. Returns # of msgs in mailbox or
// false on failure (or -1, if the error occurs while getting
// the number of messages.)

if(!user($login, $fp))
{
// Preserve the error generated by user()
return false;
}

$count = pass($pass, $fp);
if( (!$count) or ($count == -1) )
{
// Preserve the error generated by last() and pass()
return "-1";
}
return $count;
}// end function

function user ($user, $fp)
{
// Sends the USER command, returns true or false

if(empty($user))
{
$error = "POP3 user: no user id submitted";
return false;
}

$reply = send_cmd("USER $user", $fp);
if(!is_ok($reply))
{
$error = "POP3 user: Error [$reply]";
return false;
}
return true;
}// end function

function pass ($pass, $fp)
{
// Sends the PASS command, returns # of msgs in mailbox,
// returns false (undef) on Auth failure

if(empty($pass))
{
$error = "POP3 pass: no password submitted";
return false;
}

$reply = send_cmd("PASS $pass", $fp);
if(!is_ok($reply))
{
$error = "POP3 pass: authentication failed [$reply]";
quit($fp);
return false;
}
// Auth successful.
//echo "<br>User Authenticated<br>";
$count = last("count", $fp);
$COUNT = $count;
$RFC1939 = noop($fp);
if(!$RFC1939)
{
$error = "POP3 pass: NOOP failed. Server not RFC 1939
compliant";
quit($fp);
return false;
}
return $count;
}// end function

function last ( $type = "count", $fp )
{
// Returns the highest msg number in the mailbox.
// returns -1 on error, 0+ on success, if type != count
// results in a popstat() call (2 element array returned)

$last = -1;

$reply = send_cmd("STAT", $fp);
if(!is_ok($reply))
{
$error = "POP3 last: error [$reply]";
return $last;
}

$Vars = explode(" ",$reply);
$count = $Vars[1];
$size = $Vars[2];
settype($count,"integer");
settype($size,"integer");
if($type != "count")
{

return array($count,$size);
}
return $count;
}// end function

function get ($msgNum, $fp)
{
// Retrieve the specified msg number. Returns an array
// where each line of the msg is an array element.

global $buffer;
update_timer();

$buffer = $buffer;
$cmd = "RETR $msgNum";

$reply = send_cmd($cmd, $fp);

if(!is_ok($reply))
{
$error = "POP3 get: Error [$reply]";
return false;
}

$count = 0;
$MsgArray = array();

$line = fgets($fp,$buffer);
while ( !ereg("^\.\r\n",$line))
{
$MsgArray[$count] = $line;
$count++;
$line = fgets($fp,$buffer);
if(empty($line)) { break; }
}
return $MsgArray;
}// end function

function quit($fp)
{
// Closes the connection to the POP3 server, deleting
// any msgs marked as deleted.
global $buffer;

$cmd = "QUIT";
fwrite($fp,"$cmd\r\n");
$reply = fgets($fp,$buffer);
$reply = strip_clf($reply);
fclose($fp);
return true;
}// end function

function parse_banner ( $server_text )
{
$outside = true;
$banner = "";
$length = strlen($server_text);
for($count =0; $count < $length; $count++)
{
$digit = substr($server_text,$count,1);
if(!empty($digit))
{
if( (!$outside) and ($digit != '<') and ($digit !=
'>') )
{
$banner .= $digit;
}
if ($digit == '<')
{
$outside = false;
}
if($digit == '>')
{
$outside = true;
}
}
}
$banner = strip_clf($banner); // Just in case
return "<$banner>";
}// end function

function send_cmd ( $cmd, $fp )
{
// Sends a user defined command string to the
// POP server and returns the results. Useful for
// non-compliant or custom POP servers.
// Do NOT include the \r\n as part of your command
// string - it will be appended automatically.

// The return value is a standard fgets() call, which
// will read up to $buffer bytes of data, until it
// encounters a new line, or EOF, whichever happens first.

// This method works best if $cmd responds with only
// one line of data.
global $buffer;
if(!isset($fp))
{
$error = "POP3 send_cmd: No connection to server";
return false;
}

if(empty($cmd))
{
$error = "POP3 send_cmd: Empty command string";
return "";
}

$buffer = $buffer;
update_timer();
fwrite($fp,"$cmd\r\n");
$reply = fgets($fp,$buffer);
$reply = strip_clf($reply);
return $reply;
}// end function

function noop ($fp)
{

$cmd = "NOOP";
$reply = send_cmd($cmd, $fp);
if(!is_ok($reply))
{
return false;
}
return true;
}// end function

function strip_clf ($text = "")
{
// Strips \r\n from server responses

if(empty($text)) { return $text; }
$stripped = ereg_replace("\r","",$text);
$stripped = ereg_replace("\n","",$stripped);
return $stripped;
}// end function

function is_ok ($cmd = "")
{
// Return true or false on +OK or -ERR

if(empty($cmd)) { return false; }
if ( ereg ("^\+OK", $cmd ) ) { return true; }
return false;
}// end function

function update_timer()
{
global $timeout;
set_time_limit($timeout);

}

?>
Post Reply