Solved

Net::SMTP multiple recipients

Posted on 2004-08-27
7
6,948 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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

705 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now