Sending email to multiple recipients

2019-05-02 09:39发布

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?

3条回答
可以哭但决不认输i
2楼-- · 2019-05-02 10:25

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

查看更多
对你真心纯属浪费
3楼-- · 2019-05-02 10:28

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

@MailTo = ('mail1@demomail.com', 'mail2@demomail.com', ...., 'mailn@demomail.com')

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

$smtp->to(@MailTo);
查看更多
聊天终结者
4楼-- · 2019-05-02 10:41

Do it like this:

use Net::SMTP::Multipart;
$to1 = "sam\@bogus.com"; 
$to2 = 'tom@foo.com';
$smtp = Net::SMTP::Multipart->new($smtpserver);
$smtp->Header(To    =>  [ $to1, $to2, 'another_email@server.com' ],
              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.

查看更多
登录 后发表回答