Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

printing output of a perl script to the Email body

by noviceuser (Acolyte)
on Jan 29, 2021 at 08:19 UTC ( [id://11127623]=perlquestion: print w/replies, xml ) Need Help??

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

Hi Guys, i am writing a perl script and want to print the output of the script in email body. how can i do that?

  • Comment on printing output of a perl script to the Email body

Replies are listed 'Best First'.
Re: printing output of a perl script to the Email body
by Corion (Patriarch) on Jan 29, 2021 at 08:23 UTC

    The easiest approach is to wrap the script in a second script that runs the first one in (say) backticks and then sends the output via MIME::Lite.

    use MIME::Lite; my $output = `other_script.pl 2>&1`; $msg = MIME::Lite->new( From => 'me@myhost.com', To => 'you@yourhost.com', Cc => 'some@other.com, some@more.com', Subject => 'Output of a script...', Type => 'multipart/mixed' ); ### Add parts (each "attach" has same arguments as "new"): $msg->attach( Type => 'TEXT', Data => $output );

      Like Corion, I would use MIME::Lite but...

      The documentation warns:
      "MIME::Lite is not recommended by its current maintainer."

      For that reason, I started looking at alternatives but didn't really get very far - mainly because of familiarity with the easy to use MIME:Lite module.

        Yes, it's not recommended by its maintainer, but it still works very well, and I haven't encountered a case where I had to investigate a different module.

        Also, the module won't be going anywhere and gets a release from time to time. The issues with patches are somewhat likely to be worked on.

Re: printing output of a perl script to the Email body
by davies (Prior) on Jan 29, 2021 at 12:47 UTC

    Having a wish to do what the OP wants and being endowed with the super-power of reading :-), I followed the pointers to Email::Stuffer and wrote myself the script below to parse nginx logs & email me every day about who has been trying to access my server (only a few of us should do that). I have given the whole script in case you or anyone wants to trace everything, but the relevant parts are:

    use Email::Stuffer; Email::Stuffer->to($opt_email) ->from('nginxlog@davies.systems') ->subject($subject) ->text_body($body) ->transport($transport) ->send;

    It's not that different from MIME::Lite. The entire modulino:

    Regards,

    John Davies

Re: printing output of a perl script to the Email body
by jcb (Parson) on Jan 30, 2021 at 02:26 UTC

    If you just need to catch the output of your script and send it in an email, and your Perl was built with PerlIO (default since 5.8 — you almost certainly have it), you can reopen STDOUT to an in-memory "file" or simply select a different filehandle opened to an in-memory file.

    Something like: (untested)

    my $MailText = ''; open MAILOUT, '>', \$MailText or die "open in-memory output: $!"; select MAILOUT; # ... print "foo\n"; # ... print "bar\n"; # ... close MAILOUT; select STDOUT; # $MailText now contains "foo\nbar\n"; send it however you want
Re: printing output of a perl script to the Email body
by Anonymous Monk on Jan 29, 2021 at 18:54 UTC
    In addition ... take a look at things like Template. Maybe "the body of the e-mail" should be "exactly the output of the script," or ... maybe not. The same techniques that are routinely used to construct web-site pages can also very profitably be used to construct emails.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11127623]
Approved by marto
Front-paged by kcott
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2024-04-26 00:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found