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

First time trying to do this so please let me know if I got it backwards. I'm trying to go through a database and send an email to each address ($emails{$email} could be me@you.com). Doing test prints after the foreach loop it says it is emailing both email addresses in the database but the email isn't received to either account. Anyone understand why?
if ( param() ) { if ( $message | $subject eq "" ) { print "Your subject or email content was missing."; } else { foreach ( sort keys(%emails) ) { open( MAIL, "| $sendmail -t" ); print MAIL "To: $_"; print MAIL "From: $adminmail"; print MAIL "Subject: $subject\n\n"; print MAIL "$message"; print MAIL "."; close(MAIL); print "Email was sent to: $_!<br>"; } } }


"Age is nothing more than an inaccurate number bestowed upon us at birth as just another means for others to judge and classify us"

sulfericacid

Replies are listed 'Best First'.
Re: Sending looped emails
by The Mad Hatter (Priest) on Apr 07, 2003 at 00:25 UTC
    Each of those header lines (and the last dot) needs to be on a different line IIRC. So:
    open( MAIL, "| $sendmail -t" ); print MAIL "To: $_\n"; print MAIL "From: $adminmail\n"; print MAIL "Subject: $subject\n\n"; print MAIL "$message\n"; print MAIL "."; close(MAIL); print "Email was sent to: $_!<br>";
    If that doesn't work, then I suggest you read up on the docs and try to send a message manually the same way to see if that works.

    Update Also, shouldn't $message | $subject eq "" be something like $message eq "" || $subject eq ""? Or more cleanly, !$message || !$subject.

      Ok, that was the problem. I did know you had to \n each line but oddly enough I didn't realise I forgot to do that. Everyone can downvote me for making a mistake but please don't waste time by replying to this since I already have the problem solved now. As for your update, let me check to see if mine actually works.. Three tests and my original code seems to do the trick (if either or is left out, the email won't send). If your way is definately better than mine, I'll switch over but as it looks right now there's nothing wrong with it.

      Thanks for your help Mad Hatter!

      "Age is nothing more than an inaccurate number bestowed upon us at birth as just another means for others to judge and classify us"

      sulfericacid

        Hmmm. I used this code to test your way:
        $foo = ""; $bar = "moo"; print "true\n" if $foo | $bar eq "";
        That doesn't print true, like it should if your way worked. That's what let me to believe that your way wouldn't work. Of course, I'm probably not understanding something...

        Anyway, glad to help.

Re: Sending looped emails
by Pardus (Pilgrim) on Apr 07, 2003 at 03:03 UTC
    To add to the above comments: you might wanna try using something like:
    unless ($message && $subject) { ...
    instead of:
    if ( $message | $subject eq "" ) { ...
    The '|' symbol is a bitwise operator, it might work the way you use it, but isn't a clean usage.

    Also you could simply do one multiline print, instead of repeating the 'print MAIL' statement, then you wouldn't have had the bug in the first place.
    --
    Jaap Karssenberg || Pardus (Larus)? <pardus@cpan.org>
    >>>> Zoidberg: So many memories, so many strange fluids gushing out of patients' bodies.... <<<<
•Re: Sending looped emails
by merlyn (Sage) on Apr 08, 2003 at 04:36 UTC
Re: Sending looped emails
by Jenda (Abbot) on Apr 07, 2003 at 19:55 UTC

    There is another thing. You should check the return value of the open() and close(). If you don't you do not notice if something goes wrong and you fail to start the sendmail or it reports a problem.

    open( MAIL, "| $sendmail -t" ) or die "Can't start $sendmail : $!\n"; print MAIL "To: $_"; print MAIL "From: $adminmail"; print MAIL "Subject: $subject\n\n"; print MAIL "$message"; print MAIL "."; close(MAIL) or die "Failed to send the mail: $! $?\n";

    Jenda
    Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.
       -- Rick Osborne

    Edit by castaway: Closed small tag in signature