From: Andrew Ruthven Date: Tue, 25 Aug 2009 08:28:54 +0000 (+1200) Subject: Add some comments. X-Git-Url: http://git.etc.gen.nz/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7211b0203cd06026cbb6c553352ed6b450e4138b;p=mail-merge.git Add some comments. --- diff --git a/merge.pl b/merge.pl index 6fffca2..7d07ac6 100755 --- a/merge.pl +++ b/merge.pl @@ -26,6 +26,7 @@ GetOptions( 's|send' => \$send, ); +# If we're given a directory then look for the merge files in there. if (defined $dir) { $vars = "$dir/merge.csv" if $vars eq '' && -f "$dir/merge.csv"; @@ -33,11 +34,13 @@ if (defined $dir) { if $template eq '' && -f "$dir/merge.yml"; } +# Sanity check. die "Sorry, where is the variables file?" unless -f $vars; die "Sorry, where is the template file?" unless -f $template; +# A little big of prep. my $csv = Text::CSV_XS->new(); my $tt = Template->new(); @@ -48,17 +51,18 @@ if ($@) { die "Failed to load query file $template: $@"; } +# Sanity check that the required fields are present. for ('subject', 'body', 'from') { if (! defined $tmplData->{$_}) { die "$template missing required field $_\n"; } } - my $IN = new FileHandle; open($IN, $vars) || die "Failed to open $vars for reading: $!\n"; +# Work out the headers for the CSV file. my $headers = $csv->getline($IN); die "Nothing in $vars!" unless @$headers; $csv->column_names(@$headers); @@ -74,6 +78,7 @@ while (my $cols = $csv->getline_hr($IN) ) { print "\n"; } + # Do the merge. my $out; if (! $tt->process(\$tmplData->{'body'}, $cols, \$out)) { warn "Failed to process template: $tt->error()"; @@ -91,6 +96,7 @@ while (my $cols = $csv->getline_hr($IN) ) { } if ($send) { + # If we're sending email, then send it, optionally with a BCC. print "Sending to $cols->{'email'}" . ($tmplData->{'bcc'} ? " bcc $tmplData->{'bcc'} " : '') . "\n"; @@ -107,6 +113,7 @@ while (my $cols = $csv->getline_hr($IN) ) { $mail->send('sendmail'); } else { + # Say who we want to send to. print "Want to send to $cols->{'email'}" . ($tmplData->{'bcc'} ? " bcc $tmplData->{'bcc'} " : '') . "\n"; }