How do I parse emails in realtime as they are recieved

别来无恙 提交于 2019-12-03 09:00:52

Yes, there is. You can pipe emails to your scripts.

Assuming you are using cPanel, follow this steps:

  • Log in to your cPanel.
  • Click on the Forwarders icon, under the Mail tab.
  • Click on the Add Forwarder button.
  • Fill in Address to Forward and put the mail address you would like to pipe the messages from.
  • Select Pipe to a Program and fill in the full path to the script which will handle the messages.

And here is a mail catcher example that sends received email to your other mail (just for demonstration):

#!/usr/bin/php -q
<?php

// read from stdin
$fd = fopen("php://stdin", "r");
$email = "";
while (!feof($fd))
{
    $email .= fread($fd, 1024);
}
fclose($fd);


mail('you@yoursite.com','From my email pipe!','"' . $email . '"');

?>

You would use a cron job if you wanted to do something at specific times. If you want to do something whenever an email arrives you need to tie your code into your email system. The usual way to do this is with Procmail (there is a recipe you can use (just read PHP for Perl/shell)).

I've been using the PECL extension mailparse on a website for a number of years now and it has been great.

I have all mail for a particular host get piped to a php script that uses mailparse to parse the message and insert it into the database, as well as process the attachments or multiple recipients.

They have an example file try.php in the download that was able to get me started.

Depending on what mail server you have the easiest thing to do would be to pipe incoming messages to your script like Quentin said. I use exim, and all I had to do was create a valiases file for my domain that looks like this: *: "|/home/site/process_mail.php" and from there mailparse does most of the hard work and I deal with the message and add it to the database.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!