Software engineer, data guy, Open Source enthusiast, New Hampshire resident, husband, father. Fan of guitars, hiking, photography, homebrewing, sarcasm.
Configuring a server to parse email via a PHP script
In this tutorial I’ll show how you can setup a server to parse email with a PHP script. This tutorial assumes that your server is configured to receive email (I wrote this using a virtual machine running postfix).
The first thing you’ll need to do is configure an alias to direct email to a PHP script (instead of an email box). I added the following entry to the bottom of my /etc/aliases file and then ran the “newaliases” command to refresh my aliases database:
The above entry will pipe email sent to phpscript@MYDOMAIN to the designated PHP script.
And here’s the script:
At this point in the code, the body of the message will be contained in the $body variable and the headers will be in $headers.
Here is an example of the parsed headers (using print_r()):
Now you have all the email headers and message body parsed. You can do whatever your heart desires with the data, like insert it into a database or even create nodes!