I'm assuming that, since you are using a form, the data is coming in from an application/x-www-form-urlencoded POST. You have the option of passing the data either as a single field value that you'll pull in as a scalar:
my $email_list = param ('Email_List');
or you can have a large number of inputs (one for each address) and use array access to the parameters:
my @email_list = param ('Email');
Note that this capability to have repeat keys in HTTP requests has caused a large number of issues over the years, because people can completely miss entries if they call param in scalar context. This means it might be a future gotcha. You'll also probably want some javascript on you page for 'Add additional recipient' in that scenario.
Your post sounds like you would prefer a big cut-paste blob into a textbox, and then process that list on the server. A split as Corion suggests would then be a natural processing approach, but could easily be frustrated depending on how you are formatting you list. If you want to include addresses of the format
Smith, Peter (PERL) <peter.smith@perl.com>
you couldn't use commas as separators, for example. Regardless, you could implement your loop with something like:
##sendmail_email.pl
#!C:/Perl/bin/perl -w
use CGI qw(:standard);
use HTML::Entities;
use Mail::Sendmail 0.79; # doesn't work with v. 0.74!
my $Sender=param('Sender');
my $email_list=param('Email_List');
my $Subject=param('Subject')
my $Message = encode_entities(param ('Message')); #SEE WHAT I DID HERE
$html = <<END_HTML;
<p>$Message</p><p><font size="2">
END_HTML
for my $email (split /;/, $email_list) {
my %mail;
$mail{Smtp}="smtp.sendgrid.net";
$mail{Port}=587;
$mail{Auth}={user=>"Milt1", password=>"XXXXXXX", method => "LOGIN"
+, required => 1};
$mail{To}=$email;
$mail{From}= $Sender;
$mail{Subject}=$Subject;
$mail{'content-type'} = 'text/html; charset="iso-8859-1"';
$mail{body} = <<END_OF_BODY;
<html>$html</html>
END_OF_BODY
sendmail(%mail) || print "Error: $Mail::Sendmail::error\n";
}
&redirect;
sub redirect {
print header(), start_html;
print "Your Message Was Sent\n\n";
}
exit;
Finally, as a side note, depending on how visible this service is, you have some mighty security holes present there. At the least, someone can use this as a SPAM generator, and will be able to do so even more once they could submit 1000 email addresses in a go. There are also like some injection attack sensitivities, since you have not done any input filtering and are just interpolating.
#11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.
|