PDA

View Full Version : Whats wrong with this form script?


bmgrafx
12-14-2005, 12:14 AM
Hi all,
I've been trying to get a hold on php and perl for a while now and i haven't gotten any better. Most form scripts I try don't work. But everytime I get a simple script to working, I alter small things like changing the test email recipient's address from mine to a customers, everything messes up. I've done small changes to scripts and uploaded them after each small change. Eventually it will stop working, even after i revert back to the previous version that did work.

So now I have this email script that works only when I fill out the form and enters my admin@bmgrfx.net email address in the input box. Here's the script

FormMail.pl:

#!/usr/bin/perl
$mailprog = '/usr/sbin/sendmail -i -t';
@referers = ('bmgrafx.net','brex.bmgrafx.net','aol.com');
@recipients = ('admin@bmgrafx.net','billruck@aol.com');
@valid_ENV = ('REMOTE_HOST','REMOTE_ADDR','REMOTE_USER','HTTP_USER_AGENT','HTTP_REFERER');
&check_url;
&get_date;
&parse_form;
&check_required;
&send_mail;
&return_html;
sub check_url {
local($check_referer) = 0;
if ($ENV{'HTTP_REFERER'}) {
foreach $referer (@referers) {
if ($ENV{'HTTP_REFERER'} =~ m|https?://([^/]*)$referer|i) {
$check_referer = 1;
last;
}
}
}
else {
$check_referer = 1;
}

if ($check_referer != 1) { &error('bad_referer') }
}

sub get_date {
@days = ('Sunday','Monday','Tuesday','Wednesday',
'Thursday','Friday','Saturday');
@months = ('January','February','March','April','May','June','July',
'August','September','October','November','December');

# 1900 to the year to get the full 4 digit year. #
($sec,$min,$hour,$mday,$mon,$year,$wday) = (localtime(time))[0,1,2,3,4,5,6];
$time = sprintf("%02d:%02d:%02d",$hour,$min,$sec);
$year += 1900;
$date = "$days[$wday], $months[$mon] $mday, $year at $time";

}
sub parse_form {

# Define the configuration associative array. #
%Config = ('recipient','', 'subject','',
'email','', 'realname','',
'redirect','', 'bgcolor','',
'background','', 'link_color','',
'vlink_color','', 'text_color','',
'alink_color','', 'title','',
'sort','', 'print_config','',
'required','', 'env_report','',
'return_link_title','', 'return_link_url','',
'print_blank_fields','', 'missing_fields_redirect','');
if ($ENV{'REQUEST_METHOD'} eq 'GET') {
# Split the name-value pairs
@pairs = split(/&/, $ENV{'QUERY_STRING'});
}
elsif ($ENV{'REQUEST_METHOD'} eq 'POST') {
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
}
else {
&error('request_method');
}
foreach $pair (@pairs) {
local($name, $value) = split(/=/, $pair);
$name =~ tr/+/ /;
$name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$name =~ tr/\0//d;

$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$value =~ tr/\0//d;

if (defined($Config{$name})) {
$Config{$name} = $value;
}
else {
if ($Form{$name} ne '') {
$Form{$name} = "$Form{$name}, $value";
}
else {
push(@Field_Order,$name);
$Form{$name} = $value;
}
}
}

$Config{'required'} =~ s/(\s+|\n)?,(\s+|\n)?/,/g;
$Config{'required'} =~ s/(\s+)?\n+(\s+)?//g;
$Config{'env_report'} =~ s/(\s+|\n)?,(\s+|\n)?/,/g;
$Config{'env_report'} =~ s/(\s+)?\n+(\s+)?//g;
$Config{'print_config'} =~ s/(\s+|\n)?,(\s+|\n)?/,/g;
$Config{'print_config'} =~ s/(\s+)?\n+(\s+)?//g;

@Required = split(/,/,$Config{'required'});
@Env_Report = split(/,/,$Config{'env_report'});
@Print_Config = split(/,/,$Config{'print_config'});

# @Env_Report for security reasons. #
foreach $env_item (@Env_Report) {
foreach $valid_item (@valid_ENV) {
if ( $env_item eq $valid_item ) { push(@temp_array, $env_item) }
}
}
@Env_Report = @temp_array;
}

