In the previous recent thread parsing and printing e-mail, keszler suggested the Email::MIME module.
Here's a test program that uses Email::MIME to pull apart a full message.
You should be able to figure out how to isolate just the parts you want.
#!/usr/bin/perl -w
use strict;
use warnings;
use Email::MIME;
my $body = join("",(<DATA>));
my $email = Email::MIME->new($body);
print "===== Top Header =====\n";
print "From: ".($email->header("From"))."\n";
print "Date: ".($email->header("Date"))."\n";
print "Subject: ".($email->header("Subject"))."\n";
print "\n===== Top Body =====\n";
print "".($email->body())."\n";
my @parts = $email->parts();
print "There are ".@parts." parts in this message.\n";
my $p = 0;
foreach my $part (@parts)
{
$p++;
print "\n===== Part $p Header =====\n";
print "Content-Type: ".($part->header("Content-Type"))."\n";
print "Content-Transfer-Encoding: ".($part->header("Content-Transf
+er-Encoding"))."\n";
print "\n===== Part $p Body =====\n";
print "".($part->body())."\n";
}
__DATA__
From: homer@simpson.net
To: marge@simpson.net
Subject: Bart and Lisa
Date: Thu, 17 Sep 2009 12:17:23 -0700
Content-Type: multipart/alternative;
boundary="----=_NextPart_000_0009_01CA7F30.7F3A37C0"
Mime-Version: 1.0
This is a multi-part message in MIME format.
------=_NextPart_000_0009_01CA7F30.7F3A37C0
Content-Type: text/plain;
charset="us-ascii"
Content-Transfer-Encoding: 7bit
From: sender@email.address
Sent: Wednesday, December 16, 2009 9:28 PM
To: test@email.address
Subject: This is the subject
This is the body.
------=_NextPart_000_0009_01CA7F30.7F3A37C0
Content-Type: text/html;
charset="us-ascii"
Content-Transfer-Encoding: quoted-printable
<html>
<head>
<body>
This is the body.
</body>
</html>
------=_NextPart_000_0009_01CA7F30.7F3A37C0--
Output from the program:
===== Top Header =====
From: homer@simpson.net
Date: Thu, 17 Sep 2009 12:17:23 -0700
Subject: Bart and Lisa
===== Top Body =====
This is a multi-part message in MIME format.
There are 2 parts in this message.
===== Part 1 Header =====
Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: 7bit
===== Part 1 Body =====
From: sender@email.address
Sent: Wednesday, December 16, 2009 9:28 PM
To: test@email.address
Subject: This is the subject
This is the body.
===== Part 2 Header =====
Content-Type: text/html; charset="us-ascii"
Content-Transfer-Encoding: quoted-printable
===== Part 2 Body =====
<html>
<head>
<body>
This is the body.
</body>
</html>