#!/usr/bin/perl -wT eval 'exec perl -wT -S $0 ${1+"$@"}' if 0; # not running under some shell require 5; use strict; use CGI; use URI::URL; use Mail::Mailer; use vars qw( @errors ); my $subject = "Automated Message From $0"; my $mailtext; my $forwardurl = '/thankyou.html'; my $query = new CGI; my @params = $query->param; my $email = $query->param('email'); my $mailto = $query->param('mailto'); # check email address is valid checkemail($email) if $email; # construct the message for whoever's being sent the form my $param; my $value; my @values; foreach $param(@params) { @values = $query->param($param); if ($query->param('other_'.$param)) { foreach $value (@values) { if ($value eq 'other') { push (@values, $query->param('other_'.$param)); last; } } } @values = grep { $_ ne 'other' } @values; ($value = join(', ', @values)) && ($mailtext .= "\u\L$param\E: $value\n"); } if (@errors) { $mailtext .= 'IMPORTANT! The following error'. (@errors>1 ? 's were' : ' was') ." encountered during execution:\n". join("\n",@errors). "\n\n"; } sendmail($email, $mailto, $subject, $mailtext); # send off the mail print "Location: $forwardurl\n\n"; # forward the user to a 'thank you' page exit; sub sendmail { my ($from, $to, $subject, $body) = @_; my $mailer; eval { $mailer = Mail::Mailer->new('sendmail'); }; if ($@) { push (@errors, "Couldn't send mail: $@"); return 0; } else { $mailer->open({ From => $from, To => $to, Subject => $subject }); print $mailer $body; if (close ($mailer)) { return 1; } else { push (@errors, "Couldn't close mailer: $!"); return 0; } } } sub checkemail{ my $addr = shift; my $validmail; return 0 unless eval { unless ($validmail = Email::Valid->address( -address => $addr, -mxcheck => 1 )) { push (@errors, "address failed $Email::Valid::Details check."); if ($Email::Valid::Details eq 'rfc822') { push (@errors, "$addr is unlikely to be a valid email address"); } elsif ($Email::Valid::Details eq 'mx') { (my $domain = $addr) =~ s/^[^\@]+\@//; push (@errors, "$domain does not seem to have a mailserver"); } return 0; } }; if ($@) { push (@errors, "An error was encountered: $@"); return 0; } return 1; }