mr.dunstan has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to build a little report generator for our larger reports that will get forked off from the main application so it can crunch away at the data without holding up the rest of the app. Forking off the process is not the problem I'm looking at though...

What I want to do is send the report as a .tab or .csv email attachment to whoever runs the report. I've been looking at this 'E-mail with Attachments' using Mime::Lite on TPJ, but I want to avoid writing the report out to a file on the server's hard disk before emailing it off ...

There's gotta be a way to do this. If anyone tells me I owe you a coke!



-mr.dunstan
  • Comment on Mail with attachments, but w/o writing out a file first

Replies are listed 'Best First'.
Re: Mail with attachments, but w/o writing out a file first
by petdance (Parson) on Sep 20, 2001 at 00:01 UTC
    Here's a chunk of code that is in production at my office:
    my $subject = "FLR invoices"; my $msg = MIME::Entity->build( From => SOURCE, To => TARGET, Subject => $subject, Type => "multipart/mixed", ); my $textpart = <<END; Attached is the XML file $xmlfile. Andy END # Add the text greeting $msg->attach( Type => 'text/plain', Encoding => '7bit', Data => $textpart, ); # Add the XML file $msg->attach( Type => 'text/xml', Encoding => 'base64', Description => "XML file $xmlfile", Path => $xmlfile, ); $msg->send( 'sendmail' );
    Note that the body is specified thru a variable, and the attachment is a file. I'm pretty sure you don't have to specify a filename when you create the attachment.

    xoxo,
    Andy
    --
    <megaphone> Throw down the gun and tiara and come out of the float! </megaphone>

Re: Mail with attachments, but w/o writing out a file first
by runrig (Abbot) on Sep 20, 2001 at 00:10 UTC
    Write the report to a scalar (or an array of scalars) and use 'Data' instead of 'Path' (see the MIME::Lite docs).
    Or use 'FH' instead of 'Path' if that's more appropriate (if your report generator can produce a filehandle to read from, say, by forking and using pipe to write/read the report).
Re: Mail with attachments, but w/o writing out a file first
by Rhandom (Curate) on Sep 20, 2001 at 01:14 UTC
    This reply has more to do with parsing messages with attachments rather than sending them, but...

    I was recently writing some software that parses messages and separates attachments using the MIME::Entity suite of modules. I specifically had to specify not to save them out to temporary files as in the following code (note the output_to_core):

    use IO::File; use MIME::Parser (); my $fh = IO::File->new('somemailfile','r'); my $parser = MIME::Parser->new(); $parser->output_to_core(1); # do everything in memory my $mail_object = eval{ $parser->parse($fh) };


    Sorry this is not necessarily to the point, but I have included it in case you end up doing more than just send the attachments -- which almost always comes soon after being able to send them (ie - save a copy and read it in for later use).

    my @a=qw(random brilliant braindead); print $a[rand(@a)];