sub check_required {

# Localize the variables used in this subroutine. #
local($require, @error);

if ($Config{'subject'} =~ /(\n|\r)/m || $Config{'email'} =~ /(\n|\r)/m ||
$Config{'realname'} =~ /(\n|\r)/m || $Config{'recipient'} =~ /(\n|\r)/m) {
&error('invalid_headers');
}
if (!$Config{'recipient'}) {
if (!defined(%Form)) { &error('bad_referer') }
else { &error('no_recipient') }
}
else {
$valid_recipient = 0;
foreach $send_to (split(/,/,$Config{'recipient'})) {
foreach $recipient (@recipients) {
if ($send_to =~ /$recipient$/i) {
push(@send_to,$send_to); last;
}
}
}
if ($#send_to < 0) { &error('no_recipient') }
$Config{'recipient'} = join(',',@send_to);
}

foreach $require (@Required) {

if ($require eq 'email' && !&check_email($Config{$require})) {
push(@error,$require);
}

elsif (defined($Config{$require})) {
if ($Config{$require} eq '') { push(@error,$require); }
}

elsif (!defined($Form{$require}) || $Form{$require} eq '') {
push(@error,$require);
}
}

if (@error) { &error('missing_fields', @error) }
}

sub return_html {
local($key,$sort_order,$sorted_field);
local($field);
foreach $field (keys %Config) {
$safeConfig{$field} = &clean_html($Config{$field});
}

foreach $field (keys %Form) {
$Form{$field} = &clean_html($Form{$field});
}

if ($Config{'redirect'}) {
print "Location: $safeConfig{'redirect'}\n\n";
}
else {
print "Content-type: text/html\n\n";
print "<html>\n <head>\n";
if ($Config{'title'}) { print "<title>$safeConfig{'title'}</title>\n" }
else { print "<title>Thank You</title>\n" }

print " </head>\n <body";
&body_attributes;
print ">\n <center>\n";
if ($Config{'title'}) { print "<h1>$safeConfig{'title'}</h1>\n" }
else { print "<h1>Thank You For Filling Out This Form</h1>\n" }

print "</center>\n";
print "Below is what you submitted to $safeConfig{'recipient'} on ";
print "$date<p><hr size=1 width=75\%><p>\n";
if ($Config{'sort'} =~ /^order:.*,.*/) {
$sort_order = $Config{'sort'};
$sort_order =~ s/(\s+|\n)?,(\s+|\n)?/,/g;
$sort_order =~ s/(\s+)?\n+(\s+)?//g;
$sort_order =~ s/order://;
@sorted_fields = split(/,/, $sort_order);
foreach $sorted_field (@sorted_fields) {
local $sfname = &clean_html($sorted_field);

if ($Config{'print_blank_fields'} || $Form{$sorted_field} ne '') {
print "<b>$sfname:</b> $Form{$sorted_field}<p>\n";
}
}
}
else {
# Sort alphabetically if requested.
if ($Config{'sort'} eq 'alphabetic') {
@Field_Order = sort @Field_Order;
}
foreach $field (@Field_Order) {
local $fname = &clean_html($field);

if ($Config{'print_blank_fields'} || $Form{$field} ne '') {
print "<b>$fname:</b> $Form{$field}<p>\n";
}
}
}

print "<p><hr size=1 width=75%><p>\n";

if ($Config{'return_link_url'} && $Config{'return_link_title'}) {
print "<ul>\n";
print "<li><a href=\"$safeConfig{'return_link_url'}\">$safeConfig{'return_link_title'}</a>\n";
print "</ul>\n";
}

# Print the page footer. #
print <<"(END HTML FOOTER)";
<hr size=1 width=75%><p>
<center><font size=-1><a href="http://www.scriptarchive.com/formmail.html">FormMail</a> V1.92 &copy; 1995 - 2002 Matt Wright<br>
A Free Product of <a href="http://www.scriptarchive.com/">Matt's Script Archive, Inc.</a></font></center>
</body>
</html>
(END HTML FOOTER)
}
}

sub send_mail {
local($print_config,$key,$sort_order,$sorted_field,$env_report);
open(MAIL,"|$mailprog");

print MAIL "To: $Config{'recipient'}\n";
print MAIL "From: $Config{'email'} ($Config{'realname'})\n";

# Check for Message Subject
if ($Config{'subject'}) { print MAIL "Subject: $Config{'subject'}\n\n" }
else { print MAIL "Subject: WWW Form Submission\n\n" }

print MAIL "Below is the result of your feedback form. It was submitted by\n";
print MAIL "$Config{'realname'} ($Config{'email'}) on $date\n";
print MAIL "-" x 75 . "\n\n";

if (@Print_Config) {
foreach $print_config (@Print_Config) {
if ($Config{$print_config}) {
print MAIL "$print_config: $Config{$print_config}\n\n";
}
}
}

# If a sort order is specified, sort the form fields based on that. #
if ($Config{'sort'} =~ /^order:.*,.*/) {
local $sort_order = $Config{'sort'};
$sort_order =~ s/(\s+|\n)?,(\s+|\n)?/,/g;
$sort_order =~ s/(\s+)?\n+(\s+)?//g;
$sort_order =~ s/order://;
@sorted_fields = split(/,/, $sort_order);
foreach $sorted_field (@sorted_fields) {
if ($Config{'print_blank_fields'} || $Form{$sorted_field} ne '') {
print MAIL "$sorted_field: $Form{$sorted_field}\n\n";
}
}
}
else {

if ($Config{'sort'} eq 'alphabetic') {
@Field_Order = sort @Field_Order;
}
foreach $field (@Field_Order) {
if ($Config{'print_blank_fields'} || $Form{$field} ne '') {
print MAIL "$field: $Form{$field}\n\n";
}
}
}
print MAIL "-" x 75 . "\n\n";
foreach $env_report (@Env_Report) {
if ($ENV{$env_report}) {
print MAIL "$env_report: $ENV{$env_report}\n";
}
}

close (MAIL);
}

sub check_email {
$email = $_[0];
if ($email =~ /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/ ||
$email !~ /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z0-9]+)(\]?)$/) {
return 0;
}

