kbforme has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

Can any one help me in sending mail from perl script to multiple users? In the following script, indicating one mail address (for To field) works fine. But when multiple mail id's are indicated (comma or semicolon separated), no mails are being sent.

my $from = 'address'; my $to = 'address'; my $mime_type = "TEXT"; my $smtp = Net::SMTP->new('host') or die $!; $smtp->mail( $from ); $smtp->to($to); $smtp->data(); $smtp->datasend("To: $to\n"); $smtp->datasend("From: $from\n"); #$smtp->datasend("Subject: $subject\n"); $smtp->datasend("Subject: $_[0]\n"); $smtp->datasend("\n"); # done with header #$smtp->datasend($message); $smtp->datasend($_[1]); $smtp->dataend(); $smtp->quit(); # all done. message sent.

20040528 Edit by Corion: Added formatting

Replies are listed 'Best First'.
Re: Sending mails to multiple users
by Anonymous Monk on May 28, 2004 at 10:56 UTC
    The documentation suggests that this should work (untested)
    my @too = ('address1', 'address2'); ... $smtp->to(@too);

      That's right. Or multiple calls to to passing a scalar each time, does the same thing.

      $smtp->to($address1); $smtp->to($address2);
      s^^unp(;75N=&9I<V@`ack(u,^;s|\(.+\`|"$`$'\"$&\"\)"|ee;/m.+h/&&print$&;
Re: Sending mails to multiple users
by markjugg (Curate) on May 28, 2004 at 14:16 UTC
    If you are new to sending email with Perl, have you looked at MIME::Lite instead? I find it easy to use, yet powerful. It can send mail a number of ways, including SMTP. It's default is to just "Do the Right Thing". If that's not good enough, you can send the mail manually yourself.

    With it, you can specify multiple recipients as you would in an email client:

    To => 'a@b.c, d@f.g'
Re: Sending mails to multiple users
by nimdokk (Vicar) on May 28, 2004 at 13:10 UTC
    You could also try something like this:

    use Net::SMTP; my $message="This is a test of the Emergency Email Network."; my $address=q!address1@someplace.com; address2@anotherplace.com; addre +ss3@anywhere.com!; my @address=split(/; /,$address); my $sendmail=Net::SMTP->new('mailserver') or die $!; $sendmail->mail('mailfromme@myplace.com'); foreach (<@address>) {$sendmail->to($_);} $sendmail->data(); $sendmail->datasend("To: $address\n"); $sendmail->datasend("Subject: My Subject\n"); $sendmail->datasend("\n"); $sendmail->datasend($message); $sendmail->dataend;

    Granted I'm probably doing extra work in the snippet, but it serves its purpose and does what I want it to do.