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

I have a little script that pipes to mail and was wondering how I'd add a subject line and change the from line to something different than the unix process (informix db) that invokes the mail command. I've tried using the -s switch to add a subject line, but it doesn't seem to work. I looks a little something like this:
/^MAIL/ && do { $text = $execute; $syscmd = "echo $text $attributes{MSG} | mail $attributes{EADD}"; system ( $syscmd ); last SWITCH; };

Replies are listed 'Best First'.
Re: mail in script... how to add subject and from:
by kilinrax (Deacon) on Oct 16, 2000 at 20:34 UTC
    I'd use Mail::Mailer;
    sendmail ('god@heaven.com', 'die.puny@earthling.net','Test', 'This is +a test'); sub sendmail { my ($from, $to, $subject, $body) = @_; my $mailer; eval { $mailer = Mail::Mailer->new('sendmail'); }; if ($@) { warn "Couldn't send mail: $@"; } else { $mailer->open({ From => $from, To => $to, Subject => $subject }); print $mailer $body; unless (close ($mailer)) { warn "Couldn't close mailer: $!"; } } }
    Also, check that whichever user is sending the mail is allowed to set the 'From:' field.
    For instance, if you're using exim, add them to the 'trusted users' section of /etc/exim.conf
Re: mail in script... how to add subject and from:
by AgentM (Curate) on Oct 16, 2000 at 20:35 UTC
    Whoa! The best tip is: If you're in a perl script, stay there. Try using Mail::Sendmail instead. If you still need to use a command line, try 'sendmail -s...'. Remember that the Subject and To lines in an email message are actually just text that might as well be in the body of the message. (but you don't need to worry about that if you use the module above.)
    AgentM Systems or Nasca Enterprises is not responsible for the comments made by AgentM- anywhere.
Re: mail in script... how to add subject and from:
by Fastolfe (Vicar) on Oct 17, 2000 at 01:08 UTC
    Danger! Definitely go with one of the solutions already presented in this article, not only because it's more portable and simpler, but they're all more secure. It looks as though you are using potentially unsafe user-provided data in your system command, which is potentially disastrous. Please read perlsec and try running your stuff with the -T Perl option to ensure you aren't allowing any Joe Hacker to run any command on your system that he wants.
Re: mail in script... how to add subject and from:
by Penfold (Novice) on Oct 16, 2000 at 21:10 UTC
    The more Unixy than Perl solution is to make up the entire mail (headers, blank line, body) in perl and then open() a pipe to /path/to/sendmail -t
    --
    Mike Whitaker, System Architect, CricInfo Ltd