Process ALL incoming e-mails with PHP script

419 views Asked by At

For a project I'm working on right now I want users to be able to send e-mails to [email protected]. Those e-mails or to be more specific ALL e-mails send to sub.domain.com shall be processed by a PHP script.

In that script I need to be able to get the username (the part in front of the @) as well as the e-mail header & body.

  1. Is it possible to do that without previously setting up e-mail aliases for every user?

  2. Is there a way to run the script on e-mail arrival? Or do I have to run a cron job to check periodically? I don't mind if the script is accessible through the web.

  3. From what I've read so far I assume I have to use either IMAP or POP3 for that, is that correct? I have no experience with either of them. Which one is better for this usecase?

  4. Anything else to keep in mind when doing this? Assume I have full control over the server and can change & install whatever is necessary.

Thanks in advance :)

Edit: @Dagon mentioned pipeing the e-mail to a script as an alternative to #3. How? I have never done anything like this before.

Edit2: All I want to do is hook a PHP script to all incoming e-mails. Is that too broad?

1

There are 1 answers

4
maaarghk On BEST ANSWER
  1. You should be able to alias @sub.domain.com into one mailbox, and the From: header of the email will contain username.

  2. You would need to either keep the script running or run the script in a cron job. An e-mail arriving couldn't trigger the script to run without introducing extra, unnecessary complexity.

  3. You are better to use IMAP, I think, literally only because the existing PHP libraries for IMAP are better than those for POP. In general, POP is an inferior protocol anyway.

  4. You might want to use something other than the username for the email address, such as a random string linked to a username. Otherwise if you knew someone's username you could impersonate them to the PHP script.