sulfericacid has asked for the wisdom of the Perl Monks concerning the following question:

Finally...the script is so close to being done but I've run into another small problem. During a mailing if you print [name] in any of the fields it should replace it with the person's name from the db. It works fine in both the message and the signature field, it gives each of them their name. The problem is the $subject field, no matter who's turn it is to get emailed it will post the first name it finds in the db.

Example database:

sulfericacid@qwest.net > Aaron Anderson someone@overthere.com > Bill Nye bigbird@bigyellowfeathers.net > Big Bird
Each subject line with [name] in this case would print "Aaron Anderson" rather than that person's actual name. Can someone point out why this would happen?

I was also wondering if there was a nicer way to design tables with forms in CGI, this way works but it's a bit more complicated than I'd like to use.

Thanks everyone!!

"Age is nothing more than an inaccurate number bestowed upon us at birth as just another means for others to judge and classify us"

sulfericacid

Replies are listed 'Best First'.
Re: Variable substitution
by dws (Chancellor) on Apr 12, 2003 at 19:18 UTC
    Each subject line with name in this case would print "Aaron Anderson" rather than that person's actual name. Can someone point out why this would happen?

    Consider $subject. The first time into the loop, it might read  [name]! You can enlarge your whatever! Then you swap in a name, and send the first email.

    So what is $subject going to be the second time through the loop?

    Aaron Anderson! You can enlarge your whatever!
    There's nothing left to substitute.

    It looks like you need to make copies of both $subject and $messages each trip through the loop, and expand macros in the copies.

Re: Variable substitution
by theorbtwo (Prior) on Apr 12, 2003 at 19:12 UTC

    You might want to learn a templating module, rather then rolling your own, and using CGI's ugly HTML-generating code. (Perl should look like Perl, and HTML should look like HTML.) In specific, I recommend Text::Template as being very easy to learn.


    Warning: Unless otherwise stated, code is untested. Do not use without understanding. Code is posted in the hopes it is useful, but without warranty. All copyrights are relinquished into the public domain unless otherwise stated. I am not an angel. I am capable of error, and err on a fairly regular basis. If I made a mistake, please let me know (such as by replying to this node).

Re: Variable substitution
by sulfericacid (Deacon) on Apr 12, 2003 at 19:06 UTC
    Oh my gosh!! I totally forgot to post the code :( Sorry everyone...
    #!/usr/bin/perl -w open (STDERR, ">>/home/sulfericacid/public_html/test/error.log") or die "Cannot open error log, weird...an error opening an error lo +g: $!"; use strict; use warnings; use POSIX; use CGI qw/:standard/; require SDBM_File; my %sig; my %emails; my $editsig; my $sigsave = "sig.dbm"; my $lock = "pass"; #password my $list = "list.dbm"; my $adminmail = "admin\@test.com"; my $sendmail = "/usr/lib/sendmail"; my $unsub = "http://sulfericacid.perlmonk.org/test/how.pl"; # uns +ub link tie %emails, 'SDBM_File', $list, O_CREAT | O_RDWR, 0644; if ( !tied %emails ) { print "database unsuccessful $!.\n"; } tie %sig, 'SDBM_File', $sigsave, O_CREAT | O_RDWR, 0644; if ( !tied %sig ) { print "database unsuccessful $!.\n"; } print header, start_html('Email Management'); print start_form(), table( Tr( td("Subject: "), td( textfield( -name => 'subject', -size => 40 ) ) ), Tr( td("Message: "), td( textarea( -name => 'message', -columns => 40, -rows => 5 ) ) ), Tr( td("Signature: "), td( textarea( -name => 'signature', -default => $sig{'default'}, -columns => 40, -rows => 5 ) ) ), Tr( td("Password: "), td( password_field( -name => 'password', -size => 10 ) ) ), Tr( td( checkbox(-name=>'use', -value=>'yes', -label=>'Use signature'), ) ), Tr( td( checkbox(-name=>'save', -value=>'yes', -label=>'Save signature'), ) ), td(submit('button','submit')) ), end_form(), hr(); if ( param() ) { # rid ourselves from those nasty params my $message = param('message'); my $password = param('password'); my $subject = param('subject'); my $signature = param('signature'); my $save = param('save'); my $use = param('use'); my $time = localtime; if ($password ne $lock) { print "Wrong password. Email rejected from server.\n"; } else { if ( $message eq "" || $subject eq "" ) { print "Your subject or email content was missing.\n"; exit; } else { if ( $save eq "yes" ) { print "<br>"; print "Saving to database<br>\n"; $sig{'default'} = $signature; $sig{'stored'} = $sig{'default'}; print "\$sig{'default'}: $sig{'default'}<br><br>\n"; } foreach my $key ( keys %emails ) { print "Things we have: $key <br>\n"; } print "<br>\n"; while ( my ( $key, $value ) = each(%emails) ) { # Email Subs, special commands $message =~ s/\[name\]/$value/g; #[name] = user name $message =~ s/\[time\]/$time/g; #[time] = time sent $message =~ s/\[unsub\]/$unsub/g; #[unsub] = unsubscribe + email $editsig = $signature; # so we can fiddle wit +h it $editsig =~ s/\[name\]/$value/g; #[name] = user name $editsig =~ s/\[time\]/$time/g; #[time] = time sent $editsig =~ s/\[unsub\]/$unsub/g; #[unsub] = unsubscribe + email $subject =~ s/\[name\]/$value/g; #[name] = user name open( MAIL, "| $sendmail -t" ); print MAIL "To: $key\n"; print MAIL "From: $adminmail\n"; print MAIL "Subject: $subject\n\n"; print MAIL "$message\n"; if ( $use eq "yes" && $signature ne "" ) { print MAIL "$editsig\n"; } print MAIL ".\n"; close(MAIL); print "Email was sent to: $key !<br>"; } } } } untie %emails; untie %sig;


    "Age is nothing more than an inaccurate number bestowed upon us at birth as just another means for others to judge and classify us"

    sulfericacid