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

Hello Monks.. I have this script for sending out email..
#!/usr/bin/perl use warnings; use strict; use Net::SMTP; my $recepient = qq("nafroz\@sunamerica.com","sdas\@sunamerica.com"); print "$recepient \n"; my $smtp = Net::SMTP->new('smtp1.sunamerica.com'); $smtp->mail("nafroz\@sunamerica.com"); #$smtp->to("nafroz\@sunamerica.com" , "sdas\@sunamerica.com"); $smtp->to($recepient); $smtp->data(); $smtp->datasend("Subject: TEST MESSAGE\n"); $smtp->datasend("To: nafroz\@sunamerica\n"); $smtp->datasend("\n"); $smtp->datasend("Email test."); $smtp->dataend(); $smtp->quit;
Now the problem is that when I use the commented line code instead of passing $recepient...it works fine and in the above case it does not. Can you help me in fixing this. Thanks

Replies are listed 'Best First'.
Re: Problem in adress field for email script.
by GrandFather (Saint) on Jul 07, 2005 at 22:32 UTC

    I can't test it, but

    $smtp->to("nafroz\@sunamerica.com" , "sdas\@sunamerica.com");

    is passing two arguments where $smtp->to($recepient); is passing one. You could try:

    my @recepient = qq("nafroz\@sunamerica.com","sdas\@sunamerica.com"); ... $smtp->to($recepient[0], $recepient[0]);

    Perl is Huffman encoded by design.
Re: Problem in adress field for email script.
by radiantmatrix (Parson) on Jul 07, 2005 at 22:55 UTC
    <update>I see GrandFather was faster and more concise than me</update>
    qq("nafroz\@sunamerica.com","sdas\@sunamerica.com")
    Creates a string that is literally
    "nafroz\@sunamerica.com","sdas\@sunamerica.com"

    Whereas

    $smtp->to("nafroz\@sunamerica.com" , "sdas\@sunamerica.com");
    is passing a list of two values, neither of which contain quotes, to the to() method. You can make the following changes ('#-' means remove the line it quotes, and replace it with the code following):
    #- my $recepient = qq("nafroz\@sunamerica.com","sdas\@sunamerica.com") +; my @recepient = ("nafroz\@sunamerica.com","sdas\@sunamerica.com"); #- $smtp->to($recepient); $smtp->to(@recepient);

    What I have done here is replace your scalar with an array that has your two addresses as members, then alter your call to to() so that it takes that list as the argument.

    Larry Wall is Yoda: there is no try{}
    The Code that can be seen is not the true Code
Re: Problem in adress field for email script.
by tlm (Prior) on Jul 07, 2005 at 23:50 UTC

    Since you are not interpolating, you can use single quotes, and save yourself some backslashes. How about

    $smtp->to( 'nafroz@sunamerica.com,sdas@sunamerica.com' );

    the lowliest monk

Re: Problem in adress field for email script.
by cajun (Chaplain) on Jul 08, 2005 at 03:12 UTC
    Personally, I like Mime::Lite better for something like this. YMMV.

    #!/usr/bin/perl -w use strict; use MIME::Lite; my @recipients = qw( nafroz@sunamerica.com sdas@sunamerica.com ); my $message = 'Email test.'; MIME::Lite->send('smtp', 'smtp1.sunamerica.com', Timeout=>60); foreach (@recipients){ print "$_\n"; &send_message($_); } sub send_message{ my $msg = MIME::Lite->new( From => 'yourname@sunamerica.com', BCc => "$_", Subject => 'TEST MESSAGE', Type => 'TEXT', Data => "$message" ); $msg->send; }