else {

# Return a true value, e-mail verification passed. #
return 1;
}
}


sub fill_recipients {
local(@domains) = @_;
local($domain,@return_recips);

foreach $domain (@domains) {
if ($domain =~ /^\d+\.\d+\.\d+\.\d+$/) {
$domain =~ s/\./\\\./g;
push(@return_recips,'^[\w\-\.]+\@\[' . $domain . '\]');
}
else {
$domain =~ s/\./\\\./g;
$domain =~ s/\-/\\\-/g;
push(@return_recips,'^[\w\-\.]+\@' . $domain);
}
}

return @return_recips;
}

# This function will convert <, >, & and " to their HTML equivalents. #
sub clean_html {
local $value = $_[0];
$value =~ s/\&/\&amp;/g;
$value =~ s/</\&lt;/g;
$value =~ s/>/\&gt;/g;
$value =~ s/"/\&quot;/g;
return $value;
}

sub body_attributes {
if ($Config{'bgcolor'}) { print " bgcolor=\"$safeConfig{'bgcolor'}\"" }
if ($Config{'background'}) { print " background=\"$safeConfig{'background'}\"" }
if ($Config{'link_color'}) { print " link=\"$safeConfig{'link_color'}\"" }
if ($Config{'vlink_color'}) { print " vlink=\"$safeConfig{'vlink_color'}\"" }
if ($Config{'alink_color'}) { print " alink=\"$safeConfig{'alink_color'}\"" }
if ($Config{'text_color'}) { print " text=\"$safeConfig{'text_color'}\"" }
}

