Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Net::SMTP multiple recipients

Posted on 2004-08-27
7
Medium Priority
?
7,103 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
Six Sigma Control Plans

721 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