send-email: be explicit with SSL certificate verification
[alt-git.git] / git-send-email.perl
blob60eaed32eab2ccb59bd00ac97adbf9c3e39244d6
1 #!/usr/bin/perl
3 # Copyright 2002,2005 Greg Kroah-Hartman <greg@kroah.com>
4 # Copyright 2005 Ryan Anderson <ryan@michonline.com>
6 # GPL v2 (See COPYING)
8 # Ported to support git "mbox" format files by Ryan Anderson <ryan@michonline.com>
10 # Sends a collection of emails to the given email addresses, disturbingly fast.
12 # Supports two formats:
13 # 1. mbox format files (ignoring most headers and MIME formatting - this is designed for sending patches)
14 # 2. The original format support by Greg's script:
15 # first line of the message is who to CC,
16 # and second line is the subject of the message.
19 use 5.008;
20 use strict;
21 use warnings;
22 use Term::ReadLine;
23 use Getopt::Long;
24 use Text::ParseWords;
25 use Data::Dumper;
26 use Term::ANSIColor;
27 use File::Temp qw/ tempdir tempfile /;
28 use File::Spec::Functions qw(catfile);
29 use Error qw(:try);
30 use Git;
32 Getopt::Long::Configure qw/ pass_through /;
34 package FakeTerm;
35 sub new {
36 my ($class, $reason) = @_;
37 return bless \$reason, shift;
39 sub readline {
40 my $self = shift;
41 die "Cannot use readline on FakeTerm: $$self";
43 package main;
46 sub usage {
47 print <<EOT;
48 git send-email [options] <file | directory | rev-list options >
50 Composing:
51 --from <str> * Email From:
52 --[no-]to <str> * Email To:
53 --[no-]cc <str> * Email Cc:
54 --[no-]bcc <str> * Email Bcc:
55 --subject <str> * Email "Subject:"
56 --in-reply-to <str> * Email "In-Reply-To:"
57 --[no-]annotate * Review each patch that will be sent in an editor.
58 --compose * Open an editor for introduction.
59 --compose-encoding <str> * Encoding to assume for introduction.
60 --8bit-encoding <str> * Encoding to assume 8bit mails if undeclared
62 Sending:
63 --envelope-sender <str> * Email envelope sender.
64 --smtp-server <str:int> * Outgoing SMTP server to use. The port
65 is optional. Default 'localhost'.
66 --smtp-server-option <str> * Outgoing SMTP server option to use.
67 --smtp-server-port <int> * Outgoing SMTP server port.
68 --smtp-user <str> * Username for SMTP-AUTH.
69 --smtp-pass <str> * Password for SMTP-AUTH; not necessary.
70 --smtp-encryption <str> * tls or ssl; anything else disables.
71 --smtp-ssl * Deprecated. Use '--smtp-encryption ssl'.
72 --smtp-ssl-cert-path <str> * Path to ca-certificates (either directory or file).
73 Pass an empty string to disable certificate
74 verification.
75 --smtp-domain <str> * The domain name sent to HELO/EHLO handshake
76 --smtp-debug <0|1> * Disable, enable Net::SMTP debug.
78 Automating:
79 --identity <str> * Use the sendemail.<id> options.
80 --to-cmd <str> * Email To: via `<str> \$patch_path`
81 --cc-cmd <str> * Email Cc: via `<str> \$patch_path`
82 --suppress-cc <str> * author, self, sob, cc, cccmd, body, bodycc, all.
83 --[no-]signed-off-by-cc * Send to Signed-off-by: addresses. Default on.
84 --[no-]suppress-from * Send to self. Default off.
85 --[no-]chain-reply-to * Chain In-Reply-To: fields. Default off.
86 --[no-]thread * Use In-Reply-To: field. Default on.
88 Administering:
89 --confirm <str> * Confirm recipients before sending;
90 auto, cc, compose, always, or never.
91 --quiet * Output one line of info per email.
92 --dry-run * Don't actually send the emails.
93 --[no-]validate * Perform patch sanity checks. Default on.
94 --[no-]format-patch * understand any non optional arguments as
95 `git format-patch` ones.
96 --force * Send even if safety checks would prevent it.
98 EOT
99 exit(1);
102 # most mail servers generate the Date: header, but not all...
103 sub format_2822_time {
104 my ($time) = @_;
105 my @localtm = localtime($time);
106 my @gmttm = gmtime($time);
107 my $localmin = $localtm[1] + $localtm[2] * 60;
108 my $gmtmin = $gmttm[1] + $gmttm[2] * 60;
109 if ($localtm[0] != $gmttm[0]) {
110 die "local zone differs from GMT by a non-minute interval\n";
112 if ((($gmttm[6] + 1) % 7) == $localtm[6]) {
113 $localmin += 1440;
114 } elsif ((($gmttm[6] - 1) % 7) == $localtm[6]) {
115 $localmin -= 1440;
116 } elsif ($gmttm[6] != $localtm[6]) {
117 die "local time offset greater than or equal to 24 hours\n";
119 my $offset = $localmin - $gmtmin;
120 my $offhour = $offset / 60;
121 my $offmin = abs($offset % 60);
122 if (abs($offhour) >= 24) {
123 die ("local time offset greater than or equal to 24 hours\n");
126 return sprintf("%s, %2d %s %d %02d:%02d:%02d %s%02d%02d",
127 qw(Sun Mon Tue Wed Thu Fri Sat)[$localtm[6]],
128 $localtm[3],
129 qw(Jan Feb Mar Apr May Jun
130 Jul Aug Sep Oct Nov Dec)[$localtm[4]],
131 $localtm[5]+1900,
132 $localtm[2],
133 $localtm[1],
134 $localtm[0],
135 ($offset >= 0) ? '+' : '-',
136 abs($offhour),
137 $offmin,
141 my $have_email_valid = eval { require Email::Valid; 1 };
142 my $have_mail_address = eval { require Mail::Address; 1 };
143 my $smtp;
144 my $auth;
146 # Variables we fill in automatically, or via prompting:
147 my (@to,$no_to,@initial_to,@cc,$no_cc,@initial_cc,@bcclist,$no_bcc,@xh,
148 $initial_reply_to,$initial_subject,@files,
149 $author,$sender,$smtp_authpass,$annotate,$compose,$time);
151 my $envelope_sender;
153 # Example reply to:
154 #$initial_reply_to = ''; #<20050203173208.GA23964@foobar.com>';
156 my $repo = eval { Git->repository() };
157 my @repo = $repo ? ($repo) : ();
158 my $term = eval {
159 $ENV{"GIT_SEND_EMAIL_NOTTY"}
160 ? new Term::ReadLine 'git-send-email', \*STDIN, \*STDOUT
161 : new Term::ReadLine 'git-send-email';
163 if ($@) {
164 $term = new FakeTerm "$@: going non-interactive";
167 # Behavior modification variables
168 my ($quiet, $dry_run) = (0, 0);
169 my $format_patch;
170 my $compose_filename;
171 my $force = 0;
173 # Handle interactive edition of files.
174 my $multiedit;
175 my $editor;
177 sub do_edit {
178 if (!defined($editor)) {
179 $editor = Git::command_oneline('var', 'GIT_EDITOR');
181 if (defined($multiedit) && !$multiedit) {
182 map {
183 system('sh', '-c', $editor.' "$@"', $editor, $_);
184 if (($? & 127) || ($? >> 8)) {
185 die("the editor exited uncleanly, aborting everything");
187 } @_;
188 } else {
189 system('sh', '-c', $editor.' "$@"', $editor, @_);
190 if (($? & 127) || ($? >> 8)) {
191 die("the editor exited uncleanly, aborting everything");
196 # Variables with corresponding config settings
197 my ($thread, $chain_reply_to, $suppress_from, $signed_off_by_cc);
198 my ($to_cmd, $cc_cmd);
199 my ($smtp_server, $smtp_server_port, @smtp_server_options);
200 my ($smtp_authuser, $smtp_encryption, $smtp_ssl_cert_path);
201 my ($identity, $aliasfiletype, @alias_files, $smtp_domain);
202 my ($validate, $confirm);
203 my (@suppress_cc);
204 my ($auto_8bit_encoding);
205 my ($compose_encoding);
207 my ($debug_net_smtp) = 0; # Net::SMTP, see send_message()
209 my $not_set_by_user = "true but not set by the user";
211 my %config_bool_settings = (
212 "thread" => [\$thread, 1],
213 "chainreplyto" => [\$chain_reply_to, $not_set_by_user],
214 "suppressfrom" => [\$suppress_from, undef],
215 "signedoffbycc" => [\$signed_off_by_cc, undef],
216 "signedoffcc" => [\$signed_off_by_cc, undef], # Deprecated
217 "validate" => [\$validate, 1],
218 "multiedit" => [\$multiedit, undef],
219 "annotate" => [\$annotate, undef]
222 my %config_settings = (
223 "smtpserver" => \$smtp_server,
224 "smtpserverport" => \$smtp_server_port,
225 "smtpserveroption" => \@smtp_server_options,
226 "smtpuser" => \$smtp_authuser,
227 "smtppass" => \$smtp_authpass,
228 "smtpsslcertpath" => \$smtp_ssl_cert_path,
229 "smtpdomain" => \$smtp_domain,
230 "to" => \@initial_to,
231 "tocmd" => \$to_cmd,
232 "cc" => \@initial_cc,
233 "cccmd" => \$cc_cmd,
234 "aliasfiletype" => \$aliasfiletype,
235 "bcc" => \@bcclist,
236 "suppresscc" => \@suppress_cc,
237 "envelopesender" => \$envelope_sender,
238 "confirm" => \$confirm,
239 "from" => \$sender,
240 "assume8bitencoding" => \$auto_8bit_encoding,
241 "composeencoding" => \$compose_encoding,
244 my %config_path_settings = (
245 "aliasesfile" => \@alias_files,
248 # Help users prepare for 1.7.0
249 sub chain_reply_to {
250 if (defined $chain_reply_to &&
251 $chain_reply_to eq $not_set_by_user) {
252 print STDERR
253 "In git 1.7.0, the default has changed to --no-chain-reply-to\n" .
254 "Set sendemail.chainreplyto configuration variable to true if\n" .
255 "you want to keep --chain-reply-to as your default.\n";
256 $chain_reply_to = 0;
258 return $chain_reply_to;
261 # Handle Uncouth Termination
262 sub signal_handler {
264 # Make text normal
265 print color("reset"), "\n";
267 # SMTP password masked
268 system "stty echo";
270 # tmp files from --compose
271 if (defined $compose_filename) {
272 if (-e $compose_filename) {
273 print "'$compose_filename' contains an intermediate version of the email you were composing.\n";
275 if (-e ($compose_filename . ".final")) {
276 print "'$compose_filename.final' contains the composed email.\n"
280 exit;
283 $SIG{TERM} = \&signal_handler;
284 $SIG{INT} = \&signal_handler;
286 # Begin by accumulating all the variables (defined above), that we will end up
287 # needing, first, from the command line:
289 my $help;
290 my $rc = GetOptions("h" => \$help,
291 "sender|from=s" => \$sender,
292 "in-reply-to=s" => \$initial_reply_to,
293 "subject=s" => \$initial_subject,
294 "to=s" => \@initial_to,
295 "to-cmd=s" => \$to_cmd,
296 "no-to" => \$no_to,
297 "cc=s" => \@initial_cc,
298 "no-cc" => \$no_cc,
299 "bcc=s" => \@bcclist,
300 "no-bcc" => \$no_bcc,
301 "chain-reply-to!" => \$chain_reply_to,
302 "smtp-server=s" => \$smtp_server,
303 "smtp-server-option=s" => \@smtp_server_options,
304 "smtp-server-port=s" => \$smtp_server_port,
305 "smtp-user=s" => \$smtp_authuser,
306 "smtp-pass:s" => \$smtp_authpass,
307 "smtp-ssl" => sub { $smtp_encryption = 'ssl' },
308 "smtp-encryption=s" => \$smtp_encryption,
309 "smtp-ssl-cert-path" => \$smtp_ssl_cert_path,
310 "smtp-debug:i" => \$debug_net_smtp,
311 "smtp-domain:s" => \$smtp_domain,
312 "identity=s" => \$identity,
313 "annotate!" => \$annotate,
314 "compose" => \$compose,
315 "quiet" => \$quiet,
316 "cc-cmd=s" => \$cc_cmd,
317 "suppress-from!" => \$suppress_from,
318 "suppress-cc=s" => \@suppress_cc,
319 "signed-off-cc|signed-off-by-cc!" => \$signed_off_by_cc,
320 "confirm=s" => \$confirm,
321 "dry-run" => \$dry_run,
322 "envelope-sender=s" => \$envelope_sender,
323 "thread!" => \$thread,
324 "validate!" => \$validate,
325 "format-patch!" => \$format_patch,
326 "8bit-encoding=s" => \$auto_8bit_encoding,
327 "compose-encoding=s" => \$compose_encoding,
328 "force" => \$force,
331 usage() if $help;
332 unless ($rc) {
333 usage();
336 die "Cannot run git format-patch from outside a repository\n"
337 if $format_patch and not $repo;
339 # Now, let's fill any that aren't set in with defaults:
341 sub read_config {
342 my ($prefix) = @_;
344 foreach my $setting (keys %config_bool_settings) {
345 my $target = $config_bool_settings{$setting}->[0];
346 $$target = Git::config_bool(@repo, "$prefix.$setting") unless (defined $$target);
349 foreach my $setting (keys %config_path_settings) {
350 my $target = $config_path_settings{$setting};
351 if (ref($target) eq "ARRAY") {
352 unless (@$target) {
353 my @values = Git::config_path(@repo, "$prefix.$setting");
354 @$target = @values if (@values && defined $values[0]);
357 else {
358 $$target = Git::config_path(@repo, "$prefix.$setting") unless (defined $$target);
362 foreach my $setting (keys %config_settings) {
363 my $target = $config_settings{$setting};
364 next if $setting eq "to" and defined $no_to;
365 next if $setting eq "cc" and defined $no_cc;
366 next if $setting eq "bcc" and defined $no_bcc;
367 if (ref($target) eq "ARRAY") {
368 unless (@$target) {
369 my @values = Git::config(@repo, "$prefix.$setting");
370 @$target = @values if (@values && defined $values[0]);
373 else {
374 $$target = Git::config(@repo, "$prefix.$setting") unless (defined $$target);
378 if (!defined $smtp_encryption) {
379 my $enc = Git::config(@repo, "$prefix.smtpencryption");
380 if (defined $enc) {
381 $smtp_encryption = $enc;
382 } elsif (Git::config_bool(@repo, "$prefix.smtpssl")) {
383 $smtp_encryption = 'ssl';
388 # read configuration from [sendemail "$identity"], fall back on [sendemail]
389 $identity = Git::config(@repo, "sendemail.identity") unless (defined $identity);
390 read_config("sendemail.$identity") if (defined $identity);
391 read_config("sendemail");
393 # fall back on builtin bool defaults
394 foreach my $setting (values %config_bool_settings) {
395 ${$setting->[0]} = $setting->[1] unless (defined (${$setting->[0]}));
398 # 'default' encryption is none -- this only prevents a warning
399 $smtp_encryption = '' unless (defined $smtp_encryption);
401 # Set CC suppressions
402 my(%suppress_cc);
403 if (@suppress_cc) {
404 foreach my $entry (@suppress_cc) {
405 die "Unknown --suppress-cc field: '$entry'\n"
406 unless $entry =~ /^(?:all|cccmd|cc|author|self|sob|body|bodycc)$/;
407 $suppress_cc{$entry} = 1;
411 if ($suppress_cc{'all'}) {
412 foreach my $entry (qw (cccmd cc author self sob body bodycc)) {
413 $suppress_cc{$entry} = 1;
415 delete $suppress_cc{'all'};
418 # If explicit old-style ones are specified, they trump --suppress-cc.
419 $suppress_cc{'self'} = $suppress_from if defined $suppress_from;
420 $suppress_cc{'sob'} = !$signed_off_by_cc if defined $signed_off_by_cc;
422 if ($suppress_cc{'body'}) {
423 foreach my $entry (qw (sob bodycc)) {
424 $suppress_cc{$entry} = 1;
426 delete $suppress_cc{'body'};
429 # Set confirm's default value
430 my $confirm_unconfigured = !defined $confirm;
431 if ($confirm_unconfigured) {
432 $confirm = scalar %suppress_cc ? 'compose' : 'auto';
434 die "Unknown --confirm setting: '$confirm'\n"
435 unless $confirm =~ /^(?:auto|cc|compose|always|never)/;
437 # Debugging, print out the suppressions.
438 if (0) {
439 print "suppressions:\n";
440 foreach my $entry (keys %suppress_cc) {
441 printf " %-5s -> $suppress_cc{$entry}\n", $entry;
445 my ($repoauthor, $repocommitter);
446 ($repoauthor) = Git::ident_person(@repo, 'author');
447 ($repocommitter) = Git::ident_person(@repo, 'committer');
449 # Verify the user input
451 foreach my $entry (@initial_to) {
452 die "Comma in --to entry: $entry'\n" unless $entry !~ m/,/;
455 foreach my $entry (@initial_cc) {
456 die "Comma in --cc entry: $entry'\n" unless $entry !~ m/,/;
459 foreach my $entry (@bcclist) {
460 die "Comma in --bcclist entry: $entry'\n" unless $entry !~ m/,/;
463 sub parse_address_line {
464 if ($have_mail_address) {
465 return map { $_->format } Mail::Address->parse($_[0]);
466 } else {
467 return split_addrs($_[0]);
471 sub split_addrs {
472 return quotewords('\s*,\s*', 1, @_);
475 my %aliases;
476 my %parse_alias = (
477 # multiline formats can be supported in the future
478 mutt => sub { my $fh = shift; while (<$fh>) {
479 if (/^\s*alias\s+(?:-group\s+\S+\s+)*(\S+)\s+(.*)$/) {
480 my ($alias, $addr) = ($1, $2);
481 $addr =~ s/#.*$//; # mutt allows # comments
482 # commas delimit multiple addresses
483 $aliases{$alias} = [ split_addrs($addr) ];
484 }}},
485 mailrc => sub { my $fh = shift; while (<$fh>) {
486 if (/^alias\s+(\S+)\s+(.*)$/) {
487 # spaces delimit multiple addresses
488 $aliases{$1} = [ quotewords('\s+', 0, $2) ];
489 }}},
490 pine => sub { my $fh = shift; my $f='\t[^\t]*';
491 for (my $x = ''; defined($x); $x = $_) {
492 chomp $x;
493 $x .= $1 while(defined($_ = <$fh>) && /^ +(.*)$/);
494 $x =~ /^(\S+)$f\t\(?([^\t]+?)\)?(:?$f){0,2}$/ or next;
495 $aliases{$1} = [ split_addrs($2) ];
497 elm => sub { my $fh = shift;
498 while (<$fh>) {
499 if (/^(\S+)\s+=\s+[^=]+=\s(\S+)/) {
500 my ($alias, $addr) = ($1, $2);
501 $aliases{$alias} = [ split_addrs($addr) ];
503 } },
505 gnus => sub { my $fh = shift; while (<$fh>) {
506 if (/\(define-mail-alias\s+"(\S+?)"\s+"(\S+?)"\)/) {
507 $aliases{$1} = [ $2 ];
511 if (@alias_files and $aliasfiletype and defined $parse_alias{$aliasfiletype}) {
512 foreach my $file (@alias_files) {
513 open my $fh, '<', $file or die "opening $file: $!\n";
514 $parse_alias{$aliasfiletype}->($fh);
515 close $fh;
519 ($sender) = expand_aliases($sender) if defined $sender;
521 # is_format_patch_arg($f) returns 0 if $f names a patch, or 1 if
522 # $f is a revision list specification to be passed to format-patch.
523 sub is_format_patch_arg {
524 return unless $repo;
525 my $f = shift;
526 try {
527 $repo->command('rev-parse', '--verify', '--quiet', $f);
528 if (defined($format_patch)) {
529 return $format_patch;
531 die(<<EOF);
532 File '$f' exists but it could also be the range of commits
533 to produce patches for. Please disambiguate by...
535 * Saying "./$f" if you mean a file; or
536 * Giving --format-patch option if you mean a range.
538 } catch Git::Error::Command with {
539 # Not a valid revision. Treat it as a filename.
540 return 0;
544 # Now that all the defaults are set, process the rest of the command line
545 # arguments and collect up the files that need to be processed.
546 my @rev_list_opts;
547 while (defined(my $f = shift @ARGV)) {
548 if ($f eq "--") {
549 push @rev_list_opts, "--", @ARGV;
550 @ARGV = ();
551 } elsif (-d $f and !is_format_patch_arg($f)) {
552 opendir my $dh, $f
553 or die "Failed to opendir $f: $!";
555 push @files, grep { -f $_ } map { catfile($f, $_) }
556 sort readdir $dh;
557 closedir $dh;
558 } elsif ((-f $f or -p $f) and !is_format_patch_arg($f)) {
559 push @files, $f;
560 } else {
561 push @rev_list_opts, $f;
565 if (@rev_list_opts) {
566 die "Cannot run git format-patch from outside a repository\n"
567 unless $repo;
568 push @files, $repo->command('format-patch', '-o', tempdir(CLEANUP => 1), @rev_list_opts);
571 if ($validate) {
572 foreach my $f (@files) {
573 unless (-p $f) {
574 my $error = validate_patch($f);
575 $error and die "fatal: $f: $error\nwarning: no patches were sent\n";
580 if (@files) {
581 unless ($quiet) {
582 print $_,"\n" for (@files);
584 } else {
585 print STDERR "\nNo patch files specified!\n\n";
586 usage();
589 sub get_patch_subject {
590 my $fn = shift;
591 open (my $fh, '<', $fn);
592 while (my $line = <$fh>) {
593 next unless ($line =~ /^Subject: (.*)$/);
594 close $fh;
595 return "GIT: $1\n";
597 close $fh;
598 die "No subject line in $fn ?";
601 if ($compose) {
602 # Note that this does not need to be secure, but we will make a small
603 # effort to have it be unique
604 $compose_filename = ($repo ?
605 tempfile(".gitsendemail.msg.XXXXXX", DIR => $repo->repo_path()) :
606 tempfile(".gitsendemail.msg.XXXXXX", DIR => "."))[1];
607 open my $c, ">", $compose_filename
608 or die "Failed to open for writing $compose_filename: $!";
611 my $tpl_sender = $sender || $repoauthor || $repocommitter || '';
612 my $tpl_subject = $initial_subject || '';
613 my $tpl_reply_to = $initial_reply_to || '';
615 print $c <<EOT;
616 From $tpl_sender # This line is ignored.
617 GIT: Lines beginning in "GIT:" will be removed.
618 GIT: Consider including an overall diffstat or table of contents
619 GIT: for the patch you are writing.
620 GIT:
621 GIT: Clear the body content if you don't wish to send a summary.
622 From: $tpl_sender
623 Subject: $tpl_subject
624 In-Reply-To: $tpl_reply_to
627 for my $f (@files) {
628 print $c get_patch_subject($f);
630 close $c;
632 if ($annotate) {
633 do_edit($compose_filename, @files);
634 } else {
635 do_edit($compose_filename);
638 open my $c2, ">", $compose_filename . ".final"
639 or die "Failed to open $compose_filename.final : " . $!;
641 open $c, "<", $compose_filename
642 or die "Failed to open $compose_filename : " . $!;
644 my $need_8bit_cte = file_has_nonascii($compose_filename);
645 my $in_body = 0;
646 my $summary_empty = 1;
647 if (!defined $compose_encoding) {
648 $compose_encoding = "UTF-8";
650 while(<$c>) {
651 next if m/^GIT:/;
652 if ($in_body) {
653 $summary_empty = 0 unless (/^\n$/);
654 } elsif (/^\n$/) {
655 $in_body = 1;
656 if ($need_8bit_cte) {
657 print $c2 "MIME-Version: 1.0\n",
658 "Content-Type: text/plain; ",
659 "charset=$compose_encoding\n",
660 "Content-Transfer-Encoding: 8bit\n";
662 } elsif (/^MIME-Version:/i) {
663 $need_8bit_cte = 0;
664 } elsif (/^Subject:\s*(.+)\s*$/i) {
665 $initial_subject = $1;
666 my $subject = $initial_subject;
667 $_ = "Subject: " .
668 quote_subject($subject, $compose_encoding) .
669 "\n";
670 } elsif (/^In-Reply-To:\s*(.+)\s*$/i) {
671 $initial_reply_to = $1;
672 next;
673 } elsif (/^From:\s*(.+)\s*$/i) {
674 $sender = $1;
675 next;
676 } elsif (/^(?:To|Cc|Bcc):/i) {
677 print "To/Cc/Bcc fields are not interpreted yet, they have been ignored\n";
678 next;
680 print $c2 $_;
682 close $c;
683 close $c2;
685 if ($summary_empty) {
686 print "Summary email is empty, skipping it\n";
687 $compose = -1;
689 } elsif ($annotate) {
690 do_edit(@files);
693 sub ask {
694 my ($prompt, %arg) = @_;
695 my $valid_re = $arg{valid_re};
696 my $default = $arg{default};
697 my $confirm_only = $arg{confirm_only};
698 my $resp;
699 my $i = 0;
700 return defined $default ? $default : undef
701 unless defined $term->IN and defined fileno($term->IN) and
702 defined $term->OUT and defined fileno($term->OUT);
703 while ($i++ < 10) {
704 $resp = $term->readline($prompt);
705 if (!defined $resp) { # EOF
706 print "\n";
707 return defined $default ? $default : undef;
709 if ($resp eq '' and defined $default) {
710 return $default;
712 if (!defined $valid_re or $resp =~ /$valid_re/) {
713 return $resp;
715 if ($confirm_only) {
716 my $yesno = $term->readline("Are you sure you want to use <$resp> [y/N]? ");
717 if (defined $yesno && $yesno =~ /y/i) {
718 return $resp;
722 return;
725 my %broken_encoding;
727 sub file_declares_8bit_cte {
728 my $fn = shift;
729 open (my $fh, '<', $fn);
730 while (my $line = <$fh>) {
731 last if ($line =~ /^$/);
732 return 1 if ($line =~ /^Content-Transfer-Encoding: .*8bit.*$/);
734 close $fh;
735 return 0;
738 foreach my $f (@files) {
739 next unless (body_or_subject_has_nonascii($f)
740 && !file_declares_8bit_cte($f));
741 $broken_encoding{$f} = 1;
744 if (!defined $auto_8bit_encoding && scalar %broken_encoding) {
745 print "The following files are 8bit, but do not declare " .
746 "a Content-Transfer-Encoding.\n";
747 foreach my $f (sort keys %broken_encoding) {
748 print " $f\n";
750 $auto_8bit_encoding = ask("Which 8bit encoding should I declare [UTF-8]? ",
751 default => "UTF-8");
754 if (!$force) {
755 for my $f (@files) {
756 if (get_patch_subject($f) =~ /\Q*** SUBJECT HERE ***\E/) {
757 die "Refusing to send because the patch\n\t$f\n"
758 . "has the template subject '*** SUBJECT HERE ***'. "
759 . "Pass --force if you really want to send.\n";
764 if (!defined $sender) {
765 $sender = $repoauthor || $repocommitter || '';
768 my $prompting = 0;
769 if (!@initial_to && !defined $to_cmd) {
770 my $to = ask("Who should the emails be sent to (if any)? ",
771 default => "",
772 valid_re => qr/\@.*\./, confirm_only => 1);
773 push @initial_to, parse_address_line($to) if defined $to; # sanitized/validated later
774 $prompting++;
777 sub expand_aliases {
778 return map { expand_one_alias($_) } @_;
781 my %EXPANDED_ALIASES;
782 sub expand_one_alias {
783 my $alias = shift;
784 if ($EXPANDED_ALIASES{$alias}) {
785 die "fatal: alias '$alias' expands to itself\n";
787 local $EXPANDED_ALIASES{$alias} = 1;
788 return $aliases{$alias} ? expand_aliases(@{$aliases{$alias}}) : $alias;
791 @initial_to = expand_aliases(@initial_to);
792 @initial_to = validate_address_list(sanitize_address_list(@initial_to));
793 @initial_cc = expand_aliases(@initial_cc);
794 @initial_cc = validate_address_list(sanitize_address_list(@initial_cc));
795 @bcclist = expand_aliases(@bcclist);
796 @bcclist = validate_address_list(sanitize_address_list(@bcclist));
798 if ($thread && !defined $initial_reply_to && $prompting) {
799 $initial_reply_to = ask(
800 "Message-ID to be used as In-Reply-To for the first email (if any)? ",
801 default => "",
802 valid_re => qr/\@.*\./, confirm_only => 1);
804 if (defined $initial_reply_to) {
805 $initial_reply_to =~ s/^\s*<?//;
806 $initial_reply_to =~ s/>?\s*$//;
807 $initial_reply_to = "<$initial_reply_to>" if $initial_reply_to ne '';
810 if (!defined $smtp_server) {
811 foreach (qw( /usr/sbin/sendmail /usr/lib/sendmail )) {
812 if (-x $_) {
813 $smtp_server = $_;
814 last;
817 $smtp_server ||= 'localhost'; # could be 127.0.0.1, too... *shrug*
820 if ($compose && $compose > 0) {
821 @files = ($compose_filename . ".final", @files);
824 # Variables we set as part of the loop over files
825 our ($message_id, %mail, $subject, $reply_to, $references, $message,
826 $needs_confirm, $message_num, $ask_default);
828 sub extract_valid_address {
829 my $address = shift;
830 my $local_part_regexp = qr/[^<>"\s@]+/;
831 my $domain_regexp = qr/[^.<>"\s@]+(?:\.[^.<>"\s@]+)+/;
833 # check for a local address:
834 return $address if ($address =~ /^($local_part_regexp)$/);
836 $address =~ s/^\s*<(.*)>\s*$/$1/;
837 if ($have_email_valid) {
838 return scalar Email::Valid->address($address);
841 # less robust/correct than the monster regexp in Email::Valid,
842 # but still does a 99% job, and one less dependency
843 return $1 if $address =~ /($local_part_regexp\@$domain_regexp)/;
844 return;
847 sub extract_valid_address_or_die {
848 my $address = shift;
849 $address = extract_valid_address($address);
850 die "error: unable to extract a valid address from: $address\n"
851 if !$address;
852 return $address;
855 sub validate_address {
856 my $address = shift;
857 while (!extract_valid_address($address)) {
858 print STDERR "error: unable to extract a valid address from: $address\n";
859 $_ = ask("What to do with this address? ([q]uit|[d]rop|[e]dit): ",
860 valid_re => qr/^(?:quit|q|drop|d|edit|e)/i,
861 default => 'q');
862 if (/^d/i) {
863 return undef;
864 } elsif (/^q/i) {
865 cleanup_compose_files();
866 exit(0);
868 $address = ask("Who should the email be sent to (if any)? ",
869 default => "",
870 valid_re => qr/\@.*\./, confirm_only => 1);
872 return $address;
875 sub validate_address_list {
876 return (grep { defined $_ }
877 map { validate_address($_) } @_);
880 # Usually don't need to change anything below here.
882 # we make a "fake" message id by taking the current number
883 # of seconds since the beginning of Unix time and tacking on
884 # a random number to the end, in case we are called quicker than
885 # 1 second since the last time we were called.
887 # We'll setup a template for the message id, using the "from" address:
889 my ($message_id_stamp, $message_id_serial);
890 sub make_message_id {
891 my $uniq;
892 if (!defined $message_id_stamp) {
893 $message_id_stamp = sprintf("%s-%s", time, $$);
894 $message_id_serial = 0;
896 $message_id_serial++;
897 $uniq = "$message_id_stamp-$message_id_serial";
899 my $du_part;
900 for ($sender, $repocommitter, $repoauthor) {
901 $du_part = extract_valid_address(sanitize_address($_));
902 last if (defined $du_part and $du_part ne '');
904 if (not defined $du_part or $du_part eq '') {
905 require Sys::Hostname;
906 $du_part = 'user@' . Sys::Hostname::hostname();
908 my $message_id_template = "<%s-git-send-email-%s>";
909 $message_id = sprintf($message_id_template, $uniq, $du_part);
910 #print "new message id = $message_id\n"; # Was useful for debugging
915 $time = time - scalar $#files;
917 sub unquote_rfc2047 {
918 local ($_) = @_;
919 my $encoding;
920 s{=\?([^?]+)\?q\?(.*?)\?=}{
921 $encoding = $1;
922 my $e = $2;
923 $e =~ s/_/ /g;
924 $e =~ s/=([0-9A-F]{2})/chr(hex($1))/eg;
926 }eg;
927 return wantarray ? ($_, $encoding) : $_;
930 sub quote_rfc2047 {
931 local $_ = shift;
932 my $encoding = shift || 'UTF-8';
933 s/([^-a-zA-Z0-9!*+\/])/sprintf("=%02X", ord($1))/eg;
934 s/(.*)/=\?$encoding\?q\?$1\?=/;
935 return $_;
938 sub is_rfc2047_quoted {
939 my $s = shift;
940 my $token = qr/[^][()<>@,;:"\/?.= \000-\037\177-\377]+/;
941 my $encoded_text = qr/[!->@-~]+/;
942 length($s) <= 75 &&
943 $s =~ m/^(?:"[[:ascii:]]*"|=\?$token\?$token\?$encoded_text\?=)$/o;
946 sub subject_needs_rfc2047_quoting {
947 my $s = shift;
949 return ($s =~ /[^[:ascii:]]/) || ($s =~ /=\?/);
952 sub quote_subject {
953 local $subject = shift;
954 my $encoding = shift || 'UTF-8';
956 if (subject_needs_rfc2047_quoting($subject)) {
957 return quote_rfc2047($subject, $encoding);
959 return $subject;
962 # use the simplest quoting being able to handle the recipient
963 sub sanitize_address {
964 my ($recipient) = @_;
966 # remove garbage after email address
967 $recipient =~ s/(.*>).*$/$1/;
969 my ($recipient_name, $recipient_addr) = ($recipient =~ /^(.*?)\s*(<.*)/);
971 if (not $recipient_name) {
972 return $recipient;
975 # if recipient_name is already quoted, do nothing
976 if (is_rfc2047_quoted($recipient_name)) {
977 return $recipient;
980 # rfc2047 is needed if a non-ascii char is included
981 if ($recipient_name =~ /[^[:ascii:]]/) {
982 $recipient_name =~ s/^"(.*)"$/$1/;
983 $recipient_name = quote_rfc2047($recipient_name);
986 # double quotes are needed if specials or CTLs are included
987 elsif ($recipient_name =~ /[][()<>@,;:\\".\000-\037\177]/) {
988 $recipient_name =~ s/(["\\\r])/\\$1/g;
989 $recipient_name = qq["$recipient_name"];
992 return "$recipient_name $recipient_addr";
996 sub sanitize_address_list {
997 return (map { sanitize_address($_) } @_);
1000 # Returns the local Fully Qualified Domain Name (FQDN) if available.
1002 # Tightly configured MTAa require that a caller sends a real DNS
1003 # domain name that corresponds the IP address in the HELO/EHLO
1004 # handshake. This is used to verify the connection and prevent
1005 # spammers from trying to hide their identity. If the DNS and IP don't
1006 # match, the receiveing MTA may deny the connection.
1008 # Here is a deny example of Net::SMTP with the default "localhost.localdomain"
1010 # Net::SMTP=GLOB(0x267ec28)>>> EHLO localhost.localdomain
1011 # Net::SMTP=GLOB(0x267ec28)<<< 550 EHLO argument does not match calling host
1013 # This maildomain*() code is based on ideas in Perl library Test::Reporter
1014 # /usr/share/perl5/Test/Reporter/Mail/Util.pm ==> sub _maildomain ()
1016 sub valid_fqdn {
1017 my $domain = shift;
1018 return defined $domain && !($^O eq 'darwin' && $domain =~ /\.local$/) && $domain =~ /\./;
1021 sub maildomain_net {
1022 my $maildomain;
1024 if (eval { require Net::Domain; 1 }) {
1025 my $domain = Net::Domain::domainname();
1026 $maildomain = $domain if valid_fqdn($domain);
1029 return $maildomain;
1032 sub maildomain_mta {
1033 my $maildomain;
1035 if (eval { require Net::SMTP; 1 }) {
1036 for my $host (qw(mailhost localhost)) {
1037 my $smtp = Net::SMTP->new($host);
1038 if (defined $smtp) {
1039 my $domain = $smtp->domain;
1040 $smtp->quit;
1042 $maildomain = $domain if valid_fqdn($domain);
1044 last if $maildomain;
1049 return $maildomain;
1052 sub maildomain {
1053 return maildomain_net() || maildomain_mta() || 'localhost.localdomain';
1056 sub smtp_host_string {
1057 if (defined $smtp_server_port) {
1058 return "$smtp_server:$smtp_server_port";
1059 } else {
1060 return $smtp_server;
1064 # Returns 1 if authentication succeeded or was not necessary
1065 # (smtp_user was not specified), and 0 otherwise.
1067 sub smtp_auth_maybe {
1068 if (!defined $smtp_authuser || $auth) {
1069 return 1;
1072 # Workaround AUTH PLAIN/LOGIN interaction defect
1073 # with Authen::SASL::Cyrus
1074 eval {
1075 require Authen::SASL;
1076 Authen::SASL->import(qw(Perl));
1079 # TODO: Authentication may fail not because credentials were
1080 # invalid but due to other reasons, in which we should not
1081 # reject credentials.
1082 $auth = Git::credential({
1083 'protocol' => 'smtp',
1084 'host' => smtp_host_string(),
1085 'username' => $smtp_authuser,
1086 # if there's no password, "git credential fill" will
1087 # give us one, otherwise it'll just pass this one.
1088 'password' => $smtp_authpass
1089 }, sub {
1090 my $cred = shift;
1091 return !!$smtp->auth($cred->{'username'}, $cred->{'password'});
1094 return $auth;
1097 sub ssl_verify_params {
1098 eval {
1099 require IO::Socket::SSL;
1100 IO::Socket::SSL->import(qw/SSL_VERIFY_PEER SSL_VERIFY_NONE/);
1102 if ($@) {
1103 print STDERR "Not using SSL_VERIFY_PEER due to out-of-date IO::Socket::SSL.\n";
1104 return;
1107 if (!defined $smtp_ssl_cert_path) {
1108 $smtp_ssl_cert_path = "/etc/ssl/certs";
1111 if ($smtp_ssl_cert_path eq "") {
1112 return (SSL_verify_mode => SSL_VERIFY_NONE());
1113 } elsif (-d $smtp_ssl_cert_path) {
1114 return (SSL_verify_mode => SSL_VERIFY_PEER(),
1115 SSL_ca_path => $smtp_ssl_cert_path);
1116 } elsif (-f $smtp_ssl_cert_path) {
1117 return (SSL_verify_mode => SSL_VERIFY_PEER(),
1118 SSL_ca_file => $smtp_ssl_cert_path);
1119 } else {
1120 print STDERR "Not using SSL_VERIFY_PEER because the CA path does not exist.\n";
1121 return (SSL_verify_mode => SSL_VERIFY_NONE());
1125 # Returns 1 if the message was sent, and 0 otherwise.
1126 # In actuality, the whole program dies when there
1127 # is an error sending a message.
1129 sub send_message {
1130 my @recipients = unique_email_list(@to);
1131 @cc = (grep { my $cc = extract_valid_address_or_die($_);
1132 not grep { $cc eq $_ || $_ =~ /<\Q${cc}\E>$/ } @recipients
1134 @cc);
1135 my $to = join (",\n\t", @recipients);
1136 @recipients = unique_email_list(@recipients,@cc,@bcclist);
1137 @recipients = (map { extract_valid_address_or_die($_) } @recipients);
1138 my $date = format_2822_time($time++);
1139 my $gitversion = '@@GIT_VERSION@@';
1140 if ($gitversion =~ m/..GIT_VERSION../) {
1141 $gitversion = Git::version();
1144 my $cc = join(",\n\t", unique_email_list(@cc));
1145 my $ccline = "";
1146 if ($cc ne '') {
1147 $ccline = "\nCc: $cc";
1149 my $sanitized_sender = sanitize_address($sender);
1150 make_message_id() unless defined($message_id);
1152 my $header = "From: $sanitized_sender
1153 To: $to${ccline}
1154 Subject: $subject
1155 Date: $date
1156 Message-Id: $message_id
1157 X-Mailer: git-send-email $gitversion
1159 if ($reply_to) {
1161 $header .= "In-Reply-To: $reply_to\n";
1162 $header .= "References: $references\n";
1164 if (@xh) {
1165 $header .= join("\n", @xh) . "\n";
1168 my @sendmail_parameters = ('-i', @recipients);
1169 my $raw_from = $sanitized_sender;
1170 if (defined $envelope_sender && $envelope_sender ne "auto") {
1171 $raw_from = $envelope_sender;
1173 $raw_from = extract_valid_address($raw_from);
1174 unshift (@sendmail_parameters,
1175 '-f', $raw_from) if(defined $envelope_sender);
1177 if ($needs_confirm && !$dry_run) {
1178 print "\n$header\n";
1179 if ($needs_confirm eq "inform") {
1180 $confirm_unconfigured = 0; # squelch this message for the rest of this run
1181 $ask_default = "y"; # assume yes on EOF since user hasn't explicitly asked for confirmation
1182 print " The Cc list above has been expanded by additional\n";
1183 print " addresses found in the patch commit message. By default\n";
1184 print " send-email prompts before sending whenever this occurs.\n";
1185 print " This behavior is controlled by the sendemail.confirm\n";
1186 print " configuration setting.\n";
1187 print "\n";
1188 print " For additional information, run 'git send-email --help'.\n";
1189 print " To retain the current behavior, but squelch this message,\n";
1190 print " run 'git config --global sendemail.confirm auto'.\n\n";
1192 $_ = ask("Send this email? ([y]es|[n]o|[q]uit|[a]ll): ",
1193 valid_re => qr/^(?:yes|y|no|n|quit|q|all|a)/i,
1194 default => $ask_default);
1195 die "Send this email reply required" unless defined $_;
1196 if (/^n/i) {
1197 return 0;
1198 } elsif (/^q/i) {
1199 cleanup_compose_files();
1200 exit(0);
1201 } elsif (/^a/i) {
1202 $confirm = 'never';
1206 unshift (@sendmail_parameters, @smtp_server_options);
1208 if ($dry_run) {
1209 # We don't want to send the email.
1210 } elsif ($smtp_server =~ m#^/#) {
1211 my $pid = open my $sm, '|-';
1212 defined $pid or die $!;
1213 if (!$pid) {
1214 exec($smtp_server, @sendmail_parameters) or die $!;
1216 print $sm "$header\n$message";
1217 close $sm or die $!;
1218 } else {
1220 if (!defined $smtp_server) {
1221 die "The required SMTP server is not properly defined."
1224 if ($smtp_encryption eq 'ssl') {
1225 $smtp_server_port ||= 465; # ssmtp
1226 require Net::SMTP::SSL;
1227 $smtp_domain ||= maildomain();
1228 $smtp ||= Net::SMTP::SSL->new($smtp_server,
1229 Hello => $smtp_domain,
1230 Port => $smtp_server_port,
1231 ssl_verify_params());
1233 else {
1234 require Net::SMTP;
1235 $smtp_domain ||= maildomain();
1236 $smtp ||= Net::SMTP->new(smtp_host_string(),
1237 Hello => $smtp_domain,
1238 Debug => $debug_net_smtp);
1239 if ($smtp_encryption eq 'tls' && $smtp) {
1240 require Net::SMTP::SSL;
1241 $smtp->command('STARTTLS');
1242 $smtp->response();
1243 if ($smtp->code == 220) {
1244 $smtp = Net::SMTP::SSL->start_SSL($smtp,
1245 ssl_verify_params())
1246 or die "STARTTLS failed! ".$smtp->message;
1247 $smtp_encryption = '';
1248 # Send EHLO again to receive fresh
1249 # supported commands
1250 $smtp->hello($smtp_domain);
1251 } else {
1252 die "Server does not support STARTTLS! ".$smtp->message;
1257 if (!$smtp) {
1258 die "Unable to initialize SMTP properly. Check config and use --smtp-debug. ",
1259 "VALUES: server=$smtp_server ",
1260 "encryption=$smtp_encryption ",
1261 "hello=$smtp_domain",
1262 defined $smtp_server_port ? " port=$smtp_server_port" : "";
1265 smtp_auth_maybe or die $smtp->message;
1267 $smtp->mail( $raw_from ) or die $smtp->message;
1268 $smtp->to( @recipients ) or die $smtp->message;
1269 $smtp->data or die $smtp->message;
1270 $smtp->datasend("$header\n$message") or die $smtp->message;
1271 $smtp->dataend() or die $smtp->message;
1272 $smtp->code =~ /250|200/ or die "Failed to send $subject\n".$smtp->message;
1274 if ($quiet) {
1275 printf (($dry_run ? "Dry-" : "")."Sent %s\n", $subject);
1276 } else {
1277 print (($dry_run ? "Dry-" : "")."OK. Log says:\n");
1278 if ($smtp_server !~ m#^/#) {
1279 print "Server: $smtp_server\n";
1280 print "MAIL FROM:<$raw_from>\n";
1281 foreach my $entry (@recipients) {
1282 print "RCPT TO:<$entry>\n";
1284 } else {
1285 print "Sendmail: $smtp_server ".join(' ',@sendmail_parameters)."\n";
1287 print $header, "\n";
1288 if ($smtp) {
1289 print "Result: ", $smtp->code, ' ',
1290 ($smtp->message =~ /\n([^\n]+\n)$/s), "\n";
1291 } else {
1292 print "Result: OK\n";
1296 return 1;
1299 $reply_to = $initial_reply_to;
1300 $references = $initial_reply_to || '';
1301 $subject = $initial_subject;
1302 $message_num = 0;
1304 foreach my $t (@files) {
1305 open my $fh, "<", $t or die "can't open file $t";
1307 my $author = undef;
1308 my $author_encoding;
1309 my $has_content_type;
1310 my $body_encoding;
1311 @to = ();
1312 @cc = ();
1313 @xh = ();
1314 my $input_format = undef;
1315 my @header = ();
1316 $message = "";
1317 $message_num++;
1318 # First unfold multiline header fields
1319 while(<$fh>) {
1320 last if /^\s*$/;
1321 if (/^\s+\S/ and @header) {
1322 chomp($header[$#header]);
1323 s/^\s+/ /;
1324 $header[$#header] .= $_;
1325 } else {
1326 push(@header, $_);
1329 # Now parse the header
1330 foreach(@header) {
1331 if (/^From /) {
1332 $input_format = 'mbox';
1333 next;
1335 chomp;
1336 if (!defined $input_format && /^[-A-Za-z]+:\s/) {
1337 $input_format = 'mbox';
1340 if (defined $input_format && $input_format eq 'mbox') {
1341 if (/^Subject:\s+(.*)$/i) {
1342 $subject = $1;
1344 elsif (/^From:\s+(.*)$/i) {
1345 ($author, $author_encoding) = unquote_rfc2047($1);
1346 next if $suppress_cc{'author'};
1347 next if $suppress_cc{'self'} and $author eq $sender;
1348 printf("(mbox) Adding cc: %s from line '%s'\n",
1349 $1, $_) unless $quiet;
1350 push @cc, $1;
1352 elsif (/^To:\s+(.*)$/i) {
1353 foreach my $addr (parse_address_line($1)) {
1354 printf("(mbox) Adding to: %s from line '%s'\n",
1355 $addr, $_) unless $quiet;
1356 push @to, $addr;
1359 elsif (/^Cc:\s+(.*)$/i) {
1360 foreach my $addr (parse_address_line($1)) {
1361 if (unquote_rfc2047($addr) eq $sender) {
1362 next if ($suppress_cc{'self'});
1363 } else {
1364 next if ($suppress_cc{'cc'});
1366 printf("(mbox) Adding cc: %s from line '%s'\n",
1367 $addr, $_) unless $quiet;
1368 push @cc, $addr;
1371 elsif (/^Content-type:/i) {
1372 $has_content_type = 1;
1373 if (/charset="?([^ "]+)/) {
1374 $body_encoding = $1;
1376 push @xh, $_;
1378 elsif (/^Message-Id: (.*)/i) {
1379 $message_id = $1;
1381 elsif (!/^Date:\s/i && /^[-A-Za-z]+:\s+\S/) {
1382 push @xh, $_;
1385 } else {
1386 # In the traditional
1387 # "send lots of email" format,
1388 # line 1 = cc
1389 # line 2 = subject
1390 # So let's support that, too.
1391 $input_format = 'lots';
1392 if (@cc == 0 && !$suppress_cc{'cc'}) {
1393 printf("(non-mbox) Adding cc: %s from line '%s'\n",
1394 $_, $_) unless $quiet;
1395 push @cc, $_;
1396 } elsif (!defined $subject) {
1397 $subject = $_;
1401 # Now parse the message body
1402 while(<$fh>) {
1403 $message .= $_;
1404 if (/^(Signed-off-by|Cc): (.*)$/i) {
1405 chomp;
1406 my ($what, $c) = ($1, $2);
1407 chomp $c;
1408 if ($c eq $sender) {
1409 next if ($suppress_cc{'self'});
1410 } else {
1411 next if $suppress_cc{'sob'} and $what =~ /Signed-off-by/i;
1412 next if $suppress_cc{'bodycc'} and $what =~ /Cc/i;
1414 push @cc, $c;
1415 printf("(body) Adding cc: %s from line '%s'\n",
1416 $c, $_) unless $quiet;
1419 close $fh;
1421 push @to, recipients_cmd("to-cmd", "to", $to_cmd, $t)
1422 if defined $to_cmd;
1423 push @cc, recipients_cmd("cc-cmd", "cc", $cc_cmd, $t)
1424 if defined $cc_cmd && !$suppress_cc{'cccmd'};
1426 if ($broken_encoding{$t} && !$has_content_type) {
1427 $has_content_type = 1;
1428 push @xh, "MIME-Version: 1.0",
1429 "Content-Type: text/plain; charset=$auto_8bit_encoding",
1430 "Content-Transfer-Encoding: 8bit";
1431 $body_encoding = $auto_8bit_encoding;
1434 if ($broken_encoding{$t} && !is_rfc2047_quoted($subject)) {
1435 $subject = quote_subject($subject, $auto_8bit_encoding);
1438 if (defined $author and $author ne $sender) {
1439 $message = "From: $author\n\n$message";
1440 if (defined $author_encoding) {
1441 if ($has_content_type) {
1442 if ($body_encoding eq $author_encoding) {
1443 # ok, we already have the right encoding
1445 else {
1446 # uh oh, we should re-encode
1449 else {
1450 $has_content_type = 1;
1451 push @xh,
1452 'MIME-Version: 1.0',
1453 "Content-Type: text/plain; charset=$author_encoding",
1454 'Content-Transfer-Encoding: 8bit';
1459 $needs_confirm = (
1460 $confirm eq "always" or
1461 ($confirm =~ /^(?:auto|cc)$/ && @cc) or
1462 ($confirm =~ /^(?:auto|compose)$/ && $compose && $message_num == 1));
1463 $needs_confirm = "inform" if ($needs_confirm && $confirm_unconfigured && @cc);
1465 @to = validate_address_list(sanitize_address_list(@to));
1466 @cc = validate_address_list(sanitize_address_list(@cc));
1468 @to = (@initial_to, @to);
1469 @cc = (@initial_cc, @cc);
1471 my $message_was_sent = send_message();
1473 # set up for the next message
1474 if ($thread && $message_was_sent &&
1475 (chain_reply_to() || !defined $reply_to || length($reply_to) == 0 ||
1476 $message_num == 1)) {
1477 $reply_to = $message_id;
1478 if (length $references > 0) {
1479 $references .= "\n $message_id";
1480 } else {
1481 $references = "$message_id";
1484 $message_id = undef;
1487 # Execute a command (e.g. $to_cmd) to get a list of email addresses
1488 # and return a results array
1489 sub recipients_cmd {
1490 my ($prefix, $what, $cmd, $file) = @_;
1492 my $sanitized_sender = sanitize_address($sender);
1493 my @addresses = ();
1494 open my $fh, "-|", "$cmd \Q$file\E"
1495 or die "($prefix) Could not execute '$cmd'";
1496 while (my $address = <$fh>) {
1497 $address =~ s/^\s*//g;
1498 $address =~ s/\s*$//g;
1499 $address = sanitize_address($address);
1500 next if ($address eq $sanitized_sender and $suppress_from);
1501 push @addresses, $address;
1502 printf("($prefix) Adding %s: %s from: '%s'\n",
1503 $what, $address, $cmd) unless $quiet;
1505 close $fh
1506 or die "($prefix) failed to close pipe to '$cmd'";
1507 return @addresses;
1510 cleanup_compose_files();
1512 sub cleanup_compose_files {
1513 unlink($compose_filename, $compose_filename . ".final") if $compose;
1516 $smtp->quit if $smtp;
1518 sub unique_email_list {
1519 my %seen;
1520 my @emails;
1522 foreach my $entry (@_) {
1523 my $clean = extract_valid_address_or_die($entry);
1524 $seen{$clean} ||= 0;
1525 next if $seen{$clean}++;
1526 push @emails, $entry;
1528 return @emails;
1531 sub validate_patch {
1532 my $fn = shift;
1533 open(my $fh, '<', $fn)
1534 or die "unable to open $fn: $!\n";
1535 while (my $line = <$fh>) {
1536 if (length($line) > 998) {
1537 return "$.: patch contains a line longer than 998 characters";
1540 return;
1543 sub file_has_nonascii {
1544 my $fn = shift;
1545 open(my $fh, '<', $fn)
1546 or die "unable to open $fn: $!\n";
1547 while (my $line = <$fh>) {
1548 return 1 if $line =~ /[^[:ascii:]]/;
1550 return 0;
1553 sub body_or_subject_has_nonascii {
1554 my $fn = shift;
1555 open(my $fh, '<', $fn)
1556 or die "unable to open $fn: $!\n";
1557 while (my $line = <$fh>) {
1558 last if $line =~ /^$/;
1559 return 1 if $line =~ /^Subject.*[^[:ascii:]]/;
1561 while (my $line = <$fh>) {
1562 return 1 if $line =~ /[^[:ascii:]]/;
1564 return 0;