Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I have a bunch of code that queries an Oracle DB, usually puts the data in a spreadsheet using Win32, formats it and ships it using MIME-Lite. What I've, unsuccessfully, been trying to do lately is to just send some data (from the DB) in the body of the email. I read and re-read the MIME::Lite stuff on cpan and came back empty handed. If it cannot be done using this module, could you please point me to a module with which it can be done? Thanks.

I.E:
sub sql { my @output = $sqlCount->execute; while (@output=$sqlCount->fetchrow_array) { print "data: @output \n"; } $sqlCount->finish(); $dbcon->disconnect(); } my $msg = MIME::Lite->new( from=>'', to =>'', subject=>'', Data=>'&sql;' ### yeah, I wish ); $msg->send('smtp','foobar',Timeout=>60);

Replies are listed 'Best First'.
Re: passing data using MIME::Lite
by Joost (Canon) on Mar 01, 2005 at 15:50 UTC
    You're not actually returning any useful information from sql().

    how about this:

    sub sql { $sqlCount->execute or die "Can't execute query"; my $data; while (@output=$sqlCount->fetchrow_array) { $data .= "data: @output \n"; } $sqlCount->finish(); $dbcon->disconnect(); return $data; } # .... my $msg = MIME::Lite->new( From=>'', To =>'', Subject=>'', Data => sql() );
    It's still messy, though.

    Also see perlsub

Re: passing data using MIME::Lite
by jdtoronto (Prior) on Mar 01, 2005 at 16:01 UTC
    Your basic mistake is how you are trying to tget the data back to MIME::LITE. Try something like this:
    sub sql { my $body_text; my @output = $sqlCount->execute; while (@output=$sqlCount->fetchrow_array) { $body_text .= join ":", @output; } $sqlCount->finish(); $dbcon->disconnect(); return $body_text; }
    Then at least you will be returning a scalar from your sql subroutine and the data just might get into the email. In fact I haven't tested this code, but it is almost identical to something I do nearly every day on one of my production machines.

    UPDATE And deibyz picked the other problem. In the message object the data should be placed as he does, or in my example, simply:

    Data => sql()
    jdtoronto
Re: passing data using MIME::Lite
by deibyz (Hermit) on Mar 01, 2005 at 15:52 UTC
    I can see some problems in your code.

    First, you're using single quotes in the Data param, wich is going to insert you the literal text "&sql".

    You're also printing what you want your function to return, not returning it. Maybe something like that (untested):

    sub sql { my @output = $sqlCount->execute; my @data; while (@output=$sqlCount->fetchrow_array) { push @data, @output; } $sqlCount->finish(); $dbcon->disconnect(); return \@data }

    And, in the Data field:

    my $data = join "\n", @{ +sql() }; ... Data => $data ...
Re: passing data using MIME::Lite
by Anonymous Monk on Mar 01, 2005 at 16:27 UTC
    thanks all folks. As pretty much everybody pointed out, the problem was the brain-dead printing in the sub, instead of returning. Thanks again.