Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Multiple addressees in PERL email script

Status
Not open for further replies.

Newposter

Technical User
May 9, 2002
735
US
How do I send a message to multiple addressees? I can't seem to get the syntax right. Here's my script:

send_mail($to, $from, $subject, $comments, $username, $userip, $useremail, $userurl, $daten, $timen);

sub send_mail {

use Net::SMTP;

my $relay = "domain.com";
my $smtp = Net::SMTP->new($relay)
|| die "Can't open mail connection: $!";

$smtp->mail($fromemail);
$smtp->to($useremail,$masteremail);

$smtp->data();
$smtp->datasend("To: $useremail,$masteremail\n");
$smtp->datasend("From: $fromemail\n");
$smtp->datasend("Subject: Thank you for visiting domain.com\n");
$smtp->datasend("\n");
$smtp->datasend("\n");
$smtp->datasend("\n");
$smtp->datasend("A copy of your comments can be found below\n");
$smtp->datasend("======================================\n");
$smtp->datasend("NAME: $username($userip) at $daten($timen)\n");
$smtp->datasend("EMAIL: $useremail\n");
$smtp->datasend("HOME PAGE: $userurl\n");
$smtp->datasend("\n");
$smtp->datasend("$comments\n");
$smtp->datasend("======================================\n");

$smtp->dataend();

It works if I send to $masteremail only, but not from $useremail only. $masteremail is hard-coded, $useremail is a form input which is parsed correctly and displayed in the resulting guestbook entry as a good hyperlink. Is this a simple syntax error? Newposter
"Good judgment comes from experience. Experience comes from bad judgment."
 
you are passing the scalars "incorrectly". what you need to do is in the sub saying something like:

Code:
($to, $from, $subject, $comments, $username, $userip, $useremail, $userurl, $daten, $timen) = @_;
you see @_ is what stores the incoming arguments in a sub. so you need to restore the names you want them to be referred to in the sub. also as good practice, it would be good to call send_mail sub using an & in front of the send_mail. this tells perl to search the current script for the sub before looking elsewhere.
see if that does anything.
--Derek

"Fear not the storm for this is where we grow strong."
 
OK, code now is:

&send_mail($to, $from, $subject, $comments, $username, $userip, $useremail, $userurl, $daten, $timen);

sub send_mail {

my($to, $from, $subject, $comments, $username, $userip, $useremail, $userurl, $daten, $timen) = @_;

use Net::SMTP;

my $relay = "domain.com";
my $smtp = Net::SMTP->new($relay)
|| die "Can't open mail connection: $!";

$smtp->mail($fromemail);
$smtp->to($masteremail,$useremail);

$smtp->data();
$smtp->datasend("To: $masteremail,$useremail\n");
$smtp->datasend("From: $fromemail\n");
$smtp->datasend("Subject: Thank you for visiting domain.com\n");
$smtp->datasend("\n");
$smtp->datasend("\n");
$smtp->datasend("\n");
$smtp->datasend("A copy of your comments can be found below\n");
$smtp->datasend("======================================\n");
$smtp->datasend(" NAME: $username($userip) at $daten($timen)\n");
$smtp->datasend(" EMAIL: $useremail\n");
$smtp->datasend(" HOME PAGE: $userurl\n");
$smtp->datasend(" \n");
$smtp->datasend(" $comments\n");
$smtp->datasend("======================================\n");

$smtp->dataend();
}

Now as $masteremail I receive a copy of the message as I did before, but this time the header shows:

To: webmaster@domain.com; guest@hisdomain.com

...which would appear to be working fine. However, the message never gets delivered to guest@hisdomain.com. So it's placing the guest's email address in the message now, but not delivering it. I don't know what the issue is here, but I'm a little closer to solving it. Newposter
"Good judgment comes from experience. Experience comes from bad judgment."
 
I've gotten it to work with multiple addresses. In fact I even put in:
Code:
$smtp->to($address1);
$smtp->to($address2);

and gotten both. You might try looking to be sure that the @ is escaped with a \ in the addresses. --Derek

"Fear not the storm for this is where we grow strong."
 
Near the beginning of my script is the line:

&set_var;

which calls the subroutine:

sub set_var {
$job =&get_var('job' ,'\n|¡ü');
$username =&get_var('username' ,'\n|&quot;|<|>|\&|;|\?|¡ü');
$useremail =&get_var('useremail' ,'\n|¡ü| ');
$userurl =&get_var('userurl' ,'\n|¡ü| ');
$cookieusername =&get_var('cookieusername' ,'\n|&quot;|<|>|\&|;|\?|¡ü');
$cookieuseremail=&get_var('cookieuseremail','\n|¡ü| ');
$cookieuserurl =&get_var('cookieuserurl' ,'\n|¡ü| ');
$comments =&get_var('comments' ,'¡ü');
$searchword =&get_var('searchword' ,'\n|¡ü');
$page =&get_var('page' ,'\n|¡ü');
$guestbookmark =&get_var('guestbookmark' ,'\n');
$manageid =&get_var('manageid' ,'\n|&quot;|<|>|\&|;| |\?|¡ü');
$managepassword =&get_var('managepassword' ,'\n|&quot;|<|>|\&|;| |\?|¡ü');

$useremail=&quot;&quot; if (!($useremail =~ /.*\@.*\..*/));
$userurl =&quot;&quot; if (!($userurl =~ /.*\:.*\..*/ ));
$comments =~ s/\n/<br>/g;

$cookieuseremail=&quot;&quot; if (!($cookieuseremail =~ /.*\@.*\..*/));
$cookieuserurl =&quot; if (!($cookieuserurl =~ /.*\:.*\..*/ ));

$userip=$ENV{'REMOTE_ADDR'};

($secn,$minn,$hourn,$dayn,$monn,$yearn,$weekn,$yeardayn,$isdst) = localtime(time+(3600*$time_miss));
$monn=$monn+1;
if ($monn<10) {$monn=&quot;0$monn&quot;;}
if ($dayn<10) {$dayn=&quot;0$dayn&quot;;}
if ($hourn<10) {$hourn=&quot;0$hourn&quot;;}
if ($minn<10) {$minn=&quot;0$minn&quot;;}
if ($secn<10) {$secn=&quot;0$secn&quot;;}
$yearn=$yearn+1900;
$daten=&quot;$yearn/$monn/$dayn&quot;;
$timen=&quot;$hourn\:$minn\:$secn&quot;;
}


This parses the $useremail correctly, and the resulting email hyperlink in the guestbook does work, but the email notification that is launched by the Net SMTP does not deliver to the visitor, only to the webmaster. The header info in the email to the webmaster does have the correct syntax to deliver to the user. Could it be this doesn't work because my mail server is not an open relay? Newposter
&quot;Good judgment comes from experience. Experience comes from bad judgment.&quot;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top