开发者

How to programmatically capture emails? (like posterous)

I need to programmatically capture emails as well as any files that are attached to them using php. Also is r开发者_开发问答unning a cron job the only way to continue checking if there are any new emails or is there a way to automatically fire some code as a new email arrives? Thanks any help is appreciated!


To the second part of your question: If you run your own mail server and want to avoid polling to fetch new messages, then you can add an entry to /etc/aliases that lets your MTA know to forward to your PHP script, like so:

uploads: "|/usr/bin/php -q /var/flickr/uploads.php"

This entry will tell your MTA to pipe any emails for uploads@example.com to uploads.php. From there, you can read STDIN, parse the MIME message, and processes it as you please

(stolen from Cal Henderson's Book Building Scalable Websites. I highly recommend Chapter 6)


I have an application that does exactly that.

  1. Create a daemon (regularly executing program), called InboxDownloader that connects to your inbox and checks for messages. If there are messages, have that daemon just download them and place them into a folder for further processing.
  2. Create another daemon MailProcessor, that goes through the downloaded files and parses them out. The way I go about it is using regular-expression based signatures to determine what kind of message arrived. I have a set of classes that each register as a potential "handler" for this type of a message, and if the match is made, the handlers "execute logic" method is executed, and the full contents of the method are passed to it.


Did you look at how Postie does it for WordPress?

See also the Fetchmail tool.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