如何接收和解析电子邮件使用 POP3 和 PHP

March 1st, 2010 由管理员 发表评论 »

I would like to describe some methods on how to write the processor for incoming mail. I had to use such manipulation to parse e-mails received from various sources. This can be useful for writing your own spam filter system, answering machine or ticket system to receive applications by e-mail.

To implement the e-mail parser algorithm we need

  1. connect and log-on to e-mail server
  2. count the number of incoming letters
  3. recive e-mail from the server using POP3 protocol
  4. process the e-mail headers and body and make parsing
  5. implement any additional actions

Ok, there is very specific task for PHP coding, so we need hosting that supports external connection. I do not propose to write decision entirely because much has been realized by talented programmers already. 例如, you can take a ready module which will allow accept e-mails from a remote server.

Thank’s to Manuel Lemos and his module (php class) which named pop3.php.

To connect that class to your code, you just need to use include or require command: require(“pop3.php”);


hostname=$hostname;
$result=$pop3_connection->Open();
 
// We are trying to open connection and display the result
echo $result;
// Trying to logon and display the error if any appear
$error=$pop3_connection->Login($用户,$accesscode,$apop);
如果 ($错误<>'Password error: Logon failure: unknown user name or bad password.') {echo $error; exit;}
// Now get the statistic how many emails are stored and the size of them $result=$pop3_connection->Statistics($消息, $size);
回声 "$hostname contains  $消息 of $size bytes.";
 
//..... There we can receive e-mails in the cycle and parse them.... //
 
// If nothing to do - we can close the connection
$error=$pop3_connection->Close(); //
echo $error;
?>

Now we know how to connect and log-on to the POP3 server and how to request the number of Inbox e-mails and them sizes. Next, we should receive each e-mail and parse the headers and body array.

TO BE CONTINUED

广告
  1. gold 说:

    Great, that is working, what now? How to receive the e-mail? How to write e-mail parser? Continue please, great post!

  2. Waipahu locksmith 说:

    Fantastic Post. I have read many posts on this subject and you done the best job. Keep it up!

  3. web application technology 说:

    You can not believe just how long ive been googling because of this. Through seven pages of Digg results with out finding anything. 1 search on Msn. There this isGotta start using this more often ebrantley@kc.rr.com

留言

您必须是 登录 发表评论.