- if (ask("Mail signature for $uid->{'text'} to '$address'?", $CONFIG{'mail'} ne 'ask-no', $CONFIG{'mail'} eq 'yes')) {
- my $mail = send_mail($address, $can_encrypt, $longkeyid, $uid, @attached);
- if (defined $mail) {
- my $keydir = "$KEYSBASE/$DATE_STRING";
- my $mailfile = "$keydir/$longkeyid.mail.".$uid->{'serial'}.".".sanitize_uid($uid->{'text'});
- open (KEY, ">$mailfile") or die ("Cannot open $mailfile: $!\n");
- print KEY $mail;
- close KEY;
- } else {
- warn "Generating mail failed.\n";
- };
+ my $mail = create_mail($address, $can_encrypt, $longkeyid, $uid, @attached);
+ if (defined $mail) {
+ my $should_send_mail = ask("Mail signature for $uid->{'text'} to '$address'?", $CONFIG{'mail'} ne 'ask-no', $CONFIG{'mail'} eq 'yes');
+ send_message($mail) if $should_send_mail;
+
+ my $keydir = "$KEYSBASE/$DATE_STRING";
+ my $mailfile = "$keydir/$longkeyid.mail.".($should_send_mail ? '' : 'unsent.').$uid->{'serial'}.".".sanitize_uid($uid->{'text'});
+ open (MAILFILE, ">$mailfile") or die ("Cannot open $mailfile: $!\n");
+ print MAILFILE $mail->stringify();
+ close MAILFILE;
+ } else {
+ warn "Generating mail failed.\n";