Attach Processing to Incoming Emails

I have a site set up that sends emails to members via a cron job reminding them to renew their membership. Some of these bounce back because the member no longer has the email address.

I have placed a header in the emails that can identify the member whose email bounced back.

What I would like to do is to set up processing on incoming emails that detects this particular header and automatically updates the member’s info to indicate that their email bounced.

I have created web pages and cron jobs using PHP but have never set up a PHP script to read incoming emails. I believe it is possible to run a script on incoming emails via an option in cPanel but I am not sure where or how to set up the script to process emails. I don’t want to interfere with other incoming emails so I’d prefer to have some idea of what I am doing before I start.

Does anyone have any advice on how to achieve this?

Steve (Former Advisor),

PHPClasses.org (the big brother to JSClasses.org) returned 44 potential hits on a search for “e-mail processing” of which only 5 appeared to be (partially) relevant to your question:

  1. PHP Log to File or Email: Log messages to files or send email messages
  2. Extract Name from Email Address: Extract person name from e-mail address header
  3. PHP Email To DB: Retrieve e-mail messages into a MySQL database {best - gross overkill but looks to be extremely useful}
  4. PHP Mail Log Parser: Filter mail server logs to extract email addresses
  5. Auto-Responder: Mail Autoresponder Class

A quick look at these class scripts shows how to examine incoming e-mail and parsing the headers for your embedded info should be as trivial as updating a MySQL database to delete your missing subscribers.

This IS an interesting problem … and could also be used to build and maintain a blacklist of spammer IP addresses, not just maintain your subscriber list!

Regards,

DK.

Thanks David,

I’ll take a look at those.

I am not looking to delete them as they are still members, just to flag the email address so as to not send them any more emails.

No problem, Steve,

I fully understand the db consequences (better to hold “expired” addresses).

As always, I learned something from this exercise, too. Not sure I’ll ever want my server to “have a play” with incoming e-mail but to know that I can is helpful. The simple fact that I can also collect spammer e-mail addresses, subject lines and other key words make this a potentially valuable anti-spammer tool.

Regards,

DK

Okay, I think I have a plan for testing this out. I’ll start with the PHP Email to DB script you referenced and a brand new email account. That way I can set it up so that only that new email address is included in the script - that way my existing emails are unaffected. Once I get it all working the way I want it I can then change the cron job that sends out the emails to use that new email address as the return to address for bounced emails so that any that do come back get fed into that script to be processed.

The club currently has about 1250 members of which about 950 have supplied an email address with about 45 of those having had an email returned as undeliverable. At the moment I manually examine the email headers of the returned email to locate the member number of the member it was sent to so I can manually update their membership details to indicate that the email bounced. For the 45% of members who login any with a bounce flag get a message to please fix their email address the next time they login.

Getting the bounce flag set automatically from the returned emails will save me a couple of minutes for each bounce. Also since I am offering the script for other clubs to use I don’t really want to be flagging bouced emails for them and don’t want them to have to do it themselves…

Okay, I couldn’t find where that PHP script got triggered so I decided to try to build my own. Initially the idea is to just capture the entire email and stick it in a database.

I have finally got it to the point where the email bouces back saying it can’t run the PHP.

errror message is:

pipe to |\\/home/felgall/bounced.php
    generated by bounced@amra.asn.au
    local delivery failed

PHP to be run is:

#!/usr/bin/php
<?php
$fd = fopen("php://stdin","r");
$email = '';
while (!feof ($fd)) {
  $email .= fread($fd,1024);
}
fclose($fd);
$db = new mysqli("localhost", "felgall_*****", "*****", "felgall_*****");
if (mysqli_connect_error()) {do_error('Connect Error ('.mysqli_connect_errno() .') '. mysqli_connect_error());}
$db->prepare("INSERT INTO emailImages (email) VALUES (?)");
$db->bind('S',"$email");
$db->execute();

I set up the email account giving it 20Mb just so if the processing didn’t work then at least there was somewhere for the email to be delivered to. I then set up the following filter on the email. Note that the reply address is just for the purposes of testing, I’ll figure out a more appropriate value later (preferably one that will capture all emails sent to the address).

Added a table to the database with one field called email that is a longtext field (so as to hopefully be plenty long enough for my email tests).

The above script was saved to the appropriate place and changed to 755 (as 644 resulted in bounces with insufficient access)

My best guess currently is that the path at the top of the PHP script is not correct but I have tried all the alternatives I can think of and it still doesn’t appear to be running. Unfortunately I also can’t think of any way to debug the script - writing to the database was put there as the only debugging I could think of.

Got rid of the \ on the front of the pipe name (not sure why the instructions said to put it there). Running the built in test of the filter returns:

The Filter has matched the following condition(s): 
$reply_address: contains webmaster

Return-path copied from sender
Sender      = felgall@server39.abstractdns.com
Recipient   = felgall@server39.abstractdns.com
Testing Exim filter file "/home/felgall/etc/amra.asn.au/bounced/filter"

Pipe message to: /home/felgall/bounced.php
Filtering set up at least one significant delivery or other action.
No other deliveries will occur.

Sending an email to the address gives the following error:

pipe to |/home/felgall/bounced.php
    generated by bounced@amra.asn.au
    local delivery failed

Finally fixed it - stupid errors in the database processing in the script.

Final PHP script that works:

#!/usr/local/bin/php
<?php

$fd = fopen('php://stdin','r');
$email = '';
while (!feof ($fd)) {
  $email .= fread($fd,1024);
}
fclose($fd);

$db = new mysqli('localhost', 'user', 'password', 'dbase');
$stmt = $db->prepare('INSERT INTO emailImages (email) VALUES (?)');
$stmt->bind_param('s',$email); 
$stmt->execute();
$stmt->close();

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.