You are here: RE: [PHP] Re: MIME E-mail message « PHP « IT news, forums, messages
RE: [PHP] Re: MIME E-mail message

Posted by "Oli Howson" on 10/21/22 11:35

>> Oh and cheers for the info someone gave me the other day, it inspired
>> me to just connect to the pop3 server using a raw socket, SOOOOO much
easier!!!
>share the code please, I'm interested :)

Ok... It's a bit rough and ready, never expected anyone else to want to look
at this (at least not yet!)

$host = 'mail.myserver.com';
$port = '110';
$user = 'me@myaddress.com';
$pass = 'itsasecret';

$socket = fsockopen($host,$port);

if (!$socket)
{
echo "Couldn't connect!";
}
else
{
//is it connected?
$res=getinfo($socket);
echo "Testing connection... ".$res;
echo "Send user...";
fputs($socket,"USER $user\n");
$res=getinfo($socket);
echo $res;
echo "Send pass...";
fputs($socket,"PASS $pass\n");
$res=getinfo($socket);
echo $res;
echo "Let's get the number of messages...";
fputs($socket,"STAT\n");
$res=getinfo($socket);
$res = split(' ',$res);
$res = $res[1];
echo trim($res)." messages\n";
if (trim($res) > 0)
{
echo "Let's get the array of messages...\n";
fputs($socket,"LIST\n");
$res=getinfo($socket);//gets rid of the OK
$res=getinfo($socket);
$res = split("\n",$res);
//print_r($res);

foreach ($res as $key => $value)
{
if ((trim($value) != '.') and (trim($value != "")))
{
$value = split(" ",$value);
$mids[] = $value[0];
}
}
//print_r($mids);

echo "Now we have a list of message IDs, we'll extract each message\nand
save under it's MID\n";
foreach ($mids as $key => $value)
{
echo "Requesting $value\n";
fputs($socket,"RETR $value\n");
$res=getinfo($socket);//gets rid of the OK
$res=getinfo($socket);
$res = split("\n",$res);
$cont = true;
foreach ($res as $key2 => $value2)
{
if ((trim($value) != '.') and ($cont == true))
{
$msg[] = $value2;
}
else
{
$cont = false;
}
}
$res = join("\n",$res);
echo "Saving $value\n";
file_put_contents('my/save/folder/'.$value.'.txt',$res);
}
}
echo "Finally, let's quit...";
fputs($socket,"QUIT\n");
$res=getinfo($socket);
echo $res;
}


function getinfo($socket)
{
$buff = '';
$continue = true;
while (!feof($socket) and ($continue==true))
{
$buffer = fgets($socket);
if (substr($buffer,0,4) == '-ERR')
{
$res = 'error';
$continue = false;
}
if (substr($buffer,0,3) == '+OK')
{
$res = 'ok';
$continue = false;
}
if (trim($buffer) == '.')
{
$res = 'end';
$continue = false;
}
$buff .= $buffer;
}
return $buff;
}

This was designed to run as a CLI app, may need tweaking to run through
http. Not fully tested, but does what I want!

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация