For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > November 2007 > Trouble with Email::MIME modules









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Trouble with Email::MIME modules
Filip Sneppe

2007-11-27, 10:01 pm

Hi,

I am trying to do the following: I have (an) email that is fetched from
an IMAP server
What I want to do is go over this mail and do two things:

- change all "Content-Transfer-Encoding: quoted-printable" or "7bit"
to 8bit
- drop all MIME parts that are "Content-Type: text/html"

and then save the resulting email to a text file. I want to save
the email as 8bit, since that makes parsing things a lot
easier for other scripts I have. Over the past
two ws I have spent several hours reading up on various
MIME modules and testing things out. But I am simply not able
to come up with something that does what I want. Quite a frustrating
experience altogether, as I expected this to be a quick 20-lines
script.

At this point I am getting so frustrated that I would simply like
to look at working code of what I need to achieve and then learn
where I went wrong. I've already looked at various MIME and
mail related Perl modules. I think I need Email::MIME and
Email::MIME::Modifier but I can't come up with a working script.

Is there anyone who wants to help me out with this ? Thanks in
advance.

Best regards,
Filip
Tom Phoenix

2007-11-27, 10:01 pm

On 11/27/07, Filip Sneppe <filip.sneppe@gmail.com> wrote:

> At this point I am getting so frustrated that I would simply like
> to look at working code of what I need to achieve and then learn
> where I went wrong.


Well, the first part of what you'd like would seem to imply that
somebody else should write your program for you. But maybe you don't
mean that, you just want code that shows how to use the module? Any
good module should include examples (in the documentation, or
elsewhere) to show how it should be used in working code. Often these
examples are simple to adapt for many typical tasks, perhaps even
yours.

So, on to the second part. If you're ready to see where you went
wrong, post your code. Someone here will surely help you to bring it
closer to what you need.

Cheers!

--Tom Phoenix
Stonehenge Perl Training
Filip Sneppe

2007-11-27, 10:01 pm

On Nov 27, 2007 7:20 PM, Tom Phoenix <tom@stonehenge.com> wrote:

> Well, the first part of what you'd like would seem to imply that
> somebody else should write your program for you. But maybe you don't
> mean that, you just want code that shows how to use the module? Any
> good module should include examples (in the documentation, or
> elsewhere) to show how it should be used in working code. Often these
> examples are simple to adapt for many typical tasks, perhaps even
> yours.
>
> So, on to the second part. If you're ready to see where you went
> wrong, post your code. Someone here will surely help you to bring it
> closer to what you need.
>

Ok, thank you for your quick response. The closest I got was by using
code based on th "mimedump" example here:

http://www.iaeste.or.at/doc/libmime-perl/examples/

sub convert_mime_to_8bit
{
my ($log_fh, $loglevel, $entity, $nextpart) = @_;
my @parts;
my $IO;
my $tempstring;
my $returnstring;

if (!($nextpart) && ($entity->head->original_text =~
/boundary=\"(.*)\"$/m)) {
$nextpart = $1;
};
$returnstring = "--$nextpart\n".$entity->head->original_text;
$returnstring =~ s/^Content-Transfer-Encoding:
quoted-printable/Content-Transfer-Encoding: 8bit/m;
$returnstring .= "\n";

@parts = $entity->parts;
if (@parts) {
foreach my $i (0 .. $#parts) {
log_it (2, $log_fh, "|convert_mime_to_8bit():
part $i: $parts[$i] - $nextpart\n");
$returnstring .= convert_mime_to_8bit($log_fh,
$loglevel, $parts[$i], $nextpart);
};
} else {
my ($type, $subtype) = split('/', $entity->head->mime_type);
my $body = $entity->bodyhandle;
if ($type =~ /^(text|message)$/) {
if ($IO = $body->open("r")) {
log_it (2, $log_fh,
"|convert_mime_to_8bit(): converting $type part\n");
$returnstring .= $_ while (defined($_
= $IO->getline));
$returnstring .= "\n";
$IO->close;
};
} else {
if ($IO = $body->open("r")) {
log_it (2, $log_fh,
"|convert_mime_to_8bit(): base64-encoding $type part\n");
$tempstring .= $_ while (defined($_ =
$IO->getline));
$IO->close;
$returnstring .= encode_base64($tempstring);
};
};
};

return $returnstring;
}


I am calling this code like this:

my $parser = new MIME::Parser;
$entity = $parser->parse_data($lines);
$convertedstring = convert_mime_to_8bit($log_fh, $loglevel,
$entity, "");

where $lines is a reference to an array (of lines containing the original
mail).

While the code itself does convert parts of the mail to 8bit, here are
two problems
with it:
- for some reason, for every attachment in the email, it creates files on the
local filesystem. I don't want to have to clean these up manually.
- more importantly, the email that I am puzzling back together, is not a "valid"
email: another application (in php) is reading it, and is simply
complaining that
the email format is wrong.
So basically, the subroutine is screwing up spaces or MIME metadata
somewhere.

At this point I am thinking: there's got to be better ways to do this,
without my
code having to puzzle back together the email etc.

So I'm digging around, reading up on all the MIME modules Perl has.

There is Email::MIME::Modifier which talks about this method:
encoding_set

However, I am unable to find any example code. As you can see, the
Email::MIME and Email::MIME::Modifier don't have that much example
code, especially for a non-MIME expert.

Best regards,
Filip
Tom Phoenix

2007-11-29, 7:02 pm

On 11/27/07, Filip Sneppe <filip.sneppe@gmail.com> wrote:

> - for some reason, for every attachment in the email, it creates files on the
> local filesystem. I don't want to have to clean these up manually.


The module should provide some way to clean these up, if it creates them.

> - more importantly, the email that I am puzzling back together, is not a "valid"
> email: another application (in php) is reading it, and is simply
> complaining that
> the email format is wrong.


If you're trying to parse invalid files, things get much tougher. You
may need to s the advice of MIME experts to get things working the
way you want.

I couldn't see any big bugs in the code you posted, and it seems that
nobody else did either. If you step through it with the debugger, you
may be able to see where it's going wrong.

Good luck with it!

--Tom Phoenix
Stonehenge Perl Training
Celejar

2007-11-30, 7:01 pm

On Tue, 27 Nov 2007 22:21:06 +0100
"Filip Sneppe" <filip.sneppe@gmail.com> wrote:

> I am calling this code like this:
>
> my $parser = new MIME::Parser;
> $entity = $parser->parse_data($lines);
> $convertedstring = convert_mime_to_8bit($log_fh, $loglevel,
> $entity, "");
>
> where $lines is a reference to an array (of lines containing the original
> mail).
>
> While the code itself does convert parts of the mail to 8bit, here are
> two problems
> with it:
> - for some reason, for every attachment in the email, it creates files on the
> local filesystem. I don't want to have to clean these up manually.


From the MIME::Parser docs:

> Examples of output control
>
> ### Keep parsed message bodies in core (default outputs to disk):
> $parser->output_to_core(1);
>
> ### Output each message body to a one-per-message directory:
> $parser->output_under("/tmp");
>
> ### Output each message body to the same directory:
> $parser->output_dir("/tmp");
>
> ### Change how nameless message-component files are named:
> $parser->output_prefix("msg");


And see the section "Specifying output destination" for the precise
specification.

Celejar
--
mailmin.sourceforge.net - remote access via secure (OpenPGP) email
ssuds.sourceforge.net - A Simple Sudoku Solver and Generator

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com