Web Hosting Talk







View Full Version : Seeking Perl or PHP examples of email interception


dynamicnet
09-09-2009, 10:40 AM
Greetings:

RE: Seeking Perl or PHP examples of email interception on the mail server itself.

I've complete access to our mail servers, and I'm looking to write a small Perl or PHP application which can intercept an email to a specific email box, and from there do the following:

Determine if the email is plain text or HTML/MIME-based with or without an attachment.

Insert one to several lines into the email in such a way it does not break any content encoding.

We are using qmail/vpopmail, so I know how to use a .qmail forward file where I can get info from STDIN and pipe out to STDOUT to feed into the actual mail box.

What I've been having trouble with is the inserting of the text so it comes in the body after any content endcoding start but before the content encoding end.

Can anyone share any good Perl or PHP libraries, code, etc. for handling this endeavor?

If it helps, this is some quick and dirty Perl code I drafted up which does work with plain text, but not with HTML/MIME et al:

use strict;
use Mail::Internet;
my $message = new Mail::Internet \*STDIN;
my $from = $message->head->get("From");
my $domain = (split(/@/, $from))[1];
$domain = (split(/>/,$domain))[0];
my $customer_id = '##hs_customer_id:' . $domain . '##' . "\n\n";
my $body = $message->body();

my $first_line = shift(@{$body});
my $second_line = shift(@{$body});
unshift(@{$body}, $customer_id);
unshift(@{$body}, $second_line);
unshift(@{$body}, $first_line);

$message->body($body);
$message->print( \*STDOUT );
close($message);
exit (99);


Thank you.

mattle
09-09-2009, 11:20 PM
You might have to deal with the message at a lower level. I'd probably look at the Mail::Internet::asString() method. Then, find the content encoding and slip your stuff in right after.

methodical
09-22-2009, 02:15 PM
I've done this same thing before using Qmail + PHP/CLI. Now days I know there are some built-in solutions (and PEAR based solutions) but I prefer to use a pre-built class because it provides a more complete end-to-end solution.

When I did this I used a class from PHPClasses. I guess I'm too new here to post a URL so just google "PHP Classes" and they'll be the first match.

Once you're on the site, search for the word "MIME" and you'll find several solutions for encoding and decoding mime based emails.

I hope this helps.