use Text::Wrap;
use FileHandle;
use Getopt::Long;
+use Scriptalicious;
use Text::CSV_XS;
+use List::MoreUtils;
use MIME::Entity;
use MIME::Types;
use Mail::GnuPG;
# If we've been asked to use encryption, set things up
my $mg;
-if ($tmplData->{'encrypt'}) {
- $mg = new Mail::GnuPG;
+my %mg_opts;
+if ($tmplData->{'sign'} && $send) {
+ $mg_opts{passphrase} = prompt_passwd('GPG Pasphrase: ');
+ $mg_opts{key} //= $tmplData->{sign_key};
+}
+if ($tmplData->{'encrypt'} || $tmplData->{'sign'}) {
+ $mg = new Mail::GnuPG(%mg_opts);
}
my $IN = new FileHandle;
my $file_to_attach;
if ($tmplData->{'attachment_col'} || $tmplData->{'attachment'}) {
- my $source = defined $tmplData->{'attachment_col'}
+ my $source = defined $tmplData->{'attachment_col'} && $cols->{$tmplData->{'attachment_col'}}
? $cols->{$tmplData->{'attachment_col'}}
: $tmplData->{'attachment'};
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;
+ # Possibly GPG Encrypt the message.
+ if ($tmplData->{'encrypt'}) {
+ my $ret = $mg->mime_encrypt($mail, @emails);
- if (! defined $mg || $ret == 0) {
- $mail->send('sendmail');
- } else {
+ if ($ret != 0) {
+ warn "Sorry, gpg encrypt error: " . join(", ", @{ $mg->{last_message} }) . "\n";
+ push @gpg_failed_addresses, @emails;
+ next;
+ }
+ }
+
+ # Possibly GPG Sign the message.
+ if ($tmplData->{'sign'}) {
+ my $ret = $mg->mime_sign($mail, $tmplData->{'from'});
+
+ if ($ret != 0) {
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;
}
+
+ $mail->send('sendmail');
+
} else {
# Say who we want to send to.
print "Want to send to: $email_address" .
}
close $IN;
+
+if (scalar(@gpg_failed_addresses) > 0) {
+ print "Failed to send GPG encrypt messages to the following addresses:\n";
+ print " ", join("\n ", uniq(@gpg_failed_addresses)) . "\n\n";
+}
+