From: Andrew Ruthven Date: Fri, 7 Mar 2014 01:11:42 +0000 (+1300) Subject: Start adding GPG encryption and signing support. X-Git-Url: http://git.etc.gen.nz/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9284dae5f42397937f22f1eddd22743af7c1d6de;p=mail-merge.git Start adding GPG encryption and signing support. Signing doesn't work yet. --- diff --git a/merge.pl b/merge.pl index 9732c69..13b8839 100755 --- a/merge.pl +++ b/merge.pl @@ -12,6 +12,7 @@ use Getopt::Long; use Text::CSV_XS; use MIME::Entity; use MIME::Types; +use Mail::GnuPG; my $dir = undef; my $vars = ""; @@ -27,6 +28,9 @@ GetOptions( 's|send' => \$send, ); +# Allow a lone directory on the command line. +$dir ||= shift; + # If we're given a directory then look for the merge files in there. if (defined $dir) { $vars = "$dir/merge.csv" @@ -59,6 +63,12 @@ for ('subject', 'body', 'from') { } } +# If we've been asked to use encryption, set things up +my $mg; +if ($tmplData->{'encrypt'}) { + $mg = new Mail::GnuPG; +} + my $IN = new FileHandle; open($IN, $vars) || die "Failed to open $vars for reading: $!\n"; @@ -74,6 +84,8 @@ if ($tmplData->{'attachment_col'}) { unless grep {/$tmplData->{'attachment_col'}/} @$headers; } +my @gpg_failed_addresses; + while (my $cols = $csv->getline_hr($IN) ) { last unless %$cols; @@ -163,7 +175,27 @@ while (my $cols = $csv->getline_hr($IN) ) { ); } - $mail->send('sendmail'); + for my $extra (qw/cc bcc/) { + push @emails, $tmplData->{$extra} + if defined $tmplData->{$extra}; + } + + my $ret = $mg->mime_encrypt($mail, @emails) + if defined $mg; + + if (! defined $mg || $ret == 0) { + $ret = $mg->mime_sign($mail, $tmplData->{'from'}) + if defined $mg; + + if (! defined $mg || $ret == 0) { + $mail->send('sendmail'); + } else { + die "Sorry, gpg sign error: " . join(", ", @{ $mg->{last_message} }) . "\n"; + } + } else { + warn "Sorry, gpg encrypt error: " . join(", ", @{ $mg->{last_message} }) . "\n"; + push @gpg_failed_addresses, @emails; + } } else { # Say who we want to send to. print "Want to send to: $email_address" .