Solved

Net::SMTP multiple recipients

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Perl program to obtain a machine's memory usage 6 23
Existing perl code to be changed for ftp to sftp handling 14 194
Perl strange behaviour 5 73
perl syntax 3 4
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
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…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

810 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