sub error {
# Localize variables and assign subroutine input. #
local($error,@error_fields) = @_;
local($host,$missing_field,$missing_field_list);

if ($error eq 'bad_referer') {
if ($ENV{'HTTP_REFERER'} =~ m|^https?://([\w\.]+)|i) {
$host = $1;
my $referer = &clean_html($ENV{'HTTP_REFERER'});
print <<"(END ERROR HTML)";
Content-type: text/html

<html>
<head>
<title>Bad Referrer - Access Denied</title>
</head>
<body bgcolor=#FFFFFF text=#000000>
<center>
<table border=0 width=600 bgcolor=#9C9C9C>
<tr><th><font size=+2>Bad Referrer - Access Denied</font></th></tr>
</table>
<table border=0 width=600 bgcolor=#CFCFCF>
<tr><td>The form attempting to use
<a href="http://www.scriptarchive.com/formmail.html">FormMail</a>
resides at <tt>$referer</tt>, which is not allowed to access
this cgi script.<p>
If you are attempting to configure FormMail to run with this form, you need
to add the following to \@referers, explained in detail in the
<a href="http://www.scriptarchive.com/readme/formmail.html">README</a> file.<p>

Add <tt>'$host'</tt> to your <tt><b>\@referers</b></tt> array.<hr size=1>
<center><font size=-1>
<a href="http://www.scriptarchive.com/formmail.html">FormMail</a> V1.92 &copy; 1995 - 2002 Matt Wright<br>
A Free Product of <a href="http://www.scriptarchive.com/">Matt's Script Archive, Inc.</a>
</font></center>
</td></tr>
</table>
</center>
</body>
</html>
(END ERROR HTML)
}
else {
print <<"(END ERROR HTML)";
Content-type: text/html

<html>
<head>
<title>FormMail v1.92</title>
</head>
<body bgcolor=#FFFFFF text=#000000>
<center>
<table border=0 width=600 bgcolor=#9C9C9C>
<tr><th><font size=+2>FormMail</font></th></tr>
</table>
<table border=0 width=600 bgcolor=#CFCFCF>
<tr><th><tt><font size=+1>Copyright 1995 - 2002 Matt Wright<br>
Version 1.92 - Released April 21, 2002<br>
A Free Product of <a href="http://www.scriptarchive.com/">Matt's Script Archive,
Inc.</a></font></tt></th></tr>
</table>
</center>
</body>
</html>
(END ERROR HTML)
}
}

elsif ($error eq 'request_method') {
print <<"(END ERROR HTML)";
Content-type: text/html

<html>
<head>
<title>Error: Request Method</title>
</head>
<body bgcolor=#FFFFFF text=#000000>
<center>
<table border=0 width=600 bgcolor=#9C9C9C>
<tr><th><font size=+2>Error: Request Method</font></th></tr>
</table>
<table border=0 width=600 bgcolor=#CFCFCF>
<tr><td>The Request Method of the Form you submitted did not match
either <tt>GET</tt> or <tt>POST</tt>. Please check the form and make sure the
<tt>method=</tt> statement is in upper case and matches <tt>GET</tt> or <tt>POST</tt>.<p>

<center><font size=-1>
<a href="http://www.scriptarchive.com/formmail.html">FormMail</a> V1.92 &copy; 1995 - 2002 Matt Wright<br>
A Free Product of <a href="http://www.scriptarchive.com/">Matt's Script Archive, Inc.</a>
</font></center>
</td></tr>
</table>
</center>
</body>
</html>
(END ERROR HTML)
}

elsif ($error eq 'no_recipient') {
print <<"(END ERROR HTML)";
Content-type: text/html

<html>
<head>
<title>Error: Bad/No Recipient</title>
</head>
<body bgcolor=#FFFFFF text=#000000>
<center>
<table border=0 width=600 bgcolor=#9C9C9C>
<tr><th><font size=+2>Error: Bad/No Recipient</font></th></tr>
</table>
<table border=0 width=600 bgcolor=#CFCFCF>
<tr><td>There was no recipient or an invalid recipient specified in the data sent to FormMail. Please
make sure you have filled in the <tt>recipient</tt> form field with an e-mail
address that has been configured in <tt>\@recipients</tt>. More information on filling in <tt>recipient</tt> form fields and variables can be
found in the <a href="http://www.scriptarchive.com/readme/formmail.html">README</a> file.<hr size=1>

<center><font size=-1>
<a href="http://www.scriptarchive.com/formmail.html">FormMail</a> V1.92 &copy; 1995 - 2002 Matt Wright<br>
A Free Product of <a href="http://www.scriptarchive.com/">Matt's Script Archive, Inc.</a>
</font></center>
</td></tr>
</table>
</center>
</body>
</html>
(END ERROR HTML)
}

elsif ($error eq 'invalid_headers') {
print <<"(END ERROR HTML)";
Content-type: text/html

<html>
<head>
<title>Error: Bad Header Fields</title>
</head>
<body bgcolor=#FFFFFF text=#000000>
<center>
<table border=0 width=600 bgcolor=#9C9C9C>
<tr><th><font size=+2>Error: Bad Header Fields</font></th></tr>
</table>
<table border=0 width=600 bgcolor=#CFCFCF>
<tr><td>The header fields, which include <tt>recipient</tt>, <tt>email</tt>, <tt>realname</tt> and <tt>subject</tt> were
filled in with invalid values. You may not include any newline characters in these parameters.
More information on filling in these form fields and variables can be
found in the <a href="http://www.scriptarchive.com/readme/formmail.html">README</a> file.<hr size=1>

<center><font size=-1>
<a href="http://www.scriptarchive.com/formmail.html">FormMail</a> V1.92 &copy; 1995 - 2002 Matt Wright<br>
A Free Product of <a href="http://www.scriptarchive.com/">Matt's Script Archive, Inc.</a>
</font></center>
</td></tr>
</table>
</center>
</body>
</html>
(END ERROR HTML)
}

elsif ($error eq 'missing_fields') {
if ($Config{'missing_fields_redirect'}) {
print "Location: " . &clean_html($Config{'missing_fields_redirect'}) . "\n\n";
}
else {
foreach $missing_field (@error_fields) {
$missing_field_list .= "<li>" . &clean_html($missing_field) . "\n";
}

print <<"(END ERROR HTML)";
Content-type: text/html

<html>
<head>
<title>Error: Blank Fields</title>
</head>
<center>
<table border=0 width=600 bgcolor=#9C9C9C>
<tr><th><font size=+2>Error: Blank Fields</font></th></tr>
</table>
<table border=0 width=600 bgcolor=#CFCFCF>
<tr><td>The following fields were left blank in your submission form:<p>
<ul>
$missing_field_list
</ul><br>

These fields must be filled in before you can successfully submit the form.<p>
Please use your browser's back button to return to the form and try again.<hr size=1>
<center><font size=-1>
<a href="http://www.scriptarchive.com/formmail.html">FormMail</a> V1.92 &copy; 1995 - 2002 Matt Wright<br>
A Free Product of <a href="http://www.scriptarchive.com/">Matt's Script Archive, Inc.</a>
</font></center>
</td></tr>
</table>
</center>
</body>
</html>
(END ERROR HTML)
}
}

exit;
}

bmgrafx
12-14-2005, 12:15 AM
<form name="form" method="POST" action="http://bmgrafx.net/brex/cgi-bin/FormMail.pl">
<input type="hidden" name="recipient" value="billruck@aol.com,brex@bmgrafx.net">
<div align="center">
<table width="459" border="2" cellpadding="3" cellspacing="0" bordercolor="#000000" bgcolor="#FFFF00">
<tr valign="top">
<td width="117"><span class="style1"><font size="2" face="Verdana, Arial, Helvetica, sans-serif">Your
name </font></span></td>
<td width="342"><font color="#000066" size="2" face="Verdana, Arial, Helvetica, sans-serif">
<input name="realname" type="text" id="realname" size="40">
</font></td>
</tr>
<tr valign="top">
<td><span class="style1"><font size="2" face="Verdana, Arial, Helvetica, sans-serif">Your
e-mail</font></span></td>
<td><font color="#000066" size="2" face="Verdana, Arial, Helvetica, sans-serif">
<input name="email" type="text" id="email" size="40">
</font></td>
</tr>
<tr valign="top">
<td><span class="style1"><font size="2" face="Verdana, Arial, Helvetica, sans-serif">Message</font></span></td>
<td><font color="#000066" size="2" face="Verdana, Arial, Helvetica, sans-serif">
<textarea name="messg" cols="35" rows="4" id="messg"></textarea>
</font></td>
</tr>
<tr valign="top">
<td><span class="style1"><font size="2" face="Verdana, Arial, Helvetica, sans-serif">Email Purpose </font></span></td>
<td><strong>
<label>
<input type="radio" name="reason" value="Appointment Request">
Appointment</label>
&nbsp;&nbsp;
<label>
<input type="radio" name="reason" value="Viewer Comment">
Comment</label>
&nbsp;&nbsp;
<label>
<input type="radio" name="reason" value="Customer Support">
Support</label>
</strong></td>
</tr>
<tr valign="top">
<td>&nbsp;</td>
<td>
<input type=hidden name="subject" value="B&R Exterminators - Form Submitted">
<input type=hidden name="redirect" value="http://bmgrafx.net/brex/emailsuccess.php">
<input type=hidden name="required" value="realname,email,messg">
<input type=hidden name="env_report" value="REMOTE_HOST,REMOTE_ADDR,REMOTE_USER,HTTP_USER_AGENT,HTTP_REFERER">
<input type=hidden name="sort" value="order:realname,email,messg">
<input type=hidden name="print_config" value="realname,email,reason,mssg">


<input type="reset" name="Reset" value="Reset">
<input name="Submit" type="submit" id="Submit" value="Submit"></td>
</tr>
</table>
</div>
</form>


I'd also like the add the selection from the 'reason' radio buttons to the subject if possible.

bmgrafx
12-14-2005, 12:27 AM
#!/usr/bin/perl
print "Content-type: text/html\n\n";

$title='mail test';
$to='bmgrafx@aol.com';
$from= 'admin@bmgrafx.net';
$subject='Using Sendmail';
$bccaddy='bmgrafx@aol.com';
open(MAIL, "|/usr/sbin/sendmail -t");

