On Thu, 7 Sep 2000, Roger Espel Llima wrote:
> > > Might be a faq, but why would open(FH,'|qmail-inject') fail with
> > > fatal: read-error from within mod_perl?
> > Use
> > open MAIL, "| /var/qmail/bin/qmail-inject" or &die_html("test");
> > print MAIL "[your mail]";
> > close MAIL;
> >
> > I suppose you forgot the full path to qmail-inject ...
>
> I use this, it's a bit more complete and paranoid:
>
> # send mail from the system to someone
> sub sendmail {
> my ($from, $to, $subject, $message, $name) = @_;
> local *MAIL;
>
> # limit allowed characters in email addresses
> $to =~ tr/-a-zA-Z0-9_+%$.,:!@=()[]//cd;
>
> # safe pipes adapted from man perlipc
> local $SIG{PIPE} = sub {};
> my $pid = open (MAIL, "|-");
> local $SIG{ALRM} = sub { CORE::exit; };
>
> unless (defined $pid) {
> # fork failed!
> return "Error sending mail.";
> }
>
> if ($pid) {
> # parent
> $from = "$name <$from>" if defined $name;
> print MAIL "From: $from\n";
> print MAIL "To: $to\n";
> print MAIL "Subject: $subject\n\n";
> print MAIL $message, "\n";
> close MAIL;
> return 1;
> } else {
> # child
> exec("/var/qmail/bin/qmail-inject", "-f", $from, "--", $to)
> || CORE::exit;
> }
> }
>
> --
> Roger Espel Llima, [EMAIL PROTECTED]
> http://www.iagora.com/~espel/index.html
>
Just a bit of a note, the $pid will come back even if the pipe being
opened to qmail-inject fails. A child gets forked off to handle opening
the pipe, and the process id comes back from that, not qmail-inject.
You should also check this:
close(MAIL) or die "Couldn't open pipe to qmail-inject";
(I learned this one the hard way).
Brian Nilsen
[EMAIL PROTECTED]