http://www.perlmonks.org?node_id=1024666


in reply to How to send multiple user with one mail?

Open the input.txt file, I hope the email id will be in the third filed of each line separated by space, So you will get all the email ids.grep for hotmail.com if matches push into an array, finally use the aray with the recipient function from Net::SMTP.

use strict; use warnings; my @recipient; open(my $file, "<", "input.txt") or die "Can't open input.txt: $!"; my $string = "hotmail\.com"; while (<$file>) { chomp; my $emailid= (split/\s+/,$_) [2]; if (/$string/) { print "found string $string:$emailid\n"; push(@recipient,$emailid); } else { print "did not find the string\n"; } }

All is well

Replies are listed 'Best First'.
Re^2: How to send multiple user with one mail?
by GordonLim (Acolyte) on Mar 24, 2013 at 14:29 UTC
    Hi vinoth.ree, Your script is fine to grep all the mail. But I don't know why it cannot work when I apply in
    $smtp->mail(&xxx); $smtp->to(&xxx);
    or even i assigned to scalar also same
    my $email=&xxx; $smtp->mail($email); $smtp->to(email);
    Only work when I manual keyin email like this
    $smtp->mail('YYY@hotmail.com,' 'xxx@hotmail.com'); $smtp->to('YYY@hotmail.com,' 'xxx@hotmail.com');
    below is the script that I modified a bit from the script your provided.
    sub xxx { my $query = CGI->new; my $mail = CGI->new; my @recipient = (); open(my $file, "<", "users.txt") or die "Can't open input.txt: $!"; my $hotmail = "hotmail\.com"; my $gmail = "gmail\.com"; # print $query->header('text/html'); # create the HTTP header # print $query->start_html('Login Access'); # start the HTML while (<$file>) { chomp; my $emailid= (split/\s+/,$_) [2]; if (/$hotmail/) { push(@recipient,$emailid); } elsif (/$gmail/) { push(@recipient,$emailid); } } $email = join(', ', @recipient); return $email; }
    The result of join is same like this www@hotmail.com, xxx@hotmail.com is it the ' ' symbol I didn't add in beside the mail address during join function? I have no idea how to add in there.
      Hi All, The function is work after disable the join function. :) Thanks all of you guys help me.