MIME::Entity attaching from an open filehandle?

Discussion in 'Perl Misc' started by Matthew Braid, Feb 11, 2004.

  1. 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 => '',
    To => '',
    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
     
    Matthew Braid, Feb 11, 2004
    #1
    1. Advertising

  2. Matthew Braid

    Ben Morrow Guest

    Matthew Braid <> 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.'
    :-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:
     
    Ben Morrow, Feb 11, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. CJ
    Replies:
    1
    Views:
    1,603
    Andrew Thompson
    Oct 29, 2004
  2. Kun
    Replies:
    1
    Views:
    519
    Serge Orlov
    Apr 17, 2006
  3. markla
    Replies:
    1
    Views:
    572
    Steven Cheng
    Oct 6, 2008
  4. Jan Arickx
    Replies:
    0
    Views:
    216
    Jan Arickx
    Aug 25, 2003
  5. Shmuel (Seymour J.) Metz
    Replies:
    0
    Views:
    349
    Shmuel (Seymour J.) Metz
    May 21, 2006
Loading...

Share This Page