0

I am about to start a project that requires an email to be somehow ran through a PHP script. I have full control of the server that the email will be sent to and wondered if people could give me some thoughts or pointers as to the most elegant way parsing it with PHP. I am not editing the email and then forwarding it on.

The server is CentOS with Exim email.

Thanks in advance.

beingalex
  • 2,416
  • 4
  • 32
  • 71
  • The best way to 'taste the waters' would be to look at open source email clients like [SquirrelMail](http://squirrelmail.org/) and see how they handle and implement what you seek. Chances are you will find an already implemented class. – Jack Brown Jun 26 '12 at 09:39

4 Answers4

1

You implement a client for the mail box (php imap modules can work with pop3, imap4 and local mailboxes). You poll the mailbox for new arrived messages, retrieve new ones and parse it. All using the php imap module.

arkascha
  • 41,620
  • 7
  • 58
  • 90
  • Thanks for the tip, I have found this page: http://tinsology.net/2009/04/accessing-email-through-imap-using-php/ which explains how to do this. – beingalex Jun 26 '12 at 10:28
1

One of the best examples of this comes with Wordpress... there is a file wp-mail.php which is set up as a cron task to retrieve and parse emails... I have hacked it up several times to do such things!

Brian
  • 8,418
  • 2
  • 25
  • 32
1

To make life easier for you, rather that stress yourself with parsing emails (if you don't mind the cost) you could decide to use Postmark

I've been using their services for quite some time and i love them. They have an Incoming email API service now. Enough talk, simply check it out because I believe it will help with what you're trying to do.

Emmanuel Okeke
  • 1,452
  • 15
  • 18
  • Thanks for the link, the service does look very interesting and I will keep it in mind should demand for us rises. – beingalex Jun 26 '12 at 10:27
0

There are three main approaches to this:

  1. Run a cron task to poll an IMAP/Pop3 server every x minutes
  2. Make exim run a script whenever it receives an email
  3. Use a third party service to receive the email and send it on to your site.

I wrote a Blog Post detailing the options, although it's for Rails the main concept applies to any language including PHP.

Steve Smith
  • 5,146
  • 1
  • 30
  • 31
  • 1
    I went with the cron and imap option as I like the php imap extension: http://www.php.net/manual/en/ref.imap.php – beingalex Jun 26 '12 at 16:10