## Mail Header
print MAIL "To: $to\n";
print MAIL "From: $from\n";
print MAIL "Subject: $subject\n\n";
## Mail Body
print MAIL "This is a test message from SBC Yahoo!\n";
print MAIL "$messg\n";

close(MAIL);

print "<html><head><title>$title</title></head>\n<body>\n\n";

## START HTML content
print "<h1>$title</h1>\n";
print "<p>A message has been sent from $email to B&amp;R Exterminators.";
## END HTML CONTENT
print "\n\n</body></html>";


which works but i cant figure out how to get my input box info to be sent in to email... all it emails is the 'This is a test message from SBC Yahoo!' bit.

FishMonger
12-14-2005, 12:38 AM
First, read what Matt, the author of that script, has to say about his script.
http://www.scriptarchive.com/nms.html

Then go and download the nms formmail script.
http://nms-cgi.sourceforge.net/scripts.shtml

The nms formmail script is considered by most to be the best formmail script.

FishMonger
12-14-2005, 12:42 AM
Here are some of the Perl email modules.
http://search.cpan.org/~yves/MIME-Lite-3.01/lib/MIME/Lite.pm
http://search.cpan.org/~chunzi/MIME-Lite-TT-HTML-0.01/lib/MIME/Lite/TT/HTML.pm
http://search.cpan.org/~gbarr/libnet-1.19/Net/SMTP.pm

bmgrafx
12-14-2005, 01:11 AM
Now it get a 500 Internal Server Error when I click submit...

Can you tell me how to get the second code I posted to send the variables name, email and messg?


#!/usr/bin/perl
print "Content-type: text/html\n\n";

$title='mail test';
$to='bmgrafx@al.com';
$from= 'admin@bgrafx.net';
$subject='Using Sendmail';
$bccaddy='bmgrafx@al.com';
open(MAIL, "|/usr/sbin/sendmail -t");

## Mail Header
print MAIL "To: $to\n";
print MAIL "From: $from\n";
print MAIL "Subject: $subject\n\n";
## Mail Body
print MAIL "This is a test message from SBC Yahoo!\n";
print MAIL "$messg\n";

close(MAIL);

print "<html><head><title>$title</title></head>\n<body>\n\n";

## START HTML content
print "<h1>$title</h1>\n";
print "<p>A message has been sent from $email to B&amp;R Exterminators.";
## END HTML CONTENT
print "\n\n</body></html>";

which works but i cant figure out how to get my input box info to be sent in to email... all it emails is the 'This is a test message from SBC Yahoo!' bit. Also I wanted to have a copied emailed to adminATbmgrafx.net

FishMonger
12-14-2005, 01:34 AM
There are a number of security and other issues that should be addressed, but I'll skip over them and just answer your question.

You need to use the CGI module to parse the form submission.
http://search.cpan.org/~lds/CGI.pm-3.15/CGI.pm

#!/usr/bin/perl

use CGI qw(:standard);

$name = param('realname');
$email = param('email');
$reason = param('reason');
$messg = "Name: $name\nEmail: $email\nReason: $reason";

print "Content-type: text/html\n\n";

$title='mail test';
$to='bmgrafx@al.com';
$from= 'admin@bgrafx.net';
$subject='Using Sendmail';
$bccaddy='bmgrafx@al.com';
open(MAIL, "|/usr/sbin/sendmail -t");

## Mail Header
print MAIL "To: $to\n";
print MAIL "From: $from\n";
print MAIL "Subject: $subject\n\n";
## Mail Body
print MAIL "This is a test message from SBC Yahoo!\n";
print MAIL "$messg\n";

close(MAIL);

print "<html><head><title>$title</title></head>\n<body>\n\n";

## START HTML content
print "<h1>$title</h1>\n";
print "<p>A message has been sent from $email to B&amp;R Exterminators.";
## END HTML CONTENT
print "\n\n</body></html>";

bmgrafx
12-14-2005, 02:21 AM
Thanks for all of your help Fishmonger, but I've gotten to my usualy point - where I understand nothing and all previous code stops working... Now I have this code below and its says A message has be sent to B&R Exterminators, but nothing reaches box - at first I atleast got the SBC Yahoo! text sent...

email.pl:

#!/usr/bin/perl

use CGI qw(:standard);

$name = param('realname');
$email = param('email');
$reason = param('reason');
$message = param('messg');
$messg = "Name: $name\nEmail: $email\nReason: $reason\nMessage: $message";

