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

Hi, I am writing a CGI script to send email to one or more recipients. I have tried using both Mail::Mailer and MIME::Lite, with varying degrees of failure. I got Mail::Mailer to send email out using the following code:
$address='one@example.com, two@example.com'; use Mail::Mailer; $msg = Mail::Mailer-> new('smtp', Server=>'mysmtp.server.com'); $msg->open({ From => 'sender@example.com', To => "$address", Subject => 'Subject', }); print $msg 'Body text here'; $msg->close();
No email ever arrives however, not even a bounce. I looked at the Exim logs on mysmtp.server.com, and it gives the following error:
2003-09-19 10:28:30 SMTP call from (clientname)(localhost.localdomain) + [IP address] dropped: too many unrecognized commands
It's probably a daft error somewhere, but I'm very new to Perl and can't see the problem from the documentation I've read. I tried two approaches with MIME::Lite. The first works perfectly if I sepecify the "To" line as follows:
To => 'one@example.com, two@example.com',
but if I use:
$address='one@example.com, two@example.com'; my $msg = MIME::Lite-> new( From => 'sender@example.com', To => "$address", Cc => 'three@example.com', Subject => 'Subject', Type => 'text/plain', Data => 'Body text here' ); $msg-> send('smtp', "mysmtp.server.com", Timeout=>60);
It will only send to the first address in the list. This also happens if I swap the entries for "To" & "Cc" round. The second approach was to to use a loop to send the mail as follows:
use Mime::Lite; use Text::ParseWords; # $address is actually taken from a combo box on a web page # but equals the definition below. # That's why I have done the array like this. $address='one@example.com, two@example.com, three@example.com'; @addresses = parse_csv($address); while(@addresses) { my $msg = MIME::Lite-> new( From => 'sender@example.com', To => shift(@addresses), Subject => 'Subject', Type => 'text/plain', Data => 'Body text here.' ); $msg-> send('smtp', "mysmtp.server.com", Timeout=>60); } .... sub parse_csv { return quotewords("," => 0, $_[0]); }
This only sends mail to the first recipient as well. It goes through the loop the requisite number of times. I put a "print" statement in there to make sure. I'm sorry this is such a long question. Hopefully there is an easy solution. I've only used Perl for a week, so I've probably missed something obvious. All help very gratefully received! Thanks, Andy.

Replies are listed 'Best First'.
Re: Mail::Mailer & MIME:Lite On Win2k
by antirice (Priest) on Sep 19, 2003 at 11:25 UTC

    Take a trip through the documentation and note where it refers to multiple addresses. Just because you represent something in outlook one way doesn't mean that's how it's represented when it's sent out. Try the following instead:

    $address= [ qw(one@example.com two@example.com) ];

    This assigns $address to an anonymous array containing the two addresses. For more information, check out perldata and perldsc.

    Hope this helps.

    antirice    
    The first rule of Perl club is - use Perl
    The
    ith rule of Perl club is - follow rule i - 1 for i > 1

Re: Mail::Mailer & MIME:Lite On Win2k
by demerphq (Chancellor) on Sep 19, 2003 at 12:09 UTC

    I cant speak to the problem with Mail::Mailer as I have nothing to do with it. As for MIME::Lite I can confirm that not only does MIME::Lite do the right thing, but that your code (without the loop) works perfectly. My guess is that what is happening here is that you are on Exchange and that you have two addresses that resolve to yourself (at least thats what I have and I get similar results as you say). Well, it turns out that exchange is playing games here. Since only one message is being transmitted to multiple recipients but those recipients resolve to yourself it doesnt give you the mail multiple times it only gives it to you once. Its entirely possible that other mail handlers do similar things.

    Anyway, try using two accounts in radically different places, I used work and hotmail. And I got the mail at both. :-)

    Ill change the documentation before I release 3.02 to make this clear.

    Cheers


    ---
    demerphq

    <Elian> And I do take a kind of perverse pleasure in having an OO assembly language...
Re: Mail::Mailer & MIME:Lite On Win2k
by Murf (Initiate) on Sep 24, 2003 at 14:05 UTC
    Thanks for your replies. To demerphq, I must apologise, the code I gave that you refer to does work. The problem was that I was getting "$address" from a multiple combo box on a web page.

    This was putting a blank character as the first letter of each recipient, except for the first. I don't know what the character was as it didn't show up at all when I printed $address out on the HTML page generated by the script. It wasn't whitespace either as it survived this:

    s/^\s+//; s/\s+$//;
    However, when I turn $address into an array and do the following to every item but the first:
    substr($string, 0, 1) = "";
    It works perfectly. Obviously the HTML puts a weird character between items from the combo box. I was misled by the fact that it doesn't show up when you output the string to HTML. Not as a space, a carrage return or anything.

    For the record, I tried substituting the 'smtp' command in the Mail::Mailer code with 'test', to produce the email as output, and ran it on the command line. It produced the following:

    to: one@example.com
    From: sender@example.com
    To: one@example.com
    Subject: Subject

    I don't know if this was what it should have returned, but it doesn't look at all right to me. Maybe this was why the mail server was knocking it back.

    I got MIME::Lite to do exactly what I wanted it to (eventually!). It works, it's good, I'm happy.

    Cheers,

    Andy.