#!/usr/bin/perl -T # 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; while ( $template =~ /{(.+?)}/g ) { my $m = $1; if ( my $param = param( $m ) || '' ) { $template =~ s/$m/$param/ } } 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 { # my ( $who ) = @_; # open( my $fd, 'ipacl.lst' ) or die( "open() ( $! )" ); # my $raw = <$fd>; # close( $fd ); # my @list = split( /|/, $raw ); # foreach my $i ( @list ) { # my ( $ip, $time ) = split( /:/, $i ); # if ( ( $who eq $ip ) && ( time - $time < 120 ) ) { # return 0; # } # } # open( $fd, '>>ipacl.lst' ) or die( "open() ( $! )" ); # print $fd "$who:" . time . '|'; # close( $fd ); 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; }