Gossamer Forum
Home : General : Perl Programming :

Merge CGI problem

Quote Reply
Merge CGI problem
Hey guys.

I have a script that mails an auto reply and
I would like to be able to add variables such as [name] etc. into the text file that is aent as the reply and have the script place my variables into the mail so it will be personalized. Below I have pasted a piece of the code and comented on where I am having trouble.

#############################################
open(MAIL,"|$mailprog -t");

print MAIL "To: $input{'email'}\n";
print MAIL "From: $email\n";
print MAIL "Subject: Application Notification!\n\n";
print MAIL "We have received your application!\n\n";

print MAIL " URL: $input{'url'}\n";
print MAIL " E-mail: $input{'email'}\n";
print MAIL "\n";
# What do I need to add to the following
# section to insert the variables into the
# mail so it's personalized?
if (open (REPLY,"<$reply_text"))
{ @reply_text = <REPLY>;
print MAIL "@reply_text";
close (REPLY);
close (MAIL);
}
#############################################

Any ideas?

Dave
Quote Reply
Re: Merge CGI problem In reply to
Since it doesn't appear that the reply text has anything special that is parsed, you could do this one of two ways. First, you could create a parsing system and create a template like Alex does with the links system. This could end up being quite a bit of code. Otherwise, you could put the following bit of code in as a replacement:
Code:
# What do I need to add to the following
# section to insert the variables into the
# mail so it's personalized?
print qq~
Greetings $in{'name'},
This message was sent to you regarding $in{'subject'}. Blah blah blah... etc.
~;
# And so forth.
Using template code would give you more control of the text file(s) that are normally delivered, but maybe this will help you get your feet wet.


------------------
Fred Hirsch
Web Consultant & Programmer
Quote Reply
Re: Merge CGI problem In reply to
Thanks for the reply!

I do know how to add that line to insert my variables in the script itself but if you look at the code part I pasted it opens a text file from $text_reply

if (open (REPLY,"<$reply_text"))
{ @reply_text = <REPLY>;
print MAIL "@reply_text";
close (REPLY);

I would want to add variables to to the actual text script and have the program insert the personalized variables where I define them throughout the text message. This way all I would need to do to update any messages is just change the text file. I will be using it for special promotions so it will need to be changed quite a bit and it is a pain to keep changing the script.

Thanks Fred, I am getting there.
Quote Reply
Re: Merge CGI problem In reply to
What you'll have to do then, is to design a method to parse that incoming text, and replace specially designed fields so that those items are replaced by variables. This is called templating, and its something that Alex has done with Links on a fairly grand scale. As my example, I will use something VERY simple, Alex's implimentation is probably a bit more advanced than this:
Code:
# First we need to setup the template variable
# You will need to setup one line for each
# template item you wish to replace.

%template ( name => $in{'name'},
email => $in{'email'},
comment => $in{'comment}
)
# These are the begin and end of the template
# tags themselves. As an example <%name%> would
# be replaced with the form field for name submitted
# by the user.
$begin = '<%';
$end = '%>';

# What do I need to add to the following
# section to insert the variables into the
# mail so it's personalized?

if (open (REPLY,"<$reply_text")) {
@reply_text = <REPLY>;
foreach $line (@reply_text) {
s/$begin\s*(.+?)\s*$end/
if (exists $template{$1}) {
$template{$1};
} else {
return "Unknown Tag: $1";
}/goe;
$parsed_output .= $line . "\n";
}
print MAIL "$parsed_output";
close (REPLY);
close (MAIL);
}
This should provide you the basic functionality you are looking for. Since I haven't tested it with a real example, let me know how it works.


------------------
Fred Hirsch
Web Consultant & Programmer
Quote Reply
Re: Merge CGI problem In reply to
Fred

I tried to get it to work and failed, there were a couple typo's that I fixed but it still does not sub my defined variables. It just sends the template file without substituting any form fields. Here is the below code:

print MAIL " First Name: $input{'fname'}\n";
print MAIL " Last Name: $input{'lname'}\n";
print MAIL " E-mail: $input{'email'}\n";

print MAIL "\n";

%template = ( fname => $input{'fname'},
email => $input{'email'},
lname => $input{'lname'}
);

$begin = '<%';
$end = '%>';
if (open (REPLY,"<$reply_text")) {
@reply_text = <REPLY>;
foreach $line (@reply_text) {
s/$begin\s*(.+?)\s*$end/
if (exists $template{$1}) {
$template{$1};
} else {
return "Unknown Tag: $1";
}/goe;
$parsed_output .= $line . "";
}
print MAIL "$parsed_output";
close (REPLY);
close (MAIL);

Any ideas? it's got to be something small since the code is not elaborate.

Dave
Quote Reply
Re: Merge CGI problem In reply to
"There must have been a door there in the wall.." Or a bug. Here is the corrected (and tested) code:
Code:
%template = ( fname => $input{'fname'},
email => $input{'email'},
lname => $input{'lname'}
);

$begin = '<%';
$end = '%>';

if (open (REPLY,"<$reply_text")) {
@reply_text = <REPLY>;
close (REPLY);
}

foreach $line (@reply_text) {
$line =~ s/$begin\s*(.+?)\s*$end/
if (exists $template{$1}) {
$template{$1};
} else {
return "Unknown Tag: $1";
}/goe;
$parsed_output .= $line;
}
print MAIL "$parsed_output";
Important things to note:
1) This is slightly different than the old code, the most important part is that I forgot to use $line =~ s/blah....
2)$reply_text must be defined somewhere in the program
3) The text in reply_text should have tags where you want to replace fields: Ex <%fname%>,<%lname%>, etc.

Hope this helps, sorry for the bugs.


------------------
Fred Hirsch
Web Consultant & Programmer
Quote Reply
Re: Merge CGI problem In reply to
I added the new code to the script but all I get is a 500 server error, I give up.... I'll have to just modify the script when I need to change the text.

Thanks for the help