'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";
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();
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);
print "\n";
}
+ # Do the merge.
my $out;
if (! $tt->process(\$tmplData->{'body'}, $cols, \$out)) {
warn "Failed to process template: $tt->error()";
}
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";
$mail->send('sendmail');
} else {
+ # Say who we want to send to.
print "Want to send to $cols->{'email'}" .
($tmplData->{'bcc'} ? " bcc $tmplData->{'bcc'} " : '') . "\n";
}