All pastes #227672 Raw Edit

Mine

public text v1 · immutable
#227672 ·published 2006-10-29 13:23 UTC
rendered paste body
#!/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:
# <input type="text" name="name" /> 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 "<h1>Please wait some time before sending more mails</h1>\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;';

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
        <head>
                <title>Thank you</title>
        </head>
        <body>
        <h1>Your message has been sent to David</h1>

EOM;

}

sub myfooter {
        return <<'EOM;';


        </body>
</html>

EOM;

}