MIME::Entity attaching from an open filehandle?

M

Matthew Braid

Hi all,

I'm using MIME::Tools and all its sundry packages to do some basic mail
manipulation (a little bit unbasic enough to not make MIME::Lite an option).

One part of my code requires attaching a new part onto an existing
message, and while the attach method is simple enough, I have to jump
though hoops when my data is stored in a file and I only have the
filehandle, not the file name.

At the moment, since the data could be quite large, I'm using
File::Temp::tempfile to create a temp file (requesting both a filehandle
and a name), copy the contents from the data filehandle into the
tempfile and then pass the tempfile's name to attach(). This is kind of
irksome because I know the function that returns the data filehandle
(which I can't change at the moment and would be a horrible hack if I
did) has already used File::Temp::tempfile to store the data in the
first place.

So at the moment I've got something like:

# -- START CODE --
my $datah = get_data(...); # The function that returns the data handle
# Its a library function so altering its
# return value would have numerous knock-on
# effects
my $mime = MIME::Entity->build(Data => 'Test',
From => '(e-mail address removed)',
To => '(e-mail address removed)',
Subject => 'This is a test');
{
# Urk
my ($tfh, $tempfile) = File::Temp::tempfile(UNLINK => 1);
while (defined(my $line = <$datah>)) {
print $tfh, $line;
}
$mime->attach(Path => $tempfile,
Type => 'application/octet-stream',
Encoding => 'base64');
}
# -- END CODE --

Being able to do something like:

$mime->attach(FH => $datah,
Type => 'application/octet-stream',
Encoding => 'base64');

would be infinitely preferable.

Is there any hidden way to do this, or can someone give me some tips as
to how to add this functionality to MIME::Entity?

MB
 
B

Ben Morrow

Matthew Braid said:
At the moment, since the data could be quite large, I'm using
File::Temp::tempfile to create a temp file (requesting both a filehandle
and a name), copy the contents from the data filehandle into the
tempfile and then pass the tempfile's name to attach(). This is kind of
irksome because I know the function that returns the data filehandle
(which I can't change at the moment and would be a horrible hack if I
did) has already used File::Temp::tempfile to store the data in the
first place.
Being able to do something like:

$mime->attach(FH => $datah,
Type => 'application/octet-stream',
Encoding => 'base64');

would be infinitely preferable.

Is there any hidden way to do this, or can someone give me some tips as
to how to add this functionality to MIME::Entity?

Hehehe a use for two-arg magic open :).

Try passing a path of '&=' . fileno($FH) . This should mean that
whenever the MIME::Body opens the filehandle, it will get the existing
FH on the tempfile.

Note that you are then responsible for making sure your temp file
stays open until after the MIME::Entity object is destroyed. Bad
Things *will* happen if you don't :).

Ben
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top