print "Content-type: text/html\n\n";

$title='Mail Testing';
$to='bmgrafx@aol.com';
$from= 'admin@bgrafx.net';
$subject='Using Sendmail';
$bccaddy='bmgrafx@aol.com';
open(MAIL, "|/usr/sbin/sendmail -t");

## Mail Header
print MAIL "To: $to\n";
print MAIL "From: $from\n";
print MAIL "Subject: $subject\n\n";
## Mail Body
print MAIL "This is a test message from BM Grafx\n";
print MAIL "$messg\n";

close(MAIL);

print "<html><head><title>$title</title></head>\n<body>\n\n";

## START HTML content
print "<h1>$title</h1>\n";
print "<p>A message has been sent from $email to B&amp;R Exterminators.";
## END HTML CONTENT
print "\n\n</body></html>";

and form script:

<form name="form" method="POST" action="http://bmgrafx.net/brex/cgi-bin/email.pl">

TEMOPORARILY DISABLED
<div align="center">
<table width="459" border="2" cellpadding="3" cellspacing="0" bordercolor="#000000" bgcolor="#FFFF00">
<tr valign="top">
<td width="117"><span class="style1"><font size="2" face="Verdana, Arial, Helvetica, sans-serif">Your
name </font></span></td>
<td width="342"><font color="#000066" size="2" face="Verdana, Arial, Helvetica, sans-serif">
<input name="realname" type="text" id="realname" size="40">
</font></td>
</tr>
<tr valign="top">
<td><span class="style1"><font size="2" face="Verdana, Arial, Helvetica, sans-serif">Your
e-mail</font></span></td>
<td><font color="#000066" size="2" face="Verdana, Arial, Helvetica, sans-serif">
<input name="email" type="text" id="email" size="40">
</font></td>
</tr>
<tr valign="top">
<td><span class="style1"><font size="2" face="Verdana, Arial, Helvetica, sans-serif">Message</font></span></td>
<td><font color="#000066" size="2" face="Verdana, Arial, Helvetica, sans-serif">
<textarea name="messg" cols="35" rows="4" id="messg"></textarea>
</font></td>
</tr>
<tr valign="top">
<td><span class="style1"><font size="2" face="Verdana, Arial, Helvetica, sans-serif">Email Purpose </font></span></td>
<td><strong>
<label>
<input type="radio" name="reason" value="Appointment Request">
Appointment</label>
&nbsp;&nbsp;
<label>
<input type="radio" name="reason" value="Viewer Comment">
Comment</label>
&nbsp;&nbsp;
<label>
<input type="radio" name="reason" value="Customer Support">
Support</label>
</strong></td>
</tr>
<tr valign="top">
<td>&nbsp;</td>
<td>

<input type=hidden name="redirect" value="http://bmgrafx.net/brex/emailsuccess.php">
<input type="reset" name="Reset" value="Reset">
<input name="Submit" type="submit" id="Submit" value="Submit"></td>
</tr>
</table>
</div>
</form>

Why does my code stop working after a while? I cant get a simple email script to work, first they do, then they don't!

FishMonger
12-14-2005, 02:40 AM
Lets add a little error reporting.

#!/usr/bin/perl

use warnings;
use CGI qw(:standard);
use CGI::Carp qw(fatalsToBrowser warningsToBrowser);

$name = param('realname');
$email = param('email');
$reason = param('reason');
$message = param('messg');
$messg = "Name: $name\nEmail: $email\nReason: $reason\nMessage: $message";

print "Content-type: text/html\n\n";
warningsToBrowser(1);

$title='Mail Testing';
$to='bmgrafx@aol.com';
$from= 'admin@bgrafx.net';
$subject='Using Sendmail';
$bccaddy='bmgrafx@aol.com';
open(MAIL, "|/usr/sbin/sendmail -t") or die "can't pipe to sendmail $!";

bmgrafx
12-14-2005, 02:55 AM
It works, thank you... Do you now how I can send it to myself also? Its going to my customer, but i want a copy so that I cant troubleshoot with greater knowledge. That last email script I had worked on his site for over 3 months and all of a sudden, it stopped sending emails. If I receive a copy too, atleast then i'll know when it stopped working correctly. I added the $bccaddy for me but i dont know how to incorporate it into the form.

Im going to try to figure out the security issues next.

Thanks for all your help Fishmonger!

FishMonger
12-14-2005, 03:16 AM
To send to multiple addresses, you can add them to the $to var or add a $cc or $bc field.

