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

Perl Supremos
I have a simple Perl program which produces an HTML file, that I then send via email (using MIME::lite).
My question is, how do I get the contents of my HTML file to be held in memory and passed to sendmail instead of having to write the output to a file first ?
here is the current code
#!/usr/bin/perl use DBI; use DBIx::XHTML_Table; use MIME::Lite 1.137; $user="sybuser"; $server=SERVER; $pass=nopass; open(DAT,">test.html"); print DAT "<HTML>\n"; print DAT "<BODY>\n"; my $dbh = DBI->connect("dbi:Sybase:server=$server", $user, $pass,{ PrintError => 0, # Don't print + warning messages RaiseError => 1 } ); my $table = DBIx::XHTML_Table->new($dbh); $table->exec_query(" select srvname from master..sysservers where srvid=0 "); $table->modify(td => { style => 'color: black; background: yellow; text-align: +center; border: 1', }); print DAT $table->output({ no_head => 1 }); $table->exec_query(" select SPID,Login,Application,DBName from monProcess where +Login not in (NULL,'probe') "); $table->modify(table => { border => 2, width => '100%', }); $table->modify(th => { style => 'color: black; text-align: center; font-weight: + bold', }); $table->modify(td => { style => 'color: black; background: yellow; text-align: +center; border: none', }); print DAT $table->output({ no_head => 0 }); $dbh->disconnect; print DAT $table->output({ no_head => 0 }); print DAT "\n</BODY>\n"; print DAT "\n</HTML>"; close(DAT); my $msg = MIME::Lite->new(From => 'noname@nohost', To => 'someone@nowhere.com', Subject => 'Report', Type => 'text/html', Path => 'test.html'); $msg->send;
I would rather not have to create a temporary html file to do this
e.g. Use MIME::Lite->new(DATA=>'html stuff here'); ?

Replies are listed 'Best First'.
Re: HTML file or in-memory ?
by bart (Canon) on Dec 15, 2004 at 13:14 UTC
    Use
    $html .= ...
    instead of
    print DAT ...
    and initialize it by setting
    my $html = "";
    instead of the open call. Afterwards, i.e. at the time of the close call, your HTML page will be held in the variable $html, instead of in that file — ready for you to attach it to the mail.
      Thanks. This worked a treat
Re: HTML file or in-memory ?
by gellyfish (Monsignor) on Dec 15, 2004 at 13:16 UTC

    Instead of printing to the file just append all the output to a scalar and then pass that to MIME::Lite->new() using the Data option rather than Path.

    /J\

Re: HTML file or in-memory ?
by thor (Priest) on Dec 15, 2004 at 13:57 UTC
    Alternatively, if there are a lot of print statements that you'd have to change, you can print to a scalar variable. All you have to do is call open like so:
    open(FH, ">", \$var);
    After that, you can print to FH and the results will be accumulated in $var, which you can then use the options presented by the monks that responded before me. This all assumes a fairly recent version of perl (I don't know exactly when this feature was added).

    thor

    Feel the white light, the light within
    Be your own disciple, fan the sparks of will
    For all of us waiting, your kingdom will come

      I tried this and I get a file called SCALAR(0x189a68). So I guess I haven't go the correct version for this to work (I'm using 5.6.1).

        Yup, it's your version. Pre 5.8 IMSMR you can use IO::Scalar to get similar functionality.