|
 ska mna - 2005-09-06 07:18:57
Hi,
I'm giving Manuel Lemos's Pop3 PHP class a go. I want to query a pop3 account and then store the email in a MySQL database. The bit I'm trying to do now is split the email headers into variables. I can see they are being read into an array, but I'm not up on email header information enough to know how to consitently say which one is the 'From:' line. So for example, if I just wanted the body, subject and from data, how would I extract that?
Tips, pointers much appreciated.
 Manuel Lemos - 2005-09-06 13:09:57 - In reply to message 1 from ska mna
Probably the easiest way is to check all header lines to get the header names line this:
$header_name=strtolower(strtok($header,":"));
if(!strcmp($header_name,"from"))
$from=trim(strtok(""));
 ska mna - 2005-09-07 20:08:25 - In reply to message 2 from Manuel Lemos
Thanks Manuel. That got me started. I ideally just want the actual email address, so I wrote this:
for($line=0;$line<count($headers);$line++)
{
preg_match_all('/From: ".*" <.*>/Uis',$headers[$line], $matches);
if (count($matches[0][0]==1))
{
preg_match_all('/<.*>/Uis',$matches[0][0], $emailmatch);
$emailmatch[0][0]=str_replace("<","",$emailmatch[0][0]);
$emailmatch[0][0]=str_replace(">","",$emailmatch[0][0]);
echo "<PRE>",HtmlSpecialChars($emailmatch[0][0]),"</PRE>\n";
}
}
(before i was getting, for example: From: My Name < [email protected]>)
I just want the [email protected] part. The above code does work, but it's not that robust. It shows you what I am trying to do though. Have you a better suggestion...? Much appreciated.
 ska mna - 2005-09-07 20:48:33 - In reply to message 3 from ska mna
update to the above code:
This is a bit better as it should be able to handle whether the From email address has a name prefix before the < [email protected]> part.
<?
preg_match_all('/From: .*>/Uis',$headers[$line], $matches);
if (count($matches[0][0]==1))
{
preg_match_all('/<.*>/Uis',$matches[0][0], $emailmatch);
$emailmatch[0][0]=str_replace("<","",$emailmatch[0][0]);
$emailmatch[0][0]=str_replace(">","",$emailmatch[0][0]);
echo "<PRE>",HtmlSpecialChars($emailmatch[0][0]),"</PRE>\n";
}
?>
This only seems to work for the first message when placed into the test_pop3 code though. How would I loop through all messages that have been received and display the From < [email protected]>....? I'll be working on it in the meantime to see if I can figure it out.
Thanks!
 ska mna - 2005-09-07 21:14:37 - In reply to message 4 from ska mna
He he, I think I'm getting there now actually. This loops through messages and displays the subject and email address:
<?
for($message=0; $message<=count($result); $message++){
if(($error=$pop3->RetrieveMessage($message,$headers,$body,2))=="")
{
for($line=0;$line<count($headers);$line++)
{
preg_match_all('/From: .*>/Uis',$headers[$line], $matches);
if (count($matches[0][0]==1))
{
preg_match_all('/<.*>/Uis',$matches[0][0], $emailmatch);
$emailmatch[0][0]=str_replace("<","",$emailmatch[0][0]); $emailmatch[0][0]=str_replace(">","",$emailmatch[0][0]);
echo "<PRE>",HtmlSpecialChars($emailmatch[0][0]),"</PRE>\n";
}
}
for($line=0;$line<count($body);$line++)
echo "<PRE>",HtmlSpecialChars($body[$line]),"</PRE>\n";
if(($error=$pop3->DeleteMessage(1))=="")
{
//echo "<PRE>Marked message 1 for deletion.</PRE>\n";
if(($error=$pop3->ResetDeletedMessages())=="")
{
//echo "<PRE>Resetted the list of messages to be deleted.</PRE>\n";
}
}
}
}
?>
Just posting this in case it helps someone else.
 Ronnel L. Aņasco - 2006-04-25 07:01:11 - In reply to message 2 from Manuel Lemos
hi there manuel,
i need your help.
my problem is, i am hosting my website in godaddy, and i am planning to use POP3 to get all my email messages, i used several examples of pop3 from this site, but i connot connect to the server...please give me a good example for pop3...thnx
by the way i am using php
 Manuel Lemos - 2006-04-26 02:02:06 - In reply to message 6 from Ronnel L. Aņasco
You need to try the test_pop3.php example, adjust the user, password and host variables, and tell me what is the error that it is showing.
 Viacheslav - 2007-01-14 04:33:47 - In reply to message 7 from Manuel Lemos
Hi! Excuse for mine English.
It does not turn out in any way to take the information from a field "Subject". So for example is present " Subject: Test "
I can not therefrom take and variable meaning(importance) "Test"
 Viacheslav - 2007-01-14 06:34:03 - In reply to message 8 from Viacheslav
And still question.
How to mark the letter, what it is already read?
 Viacheslav - 2007-01-14 16:20:49 - In reply to message 9 from Viacheslav
It can be made using IMAP?
|