Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > MIME::Entity attaching from an open filehandle?

Reply
Thread Tools

MIME::Entity attaching from an open filehandle?

 
 
Matthew Braid
Guest
Posts: n/a
 
      02-11-2004
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 Removed)',
To => '(E-Mail 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

 
Reply With Quote
 
 
 
 
Ben Morrow
Guest
Posts: n/a
 
      02-11-2004

Matthew Braid <(E-Mail Removed)> wrote:
> 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.

<snip>
> 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

--
'Deserve [death]? I daresay he did. Many live that deserve death. And some die
that deserve life. Can you give it to them? Then do not be too eager to deal
out death in judgement. For even the very wise cannot see all ends.'
:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-: http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Attaching Real Player to open .wvm files letterman@invalid.com Firefox 0 02-18-2009 05:26 AM
attaching someconfusing results in webbrowser.open on gnulinux krishnakant Mane Python 2 11-01-2007 03:20 PM
Attaching a Dlink modem to Router Console Port Chennak Cisco 1 10-07-2005 05:11 PM
send page without attaching John Starrett Firefox 3 08-28-2004 12:22 AM
debugger not automatically attaching aspnet_wp.exe as a debugged process Mark Kamoski ASP .Net 1 08-05-2003 02:57 PM



Advertisments