Stripping and replacing email headers

June 22nd, 2006 by Rohan

The Perl CPAN module Mail::MboxParser does a great job at stripping and replacing email headers.

Say you have your email message (with headers) in a string variable $msg_str. Now check this

use Mail::MboxParser;

my $mail = Mail::MboxParser::Mail->new($msg_str);
my $mail_new = $mail->make_convertable();
$mail_new->delete_from_header('received');
$mail_new->replace_in_header('to', 'new_to@domain.com');
$mail_new->replace_in_header('from', 'new_from@domain.com');
$mail_new->send('sendmail');

The above code strips all the Received headers and replaces the To and From headers. It even takes care of the Return-Path header and any other From - headers.

One drawback, is that I could not find a way to retrieve the message string after converting it. There is no as_string method. I’ll submit a patch if I can find time. :-)

[Post to Twitter] Tweet This Post  [Post to Facebook] Facebook 

Leave a Reply




Tweet This Post links powered by Tweet This v1.4.1, a WordPress plugin for Twitter.