print MAIL "To: $to\n";
print MAIL "Cc: $cc\n";
print MAIL "Bc: $bc\n";
print MAIL "From: $from\n";

IMO, it's better to use one of Perl's mail modules. The MIME::Lite module is very easy to use and is what I use most of the time, next would be Net::SMTP. See the links that I previously posted.

bmgrafx
12-14-2005, 03:38 AM
Is there a reason why the $bc isnt working for me?


#!/usr/bin/perl

use warnings;
use CGI qw(:standard);
use CGI::Carp qw(fatalsToBrowser warningsToBrowser);

$name = param('realname');
$email = param('emailaddress');
$reason = param('reason');
$message = param('messg');
$messg = "Name: $name\nEmail: $email\nReason: $reason\nMessage: $message";

print "Content-type: text/html\n\n";
warningsToBrowser(1);

$title='Mail Testing';
$to='bmgrafx@aol.com';
$from= 'admin@bmgrafx.net';
$subject='B&R Exterminators.com -';
$bc='brex@bmgrafx.net';
open(MAIL, "|/usr/sbin/sendmail -t") or die "can't pipe to sendmail $!";

## Mail Header
print MAIL "To: $to\n";
print MAIL "Bc: $bc\n";
print MAIL "From: $from\n";
print MAIL "Subject: $subject $reason\n\n";
## Mail Body
print MAIL "This is a test message from BM Grafx\n";
print MAIL "$messg\n";

close(MAIL);

print "<html><head><title>$title</title></head>\n<body>\n\n";

## START HTML content
print "<h1>$title</h1>\n";
print "<p>A message has been sent from $email to B&amp;R Exterminators.";
## END HTML CONTENT
print "\n\n</body></html>";

FishMonger
12-14-2005, 03:44 AM
Oops, it should be Bcc not Bc.

bmgrafx
12-14-2005, 05:48 AM
Thanx for the ump-teenth time Fishmonger... For everone else who is perl & php illiterate like me... my final code is:


#!/usr/bin/perl

use warnings;
use CGI qw(:standard);
use CGI::Carp qw(fatalsToBrowser warningsToBrowser);

$name = param('realname');
$email = param('emailaddress');
$reason = param('reason');
$message = param('messg');
$messg = "Name: $name\nEmail: $email\nReason: $reason\nMessage: $message";

print "Content-type: text/html\n\n";
warningsToBrowser(1);

$title='B&R Exterminators - Email Sent';
$to='billruck@aol.com';
$from= 'admin@bmgrafx.net';
$subject='B&R Exterminators.com -';
$bc='admin@bmgrafx.net';
open(MAIL, "|/usr/sbin/sendmail -t") or die "can't pipe to sendmail $!";

## Mail Header
print MAIL "To: $to\n";
print MAIL "Bcc: $bc\n";
print MAIL "From: $from\n";
print MAIL "Subject: $subject $reason\n\n";
## Mail Body
print MAIL "This is a message from BM Grafx. Someone has emailed your site. Your contents is below:\n";
print MAIL "\n";
print MAIL "$messg\n";
print MAIL "\n";
print MAIL "\n";
print MAIL "\n";
print MAIL "If you are receiving this email in error, please forward this email back to us.\n";

close(MAIL);

print qq~

<html><head><META NAME=robots CONTENT="noindex,nofollow"><meta http-equiv="refresh" content="10;url=$goto"><html><head><TITLE>B&R Exterminators Email / Contact Us </title><body bgcolor="#FFFFFF" text="#000000" link="#425EBB" vlink="#307992" alink="#FF0000" topmargin="0" leftmargin="0" bottommargin="0" rightmargin="0" marginwidth="0" marginheight="0"><style fprolloverstyle>A:hover {color: #FF0000}</style></head><font face="verdana,arial" size="2">
<table border="0" width="100%" bgcolor="#000000" align="center"><tr><td align="center" bgcolor="#ffff00"><font face="verdana,arial" size="2" color="#000000">
<b>B&R Exterminators</b></font></td></tr></table>
<br><br><br><br><center>Thank you for your interest. Please <a href="http://bmgrafx.net/brex/home.php">click here</a> to continue.
<br><br><BR><BR><BR><BR><BR></center><table border="0" width="100%" bgcolor="#000000" align="center"><tr><td align="center" bgcolor="#ffff00"><font face="verdana,arial" size="2" color="#000000"><b>Your email has been sent. </b></font></td></tr></table></body></html>~;




Now on to the security issues and redirecting the email sent page!