Solved

Net::SMTP multiple recipients

Posted on 2004-08-27
7
7,056 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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…
Six Sigma Control Plans

623 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