Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

How to send multiple user with one mail?

by GordonLim (Acolyte)
on Mar 20, 2013 at 18:16 UTC ( #1024583=perlquestion: print w/ replies, xml ) Need Help??
GordonLim has asked for the wisdom of the Perl Monks concerning the following question:

Hi All, Currently I know how to send out mail for 1 person, may I know how can i send out more than one person in the same mail and the e-mail address is grep from other file. I think should join with array function, but I have no idea how to grep every hotmail.com and join in the mail script. Here is my mail script:
sub sendpasstoemail { my $smtp; my $pingStatus; my $localcount; my $subject = "User \'$1\' Booking Request for ATCA Chassis"; my $string = <<INFORM Hi All, How are you? Thanks Best Regard, $username NOTE: This is an auto-generated email, please do not reply. INFORM ; $pingStatus = &checkTargetPing("ORCAS01.hotmail.com"); if ($pingStatus eq 1) { $smtp = Net::SMTP->new('ORCAS01.hotmail.com'); $smtp->mail($3); $smtp->to($3); $smtp->data(); $smtp->datasend("From: Booking and Managment system\n"); $smtp->datasend("To:$3\n"); $smtp->datasend("Subject: $subject\n"); $smtp->datasend("\n"); $smtp->datasend("$string\n"); $smtp->dataend(); $smtp->quit; print $query->h2({-align=>'center'},"Your request message +is sent to Chassis user"); }
below is my .txt file format:
#username password email address +privilege gordon gordon1234 gordon@hotmail.com + admin danish danish1234 Danish@hotmail.com + user johson johson1234 johson@hotmail.com + user

Comment on How to send multiple user with one mail?
Select or Download Code
Re: How to send multiple user with one mail?
by kschwab (Priest) on Mar 20, 2013 at 18:34 UTC
    <nevermind...misunderstood q>
Re: How to send multiple user with one mail?
by hdb (Prior) on Mar 20, 2013 at 20:55 UTC

    I am a bit confused. $smtp->mail specifies the sender, $smtp->to a recipient. In your case they are the same???

    Anyways, using $smtp->recipient( $address1, $address2, $address3, etc) works for me.

    For every complex problem there is an answer that is clear, simple, and wrong. H. L. Mencken
      Hi H. L. Mencken, I was trying to do the dynamic recipient. Meant I hope can do every recipient can grep from my that .txt file all got @hotmail.com. Sorry if my explanation make you feel confuse.
Re: How to send multiple user with one mail?
by vinoth.ree (Parson) on Mar 21, 2013 at 06:05 UTC

    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
      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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1024583]
Approved by Old_Gray_Bear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2014-12-26 02:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (164 votes), past polls