Solved

Net::SMTP multiple recipients

Posted on 2004-08-27
7
6,996 Views
Last Modified: 2010-10-05
Hi,

My mail script based on Net::SMTP is failing when I try to send to multiple recipients. I know why it's failing--because the multiple recipient formatting is incorrect. But I don't know the quickest way to fix it. According to the Perl doc:

$smtp->recipient($recipient1,$recipient2);  # Good
$smtp->recipient($recipient1,$recipient2, { SkipBad => 1 });  # Good
$smtp->recipient("$recipient,$recipient2"); # BAD  

You'll see that in my script, I'm doing #3 above, so it doesnt work.
How do I fix my script so that it will split my $Mailto string into an array or dynamically create the variables that will use this module the right way??

Here's the script:

use Net::SMTP;
$Mailfrom = "someaddress\@mysite.com";
# this line is causing the error
$Mailto = "recip1\@mysite.com,recip2\@mysite.com";

$smtp = Net::SMTP->new('maildomain',
      Hello => 'maildomain',
      Timeout => 30,
      Debug   => 1,
      );


$smtp->mail($Mailfrom);
$smtp->to($Mailto);

$smtp->data();
$smtp->datasend("To: $Mailto\n");
$smtp->datasend("From: $Mailfrom\n");
$smtp->datasend("Subject: Test msg from Netsmtp \n\n");
$smtp->datasend("A simple test message\n");
$smtp->dataend();
$smtp->quit;  
0
Comment
Question by:pmosinskis
  • 3
7 Comments
 
LVL 2

Author Comment

by:pmosinskis
ID: 11917525
BTW,

$smtp->recipient("$recipient,$recipient2"); # BAD  

is supposedly the SAME as

$smtp->to("$recipient,$recipient2"); # BAD  

0
 
LVL 2

Author Comment

by:pmosinskis
ID: 11917637
got my own answer!

@to_addresses = split(/,/, $Mailto);
foreach $addr (@to_addresses) {
      $smtp->to($addr);
}
0
 
LVL 3

Expert Comment

by:rkosai
ID: 11917783
Actually, it would be better to code it like this:

$smtp->recipient(split(/,/, $Mailto));

This would eliminate the intermediate variable @to_addresses, as well as remove the unnecessary loop.  Cleaner and faster. :-)
0
 
LVL 2

Author Comment

by:pmosinskis
ID: 12313807
i recommend that the points for the question be refunded, because i answered my own question. rkosai's solution is also correct, perhaps more optimized, but nonetheless i answered my own question.

thanks,
pete
0
 
LVL 1

Accepted Solution

by:
Computer101 earned 0 total points
ID: 12327045
PAQed, with points refunded (125)

Computer101
E-E Admin
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question