Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Sending mail help

by debiandude (Scribe)
on Jul 23, 2003 at 20:54 UTC ( [id://277336]=perlquestion: print w/replies, xml ) Need Help??

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

I want to send mail in a perl program, but I want the mail message to cotain the output of a program. This is what I was trying but it didn't work:
open(SENDMAIL, "|/usr/lib/sendmail -oi -t -odq") || die "c\Cannot for mail: $!"; print SENDMAIL << "EOF"; From: me\@abc.com To: you\@xyz.com fuction(arguments); EOF close(SENDMAIL);
Any idea on how to make this work?

Replies are listed 'Best First'.
Re: Sending mail help
by RMGir (Prior) on Jul 23, 2003 at 21:01 UTC
    You need a blank line between the headers and the body.

    Also, "fuction(arguments);" won't interpolate the result you want.

    You'd probably want something like this, assuming your sendmail arguments are correct:

    open(SENDMAIL, "|/usr/lib/sendmail -oi -t -odq") || die "Cannot for mail: $!"; # function? You'll want to specify real arguments, of course my $body=fuction(arguments); # Don't forget the Date: and Subject: headers print SENDMAIL << "EOF"; From: me\@abc.com To: you\@xyz.com $body EOF close(SENDMAIL);
    I prefer using Net::SMTP myself, or MIME::Lite. MIME::Lite is especially easy to use.
    --
    Mike
Re: Sending mail help
by skyknight (Hermit) on Jul 23, 2003 at 21:11 UTC

    First off, sending stuff directly to Sendmail can be extraordinarily dangerous. You have to be very dilligent about what you actually send in as data or you could have a very unpleasant surprise. Think about what might happen if someone passed in an email address as the string "hahascrewyou@suckers.com; rm -rf ~". You're not sending in that kind of stuff on the command line, but still, taint checking is your friend, as will be libraries such as Email::Valid. Despite all the precautions you might take, and the fact that you're not passing args on the command line but rather through stdin helps, it's still messy and you're better off using a wrapper library.

    I use Mail::Mailer. Assuming that you're not passing around bogus data, here's the basic code snippet that I use.

    my $mailer = Mail::Mailer->new('sendmail'); $mailer->open({From => 'someone@somewhere.com', To => 'recipient@someplace.com', Subject => 'this is the subject'}); print $mailer "this is the body!"; $mailer->close();
Re: Sending mail help
by Paladin (Vicar) on Jul 23, 2003 at 21:01 UTC
    Using a Here-doc like that is just like using a double quoted string, and fuction calls don't interpolate inside a double quoted string without some (IMHO ugly) "magic". You could try something like:
    print SENDMAIL << "EOF"; From: me\@abc.com To: you\@xyz.com EOF print SENDMAIL fuction(arguments); close(SENDMAIL);
    Or assign the return value of the function to a variable, and put that inside the heredoc to interpolate normally.
    my $text = function(arguments); print SENDMAIL << "EOF"; From: me\@abc.com To: you\@xyz.com $text EOF close(SENDMAIL);
Re: Sending mail help
by phydeauxarff (Priest) on Jul 24, 2003 at 01:17 UTC
    A few years ago we had a project that had to scan hundreds of thousands of IP addresses every four hours to see if they were online and then mail out the results which we wrote to a spreadsheet using Spreadsheet::WriteExcel

    Mime::Lite became our best friend, appearing at the bottom of each script , sending out the results to all the folks who cared. I am not sure if this is fits what you are doing but if you are trying to send a message with the output of a program, this method of saving it to a file and then sending that file worked out well for us.

    We were able to pretty much deploy our attempts with the example code provided so it might be worth your trouble to check out.

Re: Sending mail help
by nite_man (Deacon) on Jul 24, 2003 at 07:23 UTC

    I'd suggest you to try use Mail::Sender for sending letters from Perl scripts.

    This module is easy in using but it leaves many useful features: sending plain and HTML text, addition attachments as separate file or include them into letter body etc. (See many examples in the module documentation)

    Also, there is a other good module - MIME::Entity. This module provides an interface for building messages as MIME entities. This module leaves you develop more unitized applications. There are many examples in the documentation of this module.

    If you prefer use low level for sending letters, I'd recomend you to look at RFC 2822: Internet Message Format.

          
    --------------------------------
    SV* sv_bless(SV* sv, HV* stash);
    

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2024-04-19 20:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found