1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
| #!/usr/bin/perl
$mailprog = "/usr/lib/sendmail" ;
$mailto = 'webmaster@mondomaine.com' ;
$subject = "E-mail" ;
$formurl = "http://www.mondomaine.com/mailform.htm" ;
$errorurl = "http://www.mondomaine.com/error.html" ;
$thankyouurl = "http://www.mondomaine.com/merci.htm" ;
sub redirect_url {
my ( $url ) = shift ;
print "Location: $url\n\n" ;
}
sub parse_form_data {
my ($request_method, $input_string, $content_length) ;
my (@vars, $indiv_var, $name, $value);
my (%form) ;
$request_method = $ENV{'REQUEST_METHOD'} ;
$content_length = $ENV{'CONTENT_LENGTH'} ;
# load the entire string into $input_string
if ($request_method =~ /post/i) {
$input_string = "" ;
read( STDIN, $input_string, $content_length ) ;
}
else {
$input_string = $ENV{ 'QUERY_STRING' };
}
unless (defined $input_string) {
$input_string = "" ;
}
# put all the variable pairs (name=value) into an array
@vars = split( /&/, $input_string );
# process each individual name, value pair, putting them
# into a hash for easy access
foreach $indiv_var ( @vars ) {
# separate the pair
($name, $value) = split( /=/, $indiv_var, 2 );
# translate encoding
$name =~ s/%([\da-fA-F]{2})/pack("C", hex($1))/eg;
$name =~ tr/+/ /;
unless (defined $value) {
# just in case there was no equals as in ISINDEX
$value = "" ;
}
$value =~ s/%([\da-fA-F]{2})/pack("C", hex($1))/eg;
$value =~ tr/+/ /;
# put the pair in the hash for easy access
$form{$name} = $value ;
}
return wantarray ? %form : undef ;
}
sub send_email {
my ($mailprog, $email, $name, $mailto, $subject, $message) = @_ ;
$name =~ s/[\r\n].*//s;
$email =~ s/[\r\n].*//s;
if (open MAIL, "|$mailprog -t") {
print MAIL "To: $mailto\n" ;
print MAIL "From: \"$name\" <$email>\n" ;
print MAIL "Reply-To: \"$name\" <$email>\n" ;
print MAIL "X-Mailer: chfeedback.pl 2.2.1\n" ;
print MAIL "Subject: $subject\n\n" ;
print MAIL $message ;
close MAIL ;
}
# ignore fails, just don't do anything
return ;
}
# ----------- main program ------
my %form = parse_form_data();
my $email ;
if (exists $form{"email"}) {
$email = $form{"email"} ;
}
else {
redirect_url( $formurl );
exit ;
}
if ($email eq "") {
redirect_url( $errorurl );
exit ;
}
my $name = $form{"name"} ;
if ($name eq "") {
redirect_url( $errorurl );
exit ;
}
my $tirtre = $form{'tirtre'} ;
if ($tirtre eq "") {
redirect_url( $errorurl );
exit ;
}
my $ville = $form{'ville'} ;
if ($ville eq "") {
redirect_url( $errorurl );
exit ;
}
my $precisions = $form{'precisions'} ;
if ($precisions eq "") {redirect_url( $errorurl );
exit ;
}
my $http_referer = $ENV{ 'HTTP_REFERER' };
my $message =
"Ceci est un e-mail qui vous a été envoyé à partir de:\n" .
"$http_referer\n" .
"------------------------- CONTENU DU E-MAIL ---------------\n\n" .
"Tirtre:" . $tirtre .
"\nNom:" . $name .
"\nVille/Pays:" . $ville .
"\nE-mail:" . $email .
"\nContenu du message:" . $precisions .
"\n\n--------------------------------------------------------------------------------\n" ;
send_email ( $mailprog, $email, $name, $mailto, $subject, $message );
redirect_url( $thankyouurl ); |
Partager