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

Sorry to waste your time with this simple mail question, but I cannot get this to work properly without an internal error:
$sender="email@\somewhere.com"; $recipient="email@\somewhere.com"; $subject="Database Entry"; $mail_prog = "/usr/sbin/sendmail -t"; $mail_out="true"; if($mail_out) { &SendMail($recipient,$sender); sub SendMail { open(MAIL, "|$mail_prog -t") || &error("Could not send out ema +ils"); print MAIL "To: $recipient \n"; print MAIL "From: $sender <$sender>\n"; print MAIL "Subject: database entry\n"; print MAIL "added\n\n"; print MAIL"--------------------------------------------------- +--------------------------------------\n"; print MAIL ""; print MAIL "\n\n"; print MAIL "\n\n"; close (MAIL); } # end SendMail function } # end if statement
What am I missing? Thanks for any help, From, Extreme Newbie

Replies are listed 'Best First'.
Re: Simple SendMail Question
by $code or die (Deacon) on Jan 23, 2001 at 03:08 UTC
Re: Simple SendMail Question
by repson (Chaplain) on Jan 23, 2001 at 04:18 UTC
    Apart from what $code or die said, there are a couple of other things.
    Can you see anything wrong with this:
    $mail_prog = "/usr/sbin/sendmail -t"; open(MAIL, "|$mail_prog -t");
    Apart from the fact these variables don't have their scope declared (see strict and my) you are passing sendmail two -t switches.

    And the last thing, though it's no real problem, just a question of clarity and style is to use a heredoc instead of multiple prints.

    print MAIL <<"MESG"; To: $recipient From: $sender Subject: database entry added --------------------------------------------------- . MESG
Re: Simple SendMail Question
by tune (Curate) on Jan 23, 2001 at 04:19 UTC
    consider the following too:
    &SendMail($recipient,$sender); sub SendMail { my ($recipient,$sender) = @_; open(MAIL, "|$mail_prog -t") || &error("Could not send out emails" +); print MAIL "To: $recipient \n"; print MAIL "From: $sender <$sender>\n"; print MAIL "Subject: database entry\n"; print MAIL "added\n\n"; print MAIL"------------------------------------------------------- +----------------------------------\n"; print MAIL ""; print MAIL "\n\n"; print MAIL "\n\n"; close (MAIL); } # end SendMail function
    then &SendMail($recipient,$sender); makes sense versus &SendMail();

    -- tune

Re: Simple SendMail Question
by Maclir (Curate) on Jan 23, 2001 at 04:05 UTC
    I would have thought that "Simple" and "SendMail" would never appear together in the one sentance. It is one of those oxymorons (like Miliray Intelligence).
Re: Simple SendMail Question
by InfiniteSilence (Curate) on Jan 23, 2001 at 20:54 UTC
    Style Point: instead of print MAIL "-----------------------------------------------", you could write
    perl -e "print MAIL q(-) x 30;"

    Celebrate Intellectual Diversity

Re: Simple SendMail Question
by Chady (Priest) on Jan 23, 2001 at 23:24 UTC
    The error is with your way of escaping the @ in the e-mail address... it's done like that \@
    you can also write the address in single quotes:
    $sender='me@somewhere.com';
    Chady | http://chady.net/