Email to Database

If you are using cPanel then it is relatively easy to set up an email account that redirects any received emails directly into a table in a database. Of course once you know you can get the entire email source into a table you can then modify the script to do whatever you like with the email content.

First let's create a table in our database to store the emails in.

CREATE TABLE emailImages (
message text NOT NULL,

Next let's create a PHP script that can read the emails sent to it and store them in the database table we just created. Note that you may need to change the path in the first line if PHP is in a different location on your server. This first line is needed to process the rest as PHP as this script is effectively being run from the command line with nothing identifying it as PHP before the script starts to run.

$fd = fopen('php://stdin','r');
$email = '';
while (!feof ($fd)) {
$email .= fread($fd,1024);
$db = new mysqli('localhost', 'yourUserId', 'yourPassword', 'yourDatabase');
$stmt = $db->prepare('INSERT INTO emailImages (message) VALUES (?)');

Make sure that the script has permissions set to 700 so that it will actually have permission to run.

Our next step is to create the email account where we want the emails received to be dumped into the database table instead of being processed as emails. We don't need to give this email account very much space at all as once we carry out the final step to link all these things together there should never be any emails that actually end up in this email account. It does need to exist though so that we can use if for this process.

The final step for those using cPanel is to go to the "User-Level Filtering" option in the Mail section of cPanel.

Once there select the "Manage Filters" option next to the email account you just created. Now we create the filter that will redirect all emails sent to this email address into the script that inserts the email into the database. That name you give the filter doesn't really matter except that it will remind you just what this is for. In the rules section Select "To" and "contains" and then enter an @ in the third field. Since all emails sent to this email account will contain an @ that will ensure that all emails get processed by the filter. Now in Actions select "Pipe to a Program" and then enter the path to where you put the PHP script in the field next to it.

Note that the path is relative to the root folder of your account and not the public_html folder (the script is best placed somewhere outside of the public section of your site if you have an option to do so - for example the path might be /home/youraccountname/emailtodb.php

Now all that remains is to test the script by sending an email to that email account so as to make sure it works. Note that if there are any errors in your script the script will be unable to report them as it is running in the background. Instead it will just bounce any emails back telling you that "local delivery failed".


This article written by Stephen Chapman, Felgall Pty Ltd.

go to top

FaceBook Follow
Twitter Follow