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 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
| #! /usr/bin/perl -w -T
#
# $Id: email.pl 1656 2008-02-14 12:51:56Z coelho $
#
# perl cgi script to send an email from a template, like "cgiemail".
# the configuration is managed by environment variables.
#
# env EMAIL_TMPL: mandatory template directory for email templates
# param template: mandatory template file name in directory
#
# param [-a-zA-Z0-9]+: substituted in template body, empty if not set
# param req(uired)?-[a-zA-Z0-9]+: mandatory parameter
#
# env EMAIL_HOST: mailhost, default to 'localhost'
# env EMAIL_FROM: mail from field, default to 'postmaster@EMAIL_HOST'
# env EMAIL_RQMS: message on missing required parameters
# env EMAIL_OKAY: success redirect (or Okay header)
# env EMAIL_FAIL: failure redirect (supersedes EMAIL_RQMS, or Fail header)
#
# TODO: check parameter sizes?
use strict;
use CGI;
my $q = new CGI;
my $okay = undef;
my $fail = undef;
my $error_param = undef;
eval # TRY
{
# get or set mail server
my $mailhost = "ns351403.ovh.net";
$mailhost = $ENV{EMAIL_HOST} if exists $ENV{EMAIL_HOST};
if ($mailhost =~ /^([-a-zA-Z0-9.]+)$/)
{
$mailhost = $1; # untaint
}
else
{
die "invalid mail host: $mailhost";
}
# get or set from
my $from = "postmaster\@$mailhost";
$from = $ENV{EMAIL_FROM} if exists $ENV{EMAIL_FROM};
if ($from =~ /^([-a-zA-Z0-9.]+\@[-a-zA-Z0-9.]+)$/)
{
$from = $1; # untaint
}
else
{
die "invalid from: $from";
}
# get template directory
#die "no environment var EMAIL_TMPL" unless exists $ENV{EMAIL_TMPL};
#my $tmpl_dir = $ENV{EMAIL_TMPL};
my $tmpl_dir = "/home/iifiir/cgi-bin/";
die "no such directory ($tmpl_dir)" unless -d $tmpl_dir;
# get template
die "no mandatory template parameter" unless $q->param('template');
my $tmpl = $q->param('template');
if ($tmpl =~ /^([-a-zA-Z0-9_.]+)$/)
{
$tmpl = $1; # untaint
}
else
{
die "invalid template parameter: $tmpl";
}
my $tmpl_file = $tmpl_dir . '/' . $tmpl;
die "no such template file ($tmpl_file)" unless -r $tmpl_file;
open TMPL, "<$tmpl_file" or die "cannot open template file $tmpl_file";
# first paragraph contains all mail headers
# must also deal with DOS conventions...
my $headers = '';
my $line;
while ($line = <TMPL>)
{
$headers .= $line;
last if $line =~ /^\r?\n$/s;
}
# everything else is the message body.
$/ = undef;
my $body = <TMPL>;
close TMPL or die "cannot close template file $tmpl_file";
# get one To/Cc/Bcc, but does not allow spaces in email addresses.
my @to = split /[\s,]+/, $1 if $headers =~ /^To:\s*(.*)/im;
push @to, split(/[\s,]+/, $1) if $headers =~ /^Cc:\s*(.*)/im;
push @to, split(/[\s,]+/, $1) if $headers =~ /^Bcc:\s*(.*)/im;
die "no email destination in template" unless @to;
# redirection put within template. superseedes env
$okay = $1 if $headers =~ /^Okay:\s*(\S+)/i;
$fail = $1 if $headers =~ /^Fail:\s*(\S+)/i;
# clean some headers
$headers =~ s/^(okay|fail|bcc):[^\n]*\n//gsi;
# handle subsitutions
my (@subs,%seen) = ();
while ($body =~ /\[([-A-Za-z0-9]+)\]/g)
{
push @subs, $1 unless $seen{$1}++;
}
undef %seen;
my %subst = ();
for my $param (@subs)
{
my $value = defined $q->param($param)? $q->param($param): '';
$value = $1 if $value =~ /(.*)/s; # untaint!
if ($param =~ /^req(uired)?-/ and $value eq '')
{
my $msg = 'mandatory parameter not set:';
$msg = $ENV{EMAIL_RQMS} if exists $ENV{EMAIL_RQMS};
$msg = $1 if $msg =~ /(.*)/; # untaint, just in case
$error_param = $param;
die "$msg '$param'";
}
# to be substituted later
$subst{$param} = $value;
}
# actual substitution performed *once* here, only in body
$body =~ s/\[([-A-Za-z0-9]+)\]/$subst{$1}/eg;
undef %subst;
# low level smtp
use Net::SMTP;
my $smtp = new Net::SMTP($mailhost);
$smtp->mail($from);
$smtp->to(@to);
$smtp->data();
$smtp->datasend($headers);
$smtp->datasend($body);
$smtp->dataend();
$smtp->quit();
# success?
if (defined $okay)
{
print $q->redirect($okay);
}
elsif (exists $ENV{EMAIL_OKAY})
{
print $q->redirect($ENV{EMAIL_OKAY});
}
else
{
print "Content-Type: text/plain\r\n\r\nmail sent.\n";
}
};
if ($@) # CATCH exceptions
{
warn "$@";
my $stuff = defined $error_param? "?param=$error_param": '';
if (defined $fail)
{
print $q->redirect($fail . $stuff);
}
elsif (exists $ENV{EMAIL_FAIL})
{
print $q->redirect($ENV{EMAIL_FAIL} . $stuff);
}
else
{
print "Content-Type: text/plain\r\n\r\nerror: $@\n";
}
} |
Partager