#!/usr/bin/perl # generic contact mail use warnings; use strict; use Mail::Sendmail; use CGI::Carp qw( fatalsToBrowser ); use CGI qw( header param ); sub DEBUG( @ ); sub addError( $ ); # parameters: # fromname = sender's name # fromemail = sender's email addr # msgsubject = subject for the mail # use as: # for example. # put the file contact.conf somewhere outside the www, so people won't be able to see your email # reading the conf file my ( $to, $server, $redirect ); open( my $fd, 'contact.conf' ) or die( "open() ( $! )" ); while ( my $l = <$fd> ) { unless ( defined( $l ) && $l ) { next } if ( $l =~ /^#/ ) { next } if ( $l =~ /^to\s*=\s*(.+)/ ) { $to = $1; DEBUG "Found to: $to\n"; next } if ( $l =~ /^server\s*=\s*(.+)/ ) { $server = $1; DEBUG "Found server: $server\n"; next } if ( $l =~ /^redirect\s*=\s*(.+)/ ) { $redirect = $1; DEBUG "Found redirect: $redirect\n"; next } } close( $fd ); open( $fd, 'template.conf' ) or die( "open() ( $! )" ); my $template = ''; while ( my $l = <$fd> ) { if ( $l =~ /^#/ ) { next } $template .= $l; } close( $fd ); print header; if ( param() && $template ) { print myheader(); if ( canSendMail( $ENV{ 'REMOTE_ADDR' } ) ) { mail( $to, param( 'fromemail' ), 'Submitted contact form from your website PBD1.CO.UK', $template, $server ); } else { print "

Please wait some time before sending more mails

\n"; } print myfooter(); sub canSendMail { return 1 } sub mail { my ( $to, $from, $subject, $body, $server ) = @_; my %mail = ( To => $to, From => $from, Subject => $subject, Message => $body ); $Mail::Sendmail::mailcfg{ smtp } = [ $server ]; sendmail( %mail ) or DEBUG $Mail::Sendmail::error; DEBUG $Mail::Sendmail::log; } sub DEBUG( @ ) { my @what = @_; # print @what; } sub myheader { return <<'EOM;'; Thank you

Your message has been sent to David

EOM; } sub myfooter { return <<'EOM;'; EOM; }