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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Send email using cgi 1

Status
Not open for further replies.

shruti18

MIS
Joined
Jun 22, 2005
Messages
30
Location
US
Hello
I am trying to write a form which sends email on the basis of selection made in the drop down item of one the form fields.
So, there is an email address associated with every choice in the drop down. When the submit button of the form is clicked, the data of the form should be emailed to that corresponding email got from the selection made in the dropdown.
Could anyone suggest me how to do this?
Thanks!
 
google for nms-formmail, and have a go at making the changes yourself, if you hit problems call back

HTH
--Paul

cigless ...
 
Hi shruti18

POST to this:-

Code:
[b]#!/usr/bin/perl[/b]

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

read (STDIN, $buffer, $ENV{'CONTENT_LENGTH'});

open (MAIL, '| /usr/sbin/sendmail -t');

select MAIL;

print <<HERE;
To: recipient\@isp.co.uk
From: sender\@isp.co.uk
Subject: Form Parser Example
Content-Type: text/html
HERE

# body...

@pairs = split (/&/, $buffer);

print "<table border=1 cellpadding=3 cellspacing=0>\n";

foreach $pair (@pairs) {

  ($key, $value) = split (/=/, $pair);
  
     $key =~ tr/+/ /;
   $value =~ tr/+/ /;
     $key =~ s/%(..)/pack("c", hex($1))/eg;
   $value =~ s/%(..)/pack("c", hex($1))/eg;
   
  print "  <tr>\n";
  print "    <td>$key</td>\n";
  print "    <td>$value</td>\n";
  print "  </tr>\n";

}

print "</table>";

close MAIL;

select STDOUT;

print "Mail has been sent";


Kind Regards
Duncan
 
Hi Duncan,
In this script, we are sending the form fields to only one email address.
But, I am interested in submitting the form to the email address got from the selection made in the drop-down. (Every drop-down item has a hidden email address to which it should submit the data to).
 
How competent are you within Perl?


Kind Regards
Duncan
 
O.K.

What you need to do is to create a hash (as opposed to an array) from the posted values. If you are careful - which is not hard - you will ALWAYS manage to post a field named recipient. You will then end up with one of the posted values named $formData{"recipient"}. Do this BEFORE the mail section and you can simply replace my recipient with $formData{"recipient"}


Kind Regards
Duncan
 
this will do the job:-

Code:
[b]#!/usr/bin/perl[/b]

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

read (STDIN, $buffer, $ENV{'CONTENT_LENGTH'});

@pairs = split (/&/, $buffer);

foreach $pair (@pairs) {

  ($key, $value) = split (/=/, $pair);
  
     $key =~ tr/+/ /;
   $value =~ tr/+/ /;
     $key =~ s/%(..)/pack("c", hex($1))/eg;
   $value =~ s/%(..)/pack("c", hex($1))/eg;
   
   [b][blue]$formData{"$key"} = $value;[/blue][/b]
   
}

open (MAIL, '| /usr/sbin/sendmail -t');

select MAIL;

print <<HERE;
To: [b][red]$formData{recipient}[/red][/b]
From: sender\@isp.co.uk
Subject: Form Parser Example
Content-Type: text/html
HERE

# body...

print "<table border=1 cellpadding=3 cellspacing=0>\n";

@pairs = split (/&/, $buffer);

foreach $pair (@pairs) {

  ($key, $value) = split (/=/, $pair);
  
     $key =~ tr/+/ /;
   $value =~ tr/+/ /;
     $key =~ s/%(..)/pack("c", hex($1))/eg;
   $value =~ s/%(..)/pack("c", hex($1))/eg;
   
  print "  <tr>\n";
  print "    <td>$key</td>\n";
  print "    <td>$value</td>\n";
  print "  </tr>\n";

}

print "</table>";

close MAIL;

select STDOUT;

print "Mail has been sent";

just make sure the field in the HTML is named recipient


Kind Regards
Duncan
 
This is the Form.pl file:

#!/usr/bin/perl

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

read (STDIN, $buffer, $ENV{'CONTENT_LENGTH'});

@pairs = split (/&/, $buffer);

foreach $pair (@pairs) {

($key, $value) = split (/=/, $pair);

$key =~ tr/+/ /;
$value =~ tr/+/ /;
$key =~ s/%(..)/pack("c", hex($1))/eg;
$value =~ s/%(..)/pack("c", hex($1))/eg;

$formData{"$key"} = $value;

}

open (MAIL, '| /usr/lib/sendmail -t');

select MAIL;

print <<HERE;
To: $formData{recipient}
From: $formData{email}
Subject: Contact Form
Content-Type: text/html
HERE

# body...

print "<table border=1 cellpadding=3 cellspacing=0>\n";

@pairs = split (/&/, $buffer);

foreach $pair (@pairs) {

($key, $value) = split (/=/, $pair);

$key =~ tr/+/ /;
$value =~ tr/+/ /;
$key =~ s/%(..)/pack("c", hex($1))/eg;
$value =~ s/%(..)/pack("c", hex($1))/eg;

print " <tr>\n";
print " <td>$key</td>\n";
print " <td>$value</td>\n";
print " </tr>\n";

}

print "</table>";

close MAIL;

select STDOUT;

print "Mail has been sent. Use the back button to go back";
 
How are you getting on?


Kind Regards
Duncan
 
I am getting the same error saying:
Page not found.
Apparently the server here does not support .pl file. So I changed the extension to .cgi
But it gives the same error.
This is the form tag that I have:
<FORM NAME="mailer" METHOD="post" ACTION="
 
if you click on the link you just posted - it does not exist

you must make this link correct for this to work


Kind Regards
Duncan
 
:)))
Thank you Duncan!
I really appreciate your help!
 
No problem! Glad to help. Thanks for your vote. :-)


Kind Regards
Duncan
 
Word of Warning here folks

If you're setting the recipient in the form, then you've just created a potential spammers gateway

--Paul


cigless ...
 
I totally agree with Paul - which is why my first example had the recipient hard-coded. As he states - be careful!


Kind Regards
Duncan
 
What would be the best way to resolve this issue then?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top