CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
need help with adding multiple recipients to script

 



pena007
New User

Feb 18, 2014, 4:44 PM

Post #1 of 8 (1308 views)
need help with adding multiple recipients to script Can't Post

Hello people,
Can anybody please help me with adding multiple recipients to this script? Right now its working only for single recipient.
I need to make it work for multiple recipients.


Code
#!/usr/bin/perl 
use warnings;
use strict;
use Net::SMTP;

send_mail('127.0.0.1', # SMTP
'sender@nowhere.com', # From
'myself@myhost.com', # To
'Just a test, please ignore', # Message body
"Testing email\n" # Subject
);

sub send_mail {
my ($SMTP_HOST, $from, $to_addr, $body, $subject, $msg) = @_;

$msg = "MIME-Version: 1.0\n"
. "From: $from\n"
. "To: " . ( ref($to_addr) ? join(';', @$to_addr) : $to_addr ) . "\n"
. "Subject: $subject\n\n" # Double \n
. $body;

#
# Open a SMTP session
#
my $smtp = Net::SMTP->new( $SMTP_HOST,
'Debug' => 1, # Change to a 1 to turn on debug messages
Port => 25, #PORT
);

die("SMTP ERROR: Unable to open smtp session.\n")
if(!defined($smtp) || !($smtp));

die("Failed to set FROM address\n")
if (! ($smtp->mail( $from ) ) );

die("Failed to set recipient\n")
if (! ($smtp->recipient( ( ref($to_addr) ? @$to_addr : $to_addr ) ) ) );

$smtp->data( $msg );

$smtp->quit;
}



(This post was edited by FishMonger on Feb 18, 2014, 4:48 PM)


Zhris
Enthusiast

Feb 18, 2014, 4:48 PM

Post #2 of 8 (1300 views)
Re: [pena007] need help with adding multiple recipients to script [In reply to] Can't Post

Pass in an array of recipients instead:


Code
send_mail('127.0.0.1', # SMTP  
'sender@nowhere.com', # From
[ 'myself@myhost.com', 'myself2@myhost.com' ], # To
'Just a test, please ignore', # Message body
"Testing email\n" # Subject
);


Chris


pena007
New User

Feb 18, 2014, 5:54 PM

Post #3 of 8 (1284 views)
Re: [Zhris] need help with adding multiple recipients to script [In reply to] Can't Post

Hello Chris,
Thank you for your quick reply, but now the recepients can see in their message all other recepients like:

"From: sender@nowhere.com
To: myself@myhost.com, myself2@myhost.com, etc."

I think this script needs requires more work to make it a good mailer.


Zhris
Enthusiast

Feb 18, 2014, 6:03 PM

Post #4 of 8 (1280 views)
Re: [pena007] need help with adding multiple recipients to script [In reply to] Can't Post

Yes, you could adapt the script to include a BCC option (blind carbon copy).

This is untested:


Code
#!/usr/bin/perl  
use warnings;
use strict;
use Net::SMTP;

send_mail('127.0.0.1', # SMTP
'sender@nowhere.com', # From
[ 'myself@myhost.com' ], # To
[ 'bccmyself@myhost.com' ], # Bcc
'Just a test, please ignore', # Message body
"Testing email\n" # Subject
);

sub send_mail {
my ($SMTP_HOST, $from, $to_addr, $bcc, $body, $subject) = @_;

$bcc //= '';

my $msg = "MIME-Version: 1.0\n"
. "From: $from\n"
. "To: " . ( ref($to_addr) ? join(';', @$to_addr) : $to_addr ) . "\n"
. "Bcc: " . ( ref($bcc) ? join(';', @$bcc) : $bcc ) . "\n"
. "Subject: $subject\n\n" # Double \n
. $body;

#
# Open a SMTP session
#
my $smtp = Net::SMTP->new( $SMTP_HOST,
'Debug' => 1, # Change to a 1 to turn on debug messages
Port => 25, #PORT
);

die("SMTP ERROR: Unable to open smtp session.\n")
if(!defined($smtp) || !($smtp));

die("Failed to set FROM address\n")
if (! ($smtp->mail( $from ) ) );

die("Failed to set recipient\n")
if (! ($smtp->recipient( ( ref($to_addr) ? @$to_addr : $to_addr ) ) ) );

$smtp->data( $msg );

$smtp->quit;
}


Chris


(This post was edited by Zhris on Feb 18, 2014, 6:21 PM)


rammohan
Novice

Feb 19, 2014, 2:50 AM

Post #5 of 8 (1244 views)
Re: [pena007] need help with adding multiple recipients to script [In reply to] Can't Post

hello can any one tel me how to add file's in that script. Please let me knew


(This post was edited by rammohan on Feb 19, 2014, 2:51 AM)


FishMonger
Veteran / Moderator

Feb 19, 2014, 6:29 AM

Post #6 of 8 (1235 views)
Re: [rammohan] need help with adding multiple recipients to script [In reply to] Can't Post

Use a higher level mail module which supports attachments.

Mail::Sender - module for sending mails with attachments through an SMTP server
http://search.cpan.org/~jenda/Mail-Sender-0.8.22/Sender.pm

MIME::Lite - low-calorie MIME generator
http://search.cpan.org/~rjbs/MIME-Lite-3.030/lib/MIME/Lite.pm


Zhris
Enthusiast

Feb 19, 2014, 10:13 AM

Post #7 of 8 (1219 views)
Re: [rammohan] need help with adding multiple recipients to script [In reply to] Can't Post

You can use Net::SMTP to handle attachments, something like:


Code
$smtp->datasend("Content-Type: application/text; name= filename.txt "); 
$smtp->datasend();
$smtp->datasend($filedata);


But FishMonger has suggested better alternatives, although I would recommend using MIME::Entity over MIME::Lite.


rammohan
Novice

Feb 19, 2014, 9:05 PM

Post #8 of 8 (1204 views)
Re: [Zhris] need help with adding multiple recipients to script [In reply to] Can't Post

Thank you for your answers

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives