6

I've moved some old code from an old unix box to our new unix box, and I'm having some difficulty with a perl script sending email to multiple recipients. It works on the old box.

Old box perl: version 5.004_04 built for PA-RISC2.0

New box perl: v5.8.8 built for IA64.ARCHREV_0-thread-multi-LP64

Here's the basics of the script (stripped-down):

use Net::SMTP::Multipart;
$to = "sam\@bogus.com tom\@foo.com";
$smtp = Net::SMTP::Multipart->new($smtpserver);
$smtp->Header(To    =>  $to,
      From  =>  "junk\@junk.com",
      Subj  =>  "This is a test.");
$smtp->Text("Hello, world!\n");
$smtp->End();

This works if I change it to $to = "justOneEmail\@address.com", but if I have two or more email addresses (separated by spaces), it no longer works. I don't get an error message, but no message shows up.

Any ideas why?

2
  • You have a new Unix box that has a six year old version of Perl? Interesting definition of new! But I suppose it's an improvement on the almost fifteen year old version you have before :-)
    – Dave Cross
    May 9, 2012 at 19:58
  • @Dave, lol. Yep. "New" is relative. :)
    – Jonathan M
    May 9, 2012 at 20:11

3 Answers 3

4

Do it like this:

use Net::SMTP::Multipart;
$to1 = "sam\@bogus.com"; 
$to2 = '[email protected]';
$smtp = Net::SMTP::Multipart->new($smtpserver);
$smtp->Header(To    =>  [ $to1, $to2, '[email protected]' ],
              From  =>  "junk\@junk.com",
              Subj  =>  "This is a test.");
$smtp->Text("Hello, world!\n");
$smtp->End();

Notice that if you use double-quotes, you should escape the @ in the email addresses, or perl may try to interpret it as an array interpolation.

10
  • So the To under Header is not the place to put the email addresses? The examples on CPAN don't show the attribute you're using: search.cpan.org/~orien/Net-SMTP-Multipart-1.5.4/Multipart.pm
    – Jonathan M
    May 9, 2012 at 17:33
  • OK, my bad, i assumed you were using Net::SMTP, but you're using Net::SMTP::Multipart. I think your problem is you need a list of addresses, that is, each address is a string, separate these strings with commas. For good measure, just do it the way they do the "CC" in the CPAN example in the link you mentioned. Editing my answer to reflect this.
    – theglauber
    May 9, 2012 at 17:38
  • OK. Newbie question: if I have @tolist=['[email protected]','[email protected]']; how do I convey that to the Header? Like this? To => @tolist? Won't that mess up the parm list?
    – Jonathan M
    May 9, 2012 at 17:49
  • 2
    @JonathanM, you will want to write @tolist=('[email protected]','[email protected]'); and pass it in as To => \@tolist OR $tolist=['[email protected]','[email protected]']; and pass it in as To => $tolist.
    – Ven'Tatsu
    May 9, 2012 at 18:20
  • 1
    () creates a "list", which is more or less the same as an array. @varname is an array variable. $varname is an scalar variable (non-array, single value). \@array or [ x, y, z ] creates a reference to an array (more or less like a pointer to an array, in C). So you can do $scalar = \@array , or $scalar = [x, y, z] - see perldoc.perl.org/perlref.html
    – theglauber
    May 9, 2012 at 18:35
3

Instead of separating the email addresses with spaces, use a comma with no intervening spaces. This works for me..

1
  • Could you provide code as an example of what you are doing?
    – tale852150
    Oct 7, 2020 at 21:08
-3

Declare an array and put all the email id's like

@MailTo = ('[email protected]', '[email protected]', ...., '[email protected]')

Now use the Net::SMTP module to send out the emails

$smtp->to(@MailTo);
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.