3 # Copyright 2002,2005 Greg Kroah-Hartman <greg@kroah.com>
4 # Copyright 2005 Ryan Anderson <ryan@michonline.com>
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.
22 use POSIX qw/strftime/;
27 use File::Temp qw/ tempdir tempfile /;
28 use File::Spec::Functions qw(catdir catfile);
29 use Git::LoadCPAN::Error qw(:try);
30 use Cwd qw(abs_path cwd);
35 use Git::LoadCPAN::Mail::Address;
37 Getopt::Long::Configure qw/ pass_through /;
41 my ($class, $reason) = @_;
42 return bless \$reason, shift;
46 die "Cannot use readline on FakeTerm: $$self";
53 git send-email [options] <file | directory | rev-list options >
54 git send-email --dump-aliases
57 --from <str> * Email From:
58 --[no-]to <str> * Email To:
59 --[no-]cc <str> * Email Cc:
60 --[no-]bcc <str> * Email Bcc:
61 --subject <str> * Email "Subject:"
62 --reply-to <str> * Email "Reply-To:"
63 --in-reply-to <str> * Email "In-Reply-To:"
64 --[no-]xmailer * Add "X-Mailer:" header (default).
65 --[no-]annotate * Review each patch that will be sent in an editor.
66 --compose * Open an editor for introduction.
67 --compose-encoding <str> * Encoding to assume for introduction.
68 --8bit-encoding <str> * Encoding to assume 8bit mails if undeclared
69 --transfer-encoding <str> * Transfer encoding to use (quoted-printable, 8bit, base64)
72 --envelope-sender <str> * Email envelope sender.
73 --smtp-server <str:int> * Outgoing SMTP server to use. The port
74 is optional. Default 'localhost'.
75 --smtp-server-option <str> * Outgoing SMTP server option to use.
76 --smtp-server-port <int> * Outgoing SMTP server port.
77 --smtp-user <str> * Username for SMTP-AUTH.
78 --smtp-pass <str> * Password for SMTP-AUTH; not necessary.
79 --smtp-encryption <str> * tls or ssl; anything else disables.
80 --smtp-ssl * Deprecated. Use '--smtp-encryption ssl'.
81 --smtp-ssl-cert-path <str> * Path to ca-certificates (either directory or file).
82 Pass an empty string to disable certificate
84 --smtp-domain <str> * The domain name sent to HELO/EHLO handshake
85 --smtp-auth <str> * Space-separated list of allowed AUTH mechanisms.
86 This setting forces to use one of the listed mechanisms.
87 --smtp-debug <0|1> * Disable, enable Net::SMTP debug.
89 --batch-size <int> * send max <int> message per connection.
90 --relogin-delay <int> * delay <int> seconds between two successive login.
91 This option can only be used with --batch-size
94 --identity <str> * Use the sendemail.<id> options.
95 --to-cmd <str> * Email To: via `<str> \$patch_path`
96 --cc-cmd <str> * Email Cc: via `<str> \$patch_path`
97 --suppress-cc <str> * author, self, sob, cc, cccmd, body, bodycc, all.
98 --[no-]cc-cover * Email Cc: addresses in the cover letter.
99 --[no-]to-cover * Email To: addresses in the cover letter.
100 --[no-]signed-off-by-cc * Send to Signed-off-by: addresses. Default on.
101 --[no-]suppress-from * Send to self. Default off.
102 --[no-]chain-reply-to * Chain In-Reply-To: fields. Default off.
103 --[no-]thread * Use In-Reply-To: field. Default on.
106 --confirm <str> * Confirm recipients before sending;
107 auto, cc, compose, always, or never.
108 --quiet * Output one line of info per email.
109 --dry-run * Don't actually send the emails.
110 --[no-]validate * Perform patch sanity checks. Default on.
111 --[no-]format-patch * understand any non optional arguments as
112 `git format-patch` ones.
113 --force * Send even if safety checks would prevent it.
116 --dump-aliases * Dump configured aliases and exit.
122 # most mail servers generate the Date: header, but not all...
123 sub format_2822_time {
125 my @localtm = localtime($time);
126 my @gmttm = gmtime($time);
127 my $localmin = $localtm[1] + $localtm[2] * 60;
128 my $gmtmin = $gmttm[1] + $gmttm[2] * 60;
129 if ($localtm[0] != $gmttm[0]) {
130 die __("local zone differs from GMT by a non-minute interval\n");
132 if ((($gmttm[6] + 1) % 7) == $localtm[6]) {
134 } elsif ((($gmttm[6] - 1) % 7) == $localtm[6]) {
136 } elsif ($gmttm[6] != $localtm[6]) {
137 die __("local time offset greater than or equal to 24 hours\n");
139 my $offset = $localmin - $gmtmin;
140 my $offhour = $offset / 60;
141 my $offmin = abs($offset % 60);
142 if (abs($offhour) >= 24) {
143 die __("local time offset greater than or equal to 24 hours\n");
146 return sprintf("%s, %2d %s %d %02d:%02d:%02d %s%02d%02d",
147 qw(Sun Mon Tue Wed Thu Fri Sat)[$localtm[6]],
149 qw(Jan Feb Mar Apr May Jun
150 Jul Aug Sep Oct Nov Dec)[$localtm[4]],
155 ($offset >= 0) ? '+' : '-',
161 my $have_email_valid = eval { require Email::Valid; 1 };
166 # Regexes for RFC 2047 productions.
167 my $re_token = qr/[^][()<>@,;:\\"\/?.= \000-\037\177-\377]+/;
168 my $re_encoded_text = qr/[^? \000-\037\177-\377]+/;
169 my $re_encoded_word = qr/=\?($re_token)\?($re_token)\?($re_encoded_text)\?=/;
171 # Variables we fill in automatically, or via prompting:
172 my (@to,@cc,@xh,$envelope_sender,
173 $initial_in_reply_to,$reply_to,$initial_subject,@files,
174 $author,$sender,$smtp_authpass,$annotate,$compose,$time);
175 # Things we either get from config, *or* are overridden on the
177 my ($no_cc, $no_to, $no_bcc);
178 my (@config_to, @getopt_to);
179 my (@config_cc, @getopt_cc);
180 my (@config_bcc, @getopt_bcc);
183 #$initial_in_reply_to = ''; #<20050203173208.GA23964@foobar.com>';
185 my $repo = eval { Git->repository() };
186 my @repo = $repo ? ($repo) : ();
188 $ENV{"GIT_SEND_EMAIL_NOTTY"}
189 ? new Term::ReadLine 'git-send-email', \*STDIN, \*STDOUT
190 : new Term::ReadLine 'git-send-email';
193 $term = new FakeTerm "$@: going non-interactive";
196 # Behavior modification variables
197 my ($quiet, $dry_run) = (0, 0);
199 my $compose_filename;
201 my $dump_aliases = 0;
203 # Handle interactive edition of files.
208 if (!defined($editor)) {
209 $editor = Git::command_oneline('var', 'GIT_EDITOR');
211 if (defined($multiedit) && !$multiedit) {
213 system('sh', '-c', $editor.' "$@"', $editor, $_);
214 if (($? & 127) || ($? >> 8)) {
215 die(__("the editor exited uncleanly, aborting everything"));
219 system('sh', '-c', $editor.' "$@"', $editor, @_);
220 if (($? & 127) || ($? >> 8)) {
221 die(__("the editor exited uncleanly, aborting everything"));
226 # Variables with corresponding config settings
227 my ($suppress_from, $signed_off_by_cc);
228 my ($cover_cc, $cover_to);
229 my ($to_cmd, $cc_cmd);
230 my ($smtp_server, $smtp_server_port, @smtp_server_options);
231 my ($smtp_authuser, $smtp_encryption, $smtp_ssl_cert_path);
232 my ($batch_size, $relogin_delay);
233 my ($identity, $aliasfiletype, @alias_files, $smtp_domain, $smtp_auth);
236 my ($auto_8bit_encoding);
237 my ($compose_encoding);
238 # Variables with corresponding config settings & hardcoded defaults
239 my ($debug_net_smtp) = 0; # Net::SMTP, see send_message()
241 my $chain_reply_to = 0;
244 my $target_xfer_encoding = 'auto';
246 my %config_bool_settings = (
247 "thread" => \$thread,
248 "chainreplyto" => \$chain_reply_to,
249 "suppressfrom" => \$suppress_from,
250 "signedoffbycc" => \$signed_off_by_cc,
251 "cccover" => \$cover_cc,
252 "tocover" => \$cover_to,
253 "signedoffcc" => \$signed_off_by_cc,
254 "validate" => \$validate,
255 "multiedit" => \$multiedit,
256 "annotate" => \$annotate,
257 "xmailer" => \$use_xmailer,
260 my %config_settings = (
261 "smtpserver" => \$smtp_server,
262 "smtpserverport" => \$smtp_server_port,
263 "smtpserveroption" => \@smtp_server_options,
264 "smtpuser" => \$smtp_authuser,
265 "smtppass" => \$smtp_authpass,
266 "smtpdomain" => \$smtp_domain,
267 "smtpauth" => \$smtp_auth,
268 "smtpbatchsize" => \$batch_size,
269 "smtprelogindelay" => \$relogin_delay,
274 "aliasfiletype" => \$aliasfiletype,
275 "bcc" => \@config_bcc,
276 "suppresscc" => \@suppress_cc,
277 "envelopesender" => \$envelope_sender,
278 "confirm" => \$confirm,
280 "assume8bitencoding" => \$auto_8bit_encoding,
281 "composeencoding" => \$compose_encoding,
282 "transferencoding" => \$target_xfer_encoding,
285 my %config_path_settings = (
286 "aliasesfile" => \@alias_files,
287 "smtpsslcertpath" => \$smtp_ssl_cert_path,
290 # Handle Uncouth Termination
294 print color("reset"), "\n";
296 # SMTP password masked
299 # tmp files from --compose
300 if (defined $compose_filename) {
301 if (-e $compose_filename) {
302 printf __("'%s' contains an intermediate version ".
303 "of the email you were composing.\n"),
306 if (-e ($compose_filename . ".final")) {
307 printf __("'%s.final' contains the composed email.\n"),
315 $SIG{TERM} = \&signal_handler;
316 $SIG{INT} = \&signal_handler;
318 # Read our sendemail.* config
322 foreach my $setting (keys %config_bool_settings) {
323 my $target = $config_bool_settings{$setting};
324 my $v = Git::config_bool(@repo, "$prefix.$setting");
325 $$target = $v if defined $v;
328 foreach my $setting (keys %config_path_settings) {
329 my $target = $config_path_settings{$setting};
330 if (ref($target) eq "ARRAY") {
332 my @values = Git::config_path(@repo, "$prefix.$setting");
333 @$target = @values if (@values && defined $values[0]);
337 my $v = Git::config_path(@repo, "$prefix.$setting");
338 $$target = $v if defined $v;
342 foreach my $setting (keys %config_settings) {
343 my $target = $config_settings{$setting};
344 if (ref($target) eq "ARRAY") {
346 my @values = Git::config(@repo, "$prefix.$setting");
347 @$target = @values if (@values && defined $values[0]);
351 my $v = Git::config(@repo, "$prefix.$setting");
352 $$target = $v if defined $v;
356 if (!defined $smtp_encryption) {
357 my $enc = Git::config(@repo, "$prefix.smtpencryption");
359 $smtp_encryption = $enc;
360 } elsif (Git::config_bool(@repo, "$prefix.smtpssl")) {
361 $smtp_encryption = 'ssl';
366 $identity = Git::config(@repo, "sendemail.identity");
367 read_config("sendemail.$identity") if defined $identity;
368 read_config("sendemail");
370 # Begin by accumulating all the variables (defined above), that we will end up
371 # needing, first, from the command line:
374 my $rc = GetOptions("h" => \$help,
375 "dump-aliases" => \$dump_aliases);
377 die __("--dump-aliases incompatible with other options\n")
378 if !$help and $dump_aliases and @ARGV;
380 "sender|from=s" => \$sender,
381 "in-reply-to=s" => \$initial_in_reply_to,
382 "reply-to=s" => \$reply_to,
383 "subject=s" => \$initial_subject,
384 "to=s" => \@getopt_to,
385 "to-cmd=s" => \$to_cmd,
387 "cc=s" => \@getopt_cc,
389 "bcc=s" => \@getopt_bcc,
390 "no-bcc" => \$no_bcc,
391 "chain-reply-to!" => \$chain_reply_to,
392 "no-chain-reply-to" => sub {$chain_reply_to = 0},
393 "smtp-server=s" => \$smtp_server,
394 "smtp-server-option=s" => \@smtp_server_options,
395 "smtp-server-port=s" => \$smtp_server_port,
396 "smtp-user=s" => \$smtp_authuser,
397 "smtp-pass:s" => \$smtp_authpass,
398 "smtp-ssl" => sub { $smtp_encryption = 'ssl' },
399 "smtp-encryption=s" => \$smtp_encryption,
400 "smtp-ssl-cert-path=s" => \$smtp_ssl_cert_path,
401 "smtp-debug:i" => \$debug_net_smtp,
402 "smtp-domain:s" => \$smtp_domain,
403 "smtp-auth=s" => \$smtp_auth,
404 "identity=s" => \$identity,
405 "annotate!" => \$annotate,
406 "no-annotate" => sub {$annotate = 0},
407 "compose" => \$compose,
409 "cc-cmd=s" => \$cc_cmd,
410 "suppress-from!" => \$suppress_from,
411 "no-suppress-from" => sub {$suppress_from = 0},
412 "suppress-cc=s" => \@suppress_cc,
413 "signed-off-cc|signed-off-by-cc!" => \$signed_off_by_cc,
414 "no-signed-off-cc|no-signed-off-by-cc" => sub {$signed_off_by_cc = 0},
415 "cc-cover|cc-cover!" => \$cover_cc,
416 "no-cc-cover" => sub {$cover_cc = 0},
417 "to-cover|to-cover!" => \$cover_to,
418 "no-to-cover" => sub {$cover_to = 0},
419 "confirm=s" => \$confirm,
420 "dry-run" => \$dry_run,
421 "envelope-sender=s" => \$envelope_sender,
422 "thread!" => \$thread,
423 "no-thread" => sub {$thread = 0},
424 "validate!" => \$validate,
425 "no-validate" => sub {$validate = 0},
426 "transfer-encoding=s" => \$target_xfer_encoding,
427 "format-patch!" => \$format_patch,
428 "no-format-patch" => sub {$format_patch = 0},
429 "8bit-encoding=s" => \$auto_8bit_encoding,
430 "compose-encoding=s" => \$compose_encoding,
432 "xmailer!" => \$use_xmailer,
433 "no-xmailer" => sub {$use_xmailer = 0},
434 "batch-size=i" => \$batch_size,
435 "relogin-delay=i" => \$relogin_delay,
438 # Munge any "either config or getopt, not both" variables
439 my @initial_to = @getopt_to ? @getopt_to : ($no_to ? () : @config_to);
440 my @initial_cc = @getopt_cc ? @getopt_cc : ($no_cc ? () : @config_cc);
441 my @initial_bcc = @getopt_bcc ? @getopt_bcc : ($no_bcc ? () : @config_bcc);
448 die __("Cannot run git format-patch from outside a repository\n")
449 if $format_patch and not $repo;
451 die __("`batch-size` and `relogin` must be specified together " .
452 "(via command-line or configuration option)\n")
453 if defined $relogin_delay and not defined $batch_size;
455 # 'default' encryption is none -- this only prevents a warning
456 $smtp_encryption = '' unless (defined $smtp_encryption);
458 # Set CC suppressions
461 foreach my $entry (@suppress_cc) {
462 die sprintf(__("Unknown --suppress-cc field: '%s'\n"), $entry)
463 unless $entry =~ /^(?:all|cccmd|cc|author|self|sob|body|bodycc)$/;
464 $suppress_cc{$entry} = 1;
468 if ($suppress_cc{'all'}) {
469 foreach my $entry (qw (cccmd cc author self sob body bodycc)) {
470 $suppress_cc{$entry} = 1;
472 delete $suppress_cc{'all'};
475 # If explicit old-style ones are specified, they trump --suppress-cc.
476 $suppress_cc{'self'} = $suppress_from if defined $suppress_from;
477 $suppress_cc{'sob'} = !$signed_off_by_cc if defined $signed_off_by_cc;
479 if ($suppress_cc{'body'}) {
480 foreach my $entry (qw (sob bodycc)) {
481 $suppress_cc{$entry} = 1;
483 delete $suppress_cc{'body'};
486 # Set confirm's default value
487 my $confirm_unconfigured = !defined $confirm;
488 if ($confirm_unconfigured) {
489 $confirm = scalar %suppress_cc ? 'compose' : 'auto';
491 die sprintf(__("Unknown --confirm setting: '%s'\n"), $confirm)
492 unless $confirm =~ /^(?:auto|cc|compose|always|never)/;
494 # Debugging, print out the suppressions.
496 print "suppressions:\n";
497 foreach my $entry (keys %suppress_cc) {
498 printf " %-5s -> $suppress_cc{$entry}\n", $entry;
502 my ($repoauthor, $repocommitter);
503 ($repoauthor) = Git::ident_person(@repo, 'author');
504 ($repocommitter) = Git::ident_person(@repo, 'committer');
506 sub parse_address_line {
507 return map { $_->format } Mail::Address->parse($_[0]);
511 return quotewords('\s*,\s*', 1, @_);
516 sub parse_sendmail_alias {
519 printf STDERR __("warning: sendmail alias with quotes is not supported: %s\n"), $_;
520 } elsif (/:include:/) {
521 printf STDERR __("warning: `:include:` not supported: %s\n"), $_;
523 printf STDERR __("warning: `/file` or `|pipe` redirection not supported: %s\n"), $_;
524 } elsif (/^(\S+?)\s*:\s*(.+)$/) {
525 my ($alias, $addr) = ($1, $2);
526 $aliases{$alias} = [ split_addrs($addr) ];
528 printf STDERR __("warning: sendmail line is not recognized: %s\n"), $_;
532 sub parse_sendmail_aliases {
537 next if /^\s*$/ || /^\s*#/;
538 $s .= $_, next if $s =~ s/\\$// || s/^\s+//;
539 parse_sendmail_alias($s) if $s;
542 $s =~ s/\\$//; # silently tolerate stray '\' on last line
543 parse_sendmail_alias($s) if $s;
547 # multiline formats can be supported in the future
548 mutt => sub { my $fh = shift; while (<$fh>) {
549 if (/^\s*alias\s+(?:-group\s+\S+\s+)*(\S+)\s+(.*)$/) {
550 my ($alias, $addr) = ($1, $2);
551 $addr =~ s/#.*$//; # mutt allows # comments
552 # commas delimit multiple addresses
553 my @addr = split_addrs($addr);
555 # quotes may be escaped in the file,
556 # unescape them so we do not double-escape them later.
557 s/\\"/"/g foreach @addr;
558 $aliases{$alias} = \@addr
560 mailrc => sub { my $fh = shift; while (<$fh>) {
561 if (/^alias\s+(\S+)\s+(.*?)\s*$/) {
562 # spaces delimit multiple addresses
563 $aliases{$1} = [ quotewords('\s+', 0, $2) ];
565 pine => sub { my $fh = shift; my $f='\t[^\t]*';
566 for (my $x = ''; defined($x); $x = $_) {
568 $x .= $1 while(defined($_ = <$fh>) && /^ +(.*)$/);
569 $x =~ /^(\S+)$f\t\(?([^\t]+?)\)?(:?$f){0,2}$/ or next;
570 $aliases{$1} = [ split_addrs($2) ];
572 elm => sub { my $fh = shift;
574 if (/^(\S+)\s+=\s+[^=]+=\s(\S+)/) {
575 my ($alias, $addr) = ($1, $2);
576 $aliases{$alias} = [ split_addrs($addr) ];
579 sendmail => \&parse_sendmail_aliases,
580 gnus => sub { my $fh = shift; while (<$fh>) {
581 if (/\(define-mail-alias\s+"(\S+?)"\s+"(\S+?)"\)/) {
582 $aliases{$1} = [ $2 ];
586 if (@alias_files and $aliasfiletype and defined $parse_alias{$aliasfiletype}) {
587 foreach my $file (@alias_files) {
588 open my $fh, '<', $file or die "opening $file: $!\n";
589 $parse_alias{$aliasfiletype}->($fh);
595 print "$_\n" for (sort keys %aliases);
599 # is_format_patch_arg($f) returns 0 if $f names a patch, or 1 if
600 # $f is a revision list specification to be passed to format-patch.
601 sub is_format_patch_arg {
605 $repo->command('rev-parse', '--verify', '--quiet', $f);
606 if (defined($format_patch)) {
607 return $format_patch;
609 die sprintf(__ <<EOF, $f, $f);
610 File '%s' exists but it could also be the range of commits
611 to produce patches for. Please disambiguate by...
613 * Saying "./%s" if you mean a file; or
614 * Giving --format-patch option if you mean a range.
616 } catch Git::Error::Command with {
617 # Not a valid revision. Treat it as a filename.
622 # Now that all the defaults are set, process the rest of the command line
623 # arguments and collect up the files that need to be processed.
625 while (defined(my $f = shift @ARGV)) {
627 push @rev_list_opts, "--", @ARGV;
629 } elsif (-d $f and !is_format_patch_arg($f)) {
631 or die sprintf(__("Failed to opendir %s: %s"), $f, $!);
633 push @files, grep { -f $_ } map { catfile($f, $_) }
636 } elsif ((-f $f or -p $f) and !is_format_patch_arg($f)) {
639 push @rev_list_opts, $f;
643 if (@rev_list_opts) {
644 die __("Cannot run git format-patch from outside a repository\n")
646 push @files, $repo->command('format-patch', '-o', tempdir(CLEANUP => 1), @rev_list_opts);
649 @files = handle_backup_files(@files);
652 foreach my $f (@files) {
654 my $error = validate_patch($f, $target_xfer_encoding);
655 $error and die sprintf(__("fatal: %s: %s\nwarning: no patches were sent\n"),
663 print $_,"\n" for (@files);
666 print STDERR __("\nNo patch files specified!\n\n");
670 sub get_patch_subject {
672 open (my $fh, '<', $fn);
673 while (my $line = <$fh>) {
674 next unless ($line =~ /^Subject: (.*)$/);
679 die sprintf(__("No subject line in %s?"), $fn);
683 # Note that this does not need to be secure, but we will make a small
684 # effort to have it be unique
685 $compose_filename = ($repo ?
686 tempfile(".gitsendemail.msg.XXXXXX", DIR => $repo->repo_path()) :
687 tempfile(".gitsendemail.msg.XXXXXX", DIR => "."))[1];
688 open my $c, ">", $compose_filename
689 or die sprintf(__("Failed to open for writing %s: %s"), $compose_filename, $!);
692 my $tpl_sender = $sender || $repoauthor || $repocommitter || '';
693 my $tpl_subject = $initial_subject || '';
694 my $tpl_in_reply_to = $initial_in_reply_to || '';
695 my $tpl_reply_to = $reply_to || '';
697 print $c <<EOT1, Git::prefix_lines("GIT: ", __ <<EOT2), <<EOT3;
698 From $tpl_sender # This line is ignored.
700 Lines beginning in "GIT:" will be removed.
701 Consider including an overall diffstat or table of contents
702 for the patch you are writing.
704 Clear the body content if you don't wish to send a summary.
707 Reply-To: $tpl_reply_to
708 Subject: $tpl_subject
709 In-Reply-To: $tpl_in_reply_to
713 print $c get_patch_subject($f);
718 do_edit($compose_filename, @files);
720 do_edit($compose_filename);
723 open $c, "<", $compose_filename
724 or die sprintf(__("Failed to open %s: %s"), $compose_filename, $!);
726 if (!defined $compose_encoding) {
727 $compose_encoding = "UTF-8";
731 while (my $line = <$c>) {
732 next if $line =~ m/^GIT:/;
733 parse_header_line($line, \%parsed_email);
735 $parsed_email{'body'} = filter_body($c);
740 open my $c2, ">", $compose_filename . ".final"
741 or die sprintf(__("Failed to open %s.final: %s"), $compose_filename, $!);
744 if ($parsed_email{'From'}) {
745 $sender = delete($parsed_email{'From'});
747 if ($parsed_email{'In-Reply-To'}) {
748 $initial_in_reply_to = delete($parsed_email{'In-Reply-To'});
750 if ($parsed_email{'Reply-To'}) {
751 $reply_to = delete($parsed_email{'Reply-To'});
753 if ($parsed_email{'Subject'}) {
754 $initial_subject = delete($parsed_email{'Subject'});
755 print $c2 "Subject: " .
756 quote_subject($initial_subject, $compose_encoding) .
760 if ($parsed_email{'MIME-Version'}) {
761 print $c2 "MIME-Version: $parsed_email{'MIME-Version'}\n",
762 "Content-Type: $parsed_email{'Content-Type'};\n",
763 "Content-Transfer-Encoding: $parsed_email{'Content-Transfer-Encoding'}\n";
764 delete($parsed_email{'MIME-Version'});
765 delete($parsed_email{'Content-Type'});
766 delete($parsed_email{'Content-Transfer-Encoding'});
767 } elsif (file_has_nonascii($compose_filename)) {
768 my $content_type = (delete($parsed_email{'Content-Type'}) or
769 "text/plain; charset=$compose_encoding");
770 print $c2 "MIME-Version: 1.0\n",
771 "Content-Type: $content_type\n",
772 "Content-Transfer-Encoding: 8bit\n";
774 # Preserve unknown headers
775 foreach my $key (keys %parsed_email) {
776 next if $key eq 'body';
777 print $c2 "$key: $parsed_email{$key}";
780 if ($parsed_email{'body'}) {
781 print $c2 "\n$parsed_email{'body'}\n";
782 delete($parsed_email{'body'});
784 print __("Summary email is empty, skipping it\n");
790 } elsif ($annotate) {
795 my ($prompt, %arg) = @_;
796 my $valid_re = $arg{valid_re};
797 my $default = $arg{default};
798 my $confirm_only = $arg{confirm_only};
801 return defined $default ? $default : undef
802 unless defined $term->IN and defined fileno($term->IN) and
803 defined $term->OUT and defined fileno($term->OUT);
805 $resp = $term->readline($prompt);
806 if (!defined $resp) { # EOF
808 return defined $default ? $default : undef;
810 if ($resp eq '' and defined $default) {
813 if (!defined $valid_re or $resp =~ /$valid_re/) {
817 my $yesno = $term->readline(
818 # TRANSLATORS: please keep [y/N] as is.
819 sprintf(__("Are you sure you want to use <%s> [y/N]? "), $resp));
820 if (defined $yesno && $yesno =~ /y/i) {
828 sub parse_header_line {
830 my $parsed_line = shift;
831 my $addr_pat = join "|", qw(To Cc Bcc);
833 foreach (split(/\n/, $lines)) {
834 if (/^($addr_pat):\s*(.+)$/i) {
835 $parsed_line->{$1} = [ parse_address_line($2) ];
836 } elsif (/^([^:]*):\s*(.+)\s*$/i) {
837 $parsed_line->{$1} = $2;
845 while (my $body_line = <$c>) {
846 if ($body_line !~ m/^GIT:/) {
856 sub file_declares_8bit_cte {
858 open (my $fh, '<', $fn);
859 while (my $line = <$fh>) {
860 last if ($line =~ /^$/);
861 return 1 if ($line =~ /^Content-Transfer-Encoding: .*8bit.*$/);
867 foreach my $f (@files) {
868 next unless (body_or_subject_has_nonascii($f)
869 && !file_declares_8bit_cte($f));
870 $broken_encoding{$f} = 1;
873 if (!defined $auto_8bit_encoding && scalar %broken_encoding) {
874 print __("The following files are 8bit, but do not declare " .
875 "a Content-Transfer-Encoding.\n");
876 foreach my $f (sort keys %broken_encoding) {
879 $auto_8bit_encoding = ask(__("Which 8bit encoding should I declare [UTF-8]? "),
880 valid_re => qr/.{4}/, confirm_only => 1,
886 if (get_patch_subject($f) =~ /\Q*** SUBJECT HERE ***\E/) {
887 die sprintf(__("Refusing to send because the patch\n\t%s\n"
888 . "has the template subject '*** SUBJECT HERE ***'. "
889 . "Pass --force if you really want to send.\n"), $f);
894 if (defined $sender) {
895 $sender =~ s/^\s+|\s+$//g;
896 ($sender) = expand_aliases($sender);
898 $sender = $repoauthor || $repocommitter || '';
901 # $sender could be an already sanitized address
902 # (e.g. sendemail.from could be manually sanitized by user).
903 # But it's a no-op to run sanitize_address on an already sanitized address.
904 $sender = sanitize_address($sender);
906 my $to_whom = __("To whom should the emails be sent (if anyone)?");
908 if (!@initial_to && !defined $to_cmd) {
909 my $to = ask("$to_whom ",
911 valid_re => qr/\@.*\./, confirm_only => 1);
912 push @initial_to, parse_address_line($to) if defined $to; # sanitized/validated later
917 return map { expand_one_alias($_) } @_;
920 my %EXPANDED_ALIASES;
921 sub expand_one_alias {
923 if ($EXPANDED_ALIASES{$alias}) {
924 die sprintf(__("fatal: alias '%s' expands to itself\n"), $alias);
926 local $EXPANDED_ALIASES{$alias} = 1;
927 return $aliases{$alias} ? expand_aliases(@{$aliases{$alias}}) : $alias;
930 @initial_to = process_address_list(@initial_to);
931 @initial_cc = process_address_list(@initial_cc);
932 @initial_bcc = process_address_list(@initial_bcc);
934 if ($thread && !defined $initial_in_reply_to && $prompting) {
935 $initial_in_reply_to = ask(
936 __("Message-ID to be used as In-Reply-To for the first email (if any)? "),
938 valid_re => qr/\@.*\./, confirm_only => 1);
940 if (defined $initial_in_reply_to) {
941 $initial_in_reply_to =~ s/^\s*<?//;
942 $initial_in_reply_to =~ s/>?\s*$//;
943 $initial_in_reply_to = "<$initial_in_reply_to>" if $initial_in_reply_to ne '';
946 if (defined $reply_to) {
947 $reply_to =~ s/^\s+|\s+$//g;
948 ($reply_to) = expand_aliases($reply_to);
949 $reply_to = sanitize_address($reply_to);
952 if (!defined $smtp_server) {
953 my @sendmail_paths = qw( /usr/sbin/sendmail /usr/lib/sendmail );
954 push @sendmail_paths, map {"$_/sendmail"} split /:/, $ENV{PATH};
955 foreach (@sendmail_paths) {
961 $smtp_server ||= 'localhost'; # could be 127.0.0.1, too... *shrug*
964 if ($compose && $compose > 0) {
965 @files = ($compose_filename . ".final", @files);
968 # Variables we set as part of the loop over files
969 our ($message_id, %mail, $subject, $in_reply_to, $references, $message,
970 $needs_confirm, $message_num, $ask_default);
972 sub extract_valid_address {
974 my $local_part_regexp = qr/[^<>"\s@]+/;
975 my $domain_regexp = qr/[^.<>"\s@]+(?:\.[^.<>"\s@]+)+/;
977 # check for a local address:
978 return $address if ($address =~ /^($local_part_regexp)$/);
980 $address =~ s/^\s*<(.*)>\s*$/$1/;
981 if ($have_email_valid) {
982 return scalar Email::Valid->address($address);
985 # less robust/correct than the monster regexp in Email::Valid,
986 # but still does a 99% job, and one less dependency
987 return $1 if $address =~ /($local_part_regexp\@$domain_regexp)/;
991 sub extract_valid_address_or_die {
993 $address = extract_valid_address($address);
994 die sprintf(__("error: unable to extract a valid address from: %s\n"), $address)
999 sub validate_address {
1000 my $address = shift;
1001 while (!extract_valid_address($address)) {
1002 printf STDERR __("error: unable to extract a valid address from: %s\n"), $address;
1003 # TRANSLATORS: Make sure to include [q] [d] [e] in your
1004 # translation. The program will only accept English input
1006 $_ = ask(__("What to do with this address? ([q]uit|[d]rop|[e]dit): "),
1007 valid_re => qr/^(?:quit|q|drop|d|edit|e)/i,
1012 cleanup_compose_files();
1015 $address = ask("$to_whom ",
1017 valid_re => qr/\@.*\./, confirm_only => 1);
1022 sub validate_address_list {
1023 return (grep { defined $_ }
1024 map { validate_address($_) } @_);
1027 # Usually don't need to change anything below here.
1029 # we make a "fake" message id by taking the current number
1030 # of seconds since the beginning of Unix time and tacking on
1031 # a random number to the end, in case we are called quicker than
1032 # 1 second since the last time we were called.
1034 # We'll setup a template for the message id, using the "from" address:
1036 my ($message_id_stamp, $message_id_serial);
1037 sub make_message_id {
1039 if (!defined $message_id_stamp) {
1040 $message_id_stamp = strftime("%Y%m%d%H%M%S.$$", gmtime(time));
1041 $message_id_serial = 0;
1043 $message_id_serial++;
1044 $uniq = "$message_id_stamp-$message_id_serial";
1047 for ($sender, $repocommitter, $repoauthor) {
1048 $du_part = extract_valid_address(sanitize_address($_));
1049 last if (defined $du_part and $du_part ne '');
1051 if (not defined $du_part or $du_part eq '') {
1052 require Sys::Hostname;
1053 $du_part = 'user@' . Sys::Hostname::hostname();
1055 my $message_id_template = "<%s-%s>";
1056 $message_id = sprintf($message_id_template, $uniq, $du_part);
1057 #print "new message id = $message_id\n"; # Was useful for debugging
1062 $time = time - scalar $#files;
1064 sub unquote_rfc2047 {
1067 my $sep = qr/[ \t]+/;
1068 s{$re_encoded_word(?:$sep$re_encoded_word)*}{
1069 my @words = split $sep, $&;
1071 m/$re_encoded_word/;
1075 if ($encoding eq 'q' || $encoding eq 'Q') {
1078 s/=([0-9A-F]{2})/chr(hex($1))/egi;
1080 # other encodings not supported yet
1085 return wantarray ? ($_, $charset) : $_;
1090 my $encoding = shift || 'UTF-8';
1091 s/([^-a-zA-Z0-9!*+\/])/sprintf("=%02X", ord($1))/eg;
1092 s/(.*)/=\?$encoding\?q\?$1\?=/;
1096 sub is_rfc2047_quoted {
1099 $s =~ m/^(?:"[[:ascii:]]*"|$re_encoded_word)$/o;
1102 sub subject_needs_rfc2047_quoting {
1105 return ($s =~ /[^[:ascii:]]/) || ($s =~ /=\?/);
1109 local $subject = shift;
1110 my $encoding = shift || 'UTF-8';
1112 if (subject_needs_rfc2047_quoting($subject)) {
1113 return quote_rfc2047($subject, $encoding);
1118 # use the simplest quoting being able to handle the recipient
1119 sub sanitize_address {
1120 my ($recipient) = @_;
1122 # remove garbage after email address
1123 $recipient =~ s/(.*>).*$/$1/;
1125 my ($recipient_name, $recipient_addr) = ($recipient =~ /^(.*?)\s*(<.*)/);
1127 if (not $recipient_name) {
1131 # if recipient_name is already quoted, do nothing
1132 if (is_rfc2047_quoted($recipient_name)) {
1136 # remove non-escaped quotes
1137 $recipient_name =~ s/(^|[^\\])"/$1/g;
1139 # rfc2047 is needed if a non-ascii char is included
1140 if ($recipient_name =~ /[^[:ascii:]]/) {
1141 $recipient_name = quote_rfc2047($recipient_name);
1144 # double quotes are needed if specials or CTLs are included
1145 elsif ($recipient_name =~ /[][()<>@,;:\\".\000-\037\177]/) {
1146 $recipient_name =~ s/([\\\r])/\\$1/g;
1147 $recipient_name = qq["$recipient_name"];
1150 return "$recipient_name $recipient_addr";
1154 sub strip_garbage_one_address {
1157 if ($addr =~ /^(("[^"]*"|[^"<]*)? *<[^>]*>).*/) {
1158 # "Foo Bar" <foobar@example.com> [possibly garbage here]
1159 # Foo Bar <foobar@example.com> [possibly garbage here]
1162 if ($addr =~ /^(<[^>]*>).*/) {
1163 # <foo@example.com> [possibly garbage here]
1164 # if garbage contains other addresses, they are ignored.
1167 if ($addr =~ /^([^"#,\s]*)/) {
1168 # address without quoting: remove anything after the address
1174 sub sanitize_address_list {
1175 return (map { sanitize_address($_) } @_);
1178 sub process_address_list {
1179 my @addr_list = map { parse_address_line($_) } @_;
1180 @addr_list = expand_aliases(@addr_list);
1181 @addr_list = sanitize_address_list(@addr_list);
1182 @addr_list = validate_address_list(@addr_list);
1186 # Returns the local Fully Qualified Domain Name (FQDN) if available.
1188 # Tightly configured MTAa require that a caller sends a real DNS
1189 # domain name that corresponds the IP address in the HELO/EHLO
1190 # handshake. This is used to verify the connection and prevent
1191 # spammers from trying to hide their identity. If the DNS and IP don't
1192 # match, the receiveing MTA may deny the connection.
1194 # Here is a deny example of Net::SMTP with the default "localhost.localdomain"
1196 # Net::SMTP=GLOB(0x267ec28)>>> EHLO localhost.localdomain
1197 # Net::SMTP=GLOB(0x267ec28)<<< 550 EHLO argument does not match calling host
1199 # This maildomain*() code is based on ideas in Perl library Test::Reporter
1200 # /usr/share/perl5/Test/Reporter/Mail/Util.pm ==> sub _maildomain ()
1204 return defined $domain && !($^O eq 'darwin' && $domain =~ /\.local$/) && $domain =~ /\./;
1207 sub maildomain_net {
1210 my $domain = Net::Domain::domainname();
1211 $maildomain = $domain if valid_fqdn($domain);
1216 sub maildomain_mta {
1219 for my $host (qw(mailhost localhost)) {
1220 my $smtp = Net::SMTP->new($host);
1221 if (defined $smtp) {
1222 my $domain = $smtp->domain;
1225 $maildomain = $domain if valid_fqdn($domain);
1227 last if $maildomain;
1235 return maildomain_net() || maildomain_mta() || 'localhost.localdomain';
1238 sub smtp_host_string {
1239 if (defined $smtp_server_port) {
1240 return "$smtp_server:$smtp_server_port";
1242 return $smtp_server;
1246 # Returns 1 if authentication succeeded or was not necessary
1247 # (smtp_user was not specified), and 0 otherwise.
1249 sub smtp_auth_maybe {
1250 if (!defined $smtp_authuser || $auth) {
1254 # Workaround AUTH PLAIN/LOGIN interaction defect
1255 # with Authen::SASL::Cyrus
1257 require Authen::SASL;
1258 Authen::SASL->import(qw(Perl));
1261 # Check mechanism naming as defined in:
1262 # https://tools.ietf.org/html/rfc4422#page-8
1263 if ($smtp_auth && $smtp_auth !~ /^(\b[A-Z0-9-_]{1,20}\s*)*$/) {
1264 die "invalid smtp auth: '${smtp_auth}'";
1267 # TODO: Authentication may fail not because credentials were
1268 # invalid but due to other reasons, in which we should not
1269 # reject credentials.
1270 $auth = Git::credential({
1271 'protocol' => 'smtp',
1272 'host' => smtp_host_string(),
1273 'username' => $smtp_authuser,
1274 # if there's no password, "git credential fill" will
1275 # give us one, otherwise it'll just pass this one.
1276 'password' => $smtp_authpass
1281 my $sasl = Authen::SASL->new(
1282 mechanism => $smtp_auth,
1284 user => $cred->{'username'},
1285 pass => $cred->{'password'},
1286 authname => $cred->{'username'},
1290 return !!$smtp->auth($sasl);
1293 return !!$smtp->auth($cred->{'username'}, $cred->{'password'});
1299 sub ssl_verify_params {
1301 require IO::Socket::SSL;
1302 IO::Socket::SSL->import(qw/SSL_VERIFY_PEER SSL_VERIFY_NONE/);
1305 print STDERR "Not using SSL_VERIFY_PEER due to out-of-date IO::Socket::SSL.\n";
1309 if (!defined $smtp_ssl_cert_path) {
1310 # use the OpenSSL defaults
1311 return (SSL_verify_mode => SSL_VERIFY_PEER());
1314 if ($smtp_ssl_cert_path eq "") {
1315 return (SSL_verify_mode => SSL_VERIFY_NONE());
1316 } elsif (-d $smtp_ssl_cert_path) {
1317 return (SSL_verify_mode => SSL_VERIFY_PEER(),
1318 SSL_ca_path => $smtp_ssl_cert_path);
1319 } elsif (-f $smtp_ssl_cert_path) {
1320 return (SSL_verify_mode => SSL_VERIFY_PEER(),
1321 SSL_ca_file => $smtp_ssl_cert_path);
1323 die sprintf(__("CA path \"%s\" does not exist"), $smtp_ssl_cert_path);
1327 sub file_name_is_absolute {
1330 # msys does not grok DOS drive-prefixes
1331 if ($^O eq 'msys') {
1332 return ($path =~ m#^/# || $path =~ m#^[a-zA-Z]\:#)
1335 require File::Spec::Functions;
1336 return File::Spec::Functions::file_name_is_absolute($path);
1339 # Prepares the email, then asks the user what to do.
1341 # If the user chooses to send the email, it's sent and 1 is returned.
1342 # If the user chooses not to send the email, 0 is returned.
1343 # If the user decides they want to make further edits, -1 is returned and the
1344 # caller is expected to call send_message again after the edits are performed.
1346 # If an error occurs sending the email, this just dies.
1349 my @recipients = unique_email_list(@to);
1350 @cc = (grep { my $cc = extract_valid_address_or_die($_);
1351 not grep { $cc eq $_ || $_ =~ /<\Q${cc}\E>$/ } @recipients
1354 my $to = join (",\n\t", @recipients);
1355 @recipients = unique_email_list(@recipients,@cc,@initial_bcc);
1356 @recipients = (map { extract_valid_address_or_die($_) } @recipients);
1357 my $date = format_2822_time($time++);
1358 my $gitversion = '@@GIT_VERSION@@';
1359 if ($gitversion =~ m/..GIT_VERSION../) {
1360 $gitversion = Git::version();
1363 my $cc = join(",\n\t", unique_email_list(@cc));
1366 $ccline = "\nCc: $cc";
1368 make_message_id() unless defined($message_id);
1370 my $header = "From: $sender
1374 Message-Id: $message_id
1377 $header .= "X-Mailer: git-send-email $gitversion\n";
1381 $header .= "In-Reply-To: $in_reply_to\n";
1382 $header .= "References: $references\n";
1385 $header .= "Reply-To: $reply_to\n";
1388 $header .= join("\n", @xh) . "\n";
1391 my @sendmail_parameters = ('-i', @recipients);
1392 my $raw_from = $sender;
1393 if (defined $envelope_sender && $envelope_sender ne "auto") {
1394 $raw_from = $envelope_sender;
1396 $raw_from = extract_valid_address($raw_from);
1397 unshift (@sendmail_parameters,
1398 '-f', $raw_from) if(defined $envelope_sender);
1400 if ($needs_confirm && !$dry_run) {
1401 print "\n$header\n";
1402 if ($needs_confirm eq "inform") {
1403 $confirm_unconfigured = 0; # squelch this message for the rest of this run
1404 $ask_default = "y"; # assume yes on EOF since user hasn't explicitly asked for confirmation
1406 The Cc list above has been expanded by additional
1407 addresses found in the patch commit message. By default
1408 send-email prompts before sending whenever this occurs.
1409 This behavior is controlled by the sendemail.confirm
1410 configuration setting.
1412 For additional information, run 'git send-email --help'.
1413 To retain the current behavior, but squelch this message,
1414 run 'git config --global sendemail.confirm auto'.
1418 # TRANSLATORS: Make sure to include [y] [n] [e] [q] [a] in your
1419 # translation. The program will only accept English input
1421 $_ = ask(__("Send this email? ([y]es|[n]o|[e]dit|[q]uit|[a]ll): "),
1422 valid_re => qr/^(?:yes|y|no|n|edit|e|quit|q|all|a)/i,
1423 default => $ask_default);
1424 die __("Send this email reply required") unless defined $_;
1430 cleanup_compose_files();
1437 unshift (@sendmail_parameters, @smtp_server_options);
1440 # We don't want to send the email.
1441 } elsif (file_name_is_absolute($smtp_server)) {
1442 my $pid = open my $sm, '|-';
1443 defined $pid or die $!;
1445 exec($smtp_server, @sendmail_parameters) or die $!;
1447 print $sm "$header\n$message";
1448 close $sm or die $!;
1451 if (!defined $smtp_server) {
1452 die __("The required SMTP server is not properly defined.")
1456 my $use_net_smtp_ssl = version->parse($Net::SMTP::VERSION) < version->parse("2.34");
1457 $smtp_domain ||= maildomain();
1459 if ($smtp_encryption eq 'ssl') {
1460 $smtp_server_port ||= 465; # ssmtp
1461 require IO::Socket::SSL;
1463 # Suppress "variable accessed once" warning.
1466 $IO::Socket::SSL::DEBUG = 1;
1469 # Net::SMTP::SSL->new() does not forward any SSL options
1470 IO::Socket::SSL::set_client_defaults(
1471 ssl_verify_params());
1473 if ($use_net_smtp_ssl) {
1474 require Net::SMTP::SSL;
1475 $smtp ||= Net::SMTP::SSL->new($smtp_server,
1476 Hello => $smtp_domain,
1477 Port => $smtp_server_port,
1478 Debug => $debug_net_smtp);
1481 $smtp ||= Net::SMTP->new($smtp_server,
1482 Hello => $smtp_domain,
1483 Port => $smtp_server_port,
1484 Debug => $debug_net_smtp,
1489 $smtp_server_port ||= 25;
1490 $smtp ||= Net::SMTP->new($smtp_server,
1491 Hello => $smtp_domain,
1492 Debug => $debug_net_smtp,
1493 Port => $smtp_server_port);
1494 if ($smtp_encryption eq 'tls' && $smtp) {
1495 if ($use_net_smtp_ssl) {
1496 $smtp->command('STARTTLS');
1498 if ($smtp->code != 220) {
1499 die sprintf(__("Server does not support STARTTLS! %s"), $smtp->message);
1501 require Net::SMTP::SSL;
1502 $smtp = Net::SMTP::SSL->start_SSL($smtp,
1503 ssl_verify_params())
1504 or die sprintf(__("STARTTLS failed! %s"), IO::Socket::SSL::errstr());
1507 $smtp->starttls(ssl_verify_params())
1508 or die sprintf(__("STARTTLS failed! %s"), IO::Socket::SSL::errstr());
1510 $smtp_encryption = '';
1511 # Send EHLO again to receive fresh
1512 # supported commands
1513 $smtp->hello($smtp_domain);
1518 die __("Unable to initialize SMTP properly. Check config and use --smtp-debug."),
1519 " VALUES: server=$smtp_server ",
1520 "encryption=$smtp_encryption ",
1521 "hello=$smtp_domain",
1522 defined $smtp_server_port ? " port=$smtp_server_port" : "";
1525 smtp_auth_maybe or die $smtp->message;
1527 $smtp->mail( $raw_from ) or die $smtp->message;
1528 $smtp->to( @recipients ) or die $smtp->message;
1529 $smtp->data or die $smtp->message;
1530 $smtp->datasend("$header\n") or die $smtp->message;
1531 my @lines = split /^/, $message;
1532 foreach my $line (@lines) {
1533 $smtp->datasend("$line") or die $smtp->message;
1535 $smtp->dataend() or die $smtp->message;
1536 $smtp->code =~ /250|200/ or die sprintf(__("Failed to send %s\n"), $subject).$smtp->message;
1539 printf($dry_run ? __("Dry-Sent %s\n") : __("Sent %s\n"), $subject);
1541 print($dry_run ? __("Dry-OK. Log says:\n") : __("OK. Log says:\n"));
1542 if (!file_name_is_absolute($smtp_server)) {
1543 print "Server: $smtp_server\n";
1544 print "MAIL FROM:<$raw_from>\n";
1545 foreach my $entry (@recipients) {
1546 print "RCPT TO:<$entry>\n";
1549 print "Sendmail: $smtp_server ".join(' ',@sendmail_parameters)."\n";
1551 print $header, "\n";
1553 print __("Result: "), $smtp->code, ' ',
1554 ($smtp->message =~ /\n([^\n]+\n)$/s), "\n";
1556 print __("Result: OK\n");
1563 $in_reply_to = $initial_in_reply_to;
1564 $references = $initial_in_reply_to || '';
1565 $subject = $initial_subject;
1568 # Prepares the email, prompts the user, sends it out
1569 # Returns 0 if an edit was done and the function should be called again, or 1
1574 open my $fh, "<", $t or die sprintf(__("can't open file %s"), $t);
1577 my $sauthor = undef;
1578 my $author_encoding;
1579 my $has_content_type;
1582 my $has_mime_version;
1586 my $input_format = undef;
1590 # First unfold multiline header fields
1593 if (/^\s+\S/ and @header) {
1594 chomp($header[$#header]);
1596 $header[$#header] .= $_;
1601 # Now parse the header
1604 $input_format = 'mbox';
1608 if (!defined $input_format && /^[-A-Za-z]+:\s/) {
1609 $input_format = 'mbox';
1612 if (defined $input_format && $input_format eq 'mbox') {
1613 if (/^Subject:\s+(.*)$/i) {
1616 elsif (/^From:\s+(.*)$/i) {
1617 ($author, $author_encoding) = unquote_rfc2047($1);
1618 $sauthor = sanitize_address($author);
1619 next if $suppress_cc{'author'};
1620 next if $suppress_cc{'self'} and $sauthor eq $sender;
1621 printf(__("(mbox) Adding cc: %s from line '%s'\n"),
1622 $1, $_) unless $quiet;
1625 elsif (/^To:\s+(.*)$/i) {
1626 foreach my $addr (parse_address_line($1)) {
1627 printf(__("(mbox) Adding to: %s from line '%s'\n"),
1628 $addr, $_) unless $quiet;
1632 elsif (/^Cc:\s+(.*)$/i) {
1633 foreach my $addr (parse_address_line($1)) {
1634 my $qaddr = unquote_rfc2047($addr);
1635 my $saddr = sanitize_address($qaddr);
1636 if ($saddr eq $sender) {
1637 next if ($suppress_cc{'self'});
1639 next if ($suppress_cc{'cc'});
1641 printf(__("(mbox) Adding cc: %s from line '%s'\n"),
1642 $addr, $_) unless $quiet;
1646 elsif (/^Content-type:/i) {
1647 $has_content_type = 1;
1648 if (/charset="?([^ "]+)/) {
1649 $body_encoding = $1;
1653 elsif (/^MIME-Version/i) {
1654 $has_mime_version = 1;
1657 elsif (/^Message-Id: (.*)/i) {
1660 elsif (/^Content-Transfer-Encoding: (.*)/i) {
1661 $xfer_encoding = $1 if not defined $xfer_encoding;
1663 elsif (/^In-Reply-To: (.*)/i) {
1666 elsif (/^References: (.*)/i) {
1669 elsif (!/^Date:\s/i && /^[-A-Za-z]+:\s+\S/) {
1673 # In the traditional
1674 # "send lots of email" format,
1677 # So let's support that, too.
1678 $input_format = 'lots';
1679 if (@cc == 0 && !$suppress_cc{'cc'}) {
1680 printf(__("(non-mbox) Adding cc: %s from line '%s'\n"),
1681 $_, $_) unless $quiet;
1683 } elsif (!defined $subject) {
1688 # Now parse the message body
1691 if (/^(Signed-off-by|Cc): (.*)/i) {
1693 my ($what, $c) = ($1, $2);
1694 # strip garbage for the address we'll use:
1695 $c = strip_garbage_one_address($c);
1696 # sanitize a bit more to decide whether to suppress the address:
1697 my $sc = sanitize_address($c);
1698 if ($sc eq $sender) {
1699 next if ($suppress_cc{'self'});
1701 next if $suppress_cc{'sob'} and $what =~ /Signed-off-by/i;
1702 next if $suppress_cc{'bodycc'} and $what =~ /Cc/i;
1705 printf(__("(body) Adding cc: %s from line '%s'\n"),
1706 $c, $_) unless $quiet;
1711 push @to, recipients_cmd("to-cmd", "to", $to_cmd, $t)
1713 push @cc, recipients_cmd("cc-cmd", "cc", $cc_cmd, $t)
1714 if defined $cc_cmd && !$suppress_cc{'cccmd'};
1716 if ($broken_encoding{$t} && !$has_content_type) {
1717 $xfer_encoding = '8bit' if not defined $xfer_encoding;
1718 $has_content_type = 1;
1719 push @xh, "Content-Type: text/plain; charset=$auto_8bit_encoding";
1720 $body_encoding = $auto_8bit_encoding;
1723 if ($broken_encoding{$t} && !is_rfc2047_quoted($subject)) {
1724 $subject = quote_subject($subject, $auto_8bit_encoding);
1727 if (defined $sauthor and $sauthor ne $sender) {
1728 $message = "From: $author\n\n$message";
1729 if (defined $author_encoding) {
1730 if ($has_content_type) {
1731 if ($body_encoding eq $author_encoding) {
1732 # ok, we already have the right encoding
1735 # uh oh, we should re-encode
1739 $xfer_encoding = '8bit' if not defined $xfer_encoding;
1740 $has_content_type = 1;
1742 "Content-Type: text/plain; charset=$author_encoding";
1746 $xfer_encoding = '8bit' if not defined $xfer_encoding;
1747 ($message, $xfer_encoding) = apply_transfer_encoding(
1748 $message, $xfer_encoding, $target_xfer_encoding);
1749 push @xh, "Content-Transfer-Encoding: $xfer_encoding";
1750 unshift @xh, 'MIME-Version: 1.0' unless $has_mime_version;
1753 $confirm eq "always" or
1754 ($confirm =~ /^(?:auto|cc)$/ && @cc) or
1755 ($confirm =~ /^(?:auto|compose)$/ && $compose && $message_num == 1));
1756 $needs_confirm = "inform" if ($needs_confirm && $confirm_unconfigured && @cc);
1758 @to = process_address_list(@to);
1759 @cc = process_address_list(@cc);
1761 @to = (@initial_to, @to);
1762 @cc = (@initial_cc, @cc);
1764 if ($message_num == 1) {
1765 if (defined $cover_cc and $cover_cc) {
1768 if (defined $cover_to and $cover_to) {
1773 my $message_was_sent = send_message();
1774 if ($message_was_sent == -1) {
1779 # set up for the next message
1780 if ($thread && $message_was_sent &&
1781 ($chain_reply_to || !defined $in_reply_to || length($in_reply_to) == 0 ||
1782 $message_num == 1)) {
1783 $in_reply_to = $message_id;
1784 if (length $references > 0) {
1785 $references .= "\n $message_id";
1787 $references = "$message_id";
1790 $message_id = undef;
1792 if (defined $batch_size && $num_sent == $batch_size) {
1794 $smtp->quit if defined $smtp;
1797 sleep($relogin_delay) if defined $relogin_delay;
1803 foreach my $t (@files) {
1804 while (!process_file($t)) {
1805 # user edited the file
1809 # Execute a command (e.g. $to_cmd) to get a list of email addresses
1810 # and return a results array
1811 sub recipients_cmd {
1812 my ($prefix, $what, $cmd, $file) = @_;
1815 open my $fh, "-|", "$cmd \Q$file\E"
1816 or die sprintf(__("(%s) Could not execute '%s'"), $prefix, $cmd);
1817 while (my $address = <$fh>) {
1818 $address =~ s/^\s*//g;
1819 $address =~ s/\s*$//g;
1820 $address = sanitize_address($address);
1821 next if ($address eq $sender and $suppress_cc{'self'});
1822 push @addresses, $address;
1823 printf(__("(%s) Adding %s: %s from: '%s'\n"),
1824 $prefix, $what, $address, $cmd) unless $quiet;
1827 or die sprintf(__("(%s) failed to close pipe to '%s'"), $prefix, $cmd);
1831 cleanup_compose_files();
1833 sub cleanup_compose_files {
1834 unlink($compose_filename, $compose_filename . ".final") if $compose;
1837 $smtp->quit if $smtp;
1839 sub apply_transfer_encoding {
1840 my $message = shift;
1844 return $message if ($from eq $to and $from ne '7bit');
1846 require MIME::QuotedPrint;
1847 require MIME::Base64;
1849 $message = MIME::QuotedPrint::decode($message)
1850 if ($from eq 'quoted-printable');
1851 $message = MIME::Base64::decode($message)
1852 if ($from eq 'base64');
1854 $to = ($message =~ /.{999,}/) ? 'quoted-printable' : '8bit'
1857 die __("cannot send message as 7bit")
1858 if ($to eq '7bit' and $message =~ /[^[:ascii:]]/);
1859 return ($message, $to)
1860 if ($to eq '7bit' or $to eq '8bit');
1861 return (MIME::QuotedPrint::encode($message, "\n", 0), $to)
1862 if ($to eq 'quoted-printable');
1863 return (MIME::Base64::encode($message, "\n"), $to)
1864 if ($to eq 'base64');
1865 die __("invalid transfer encoding");
1868 sub unique_email_list {
1872 foreach my $entry (@_) {
1873 my $clean = extract_valid_address_or_die($entry);
1874 $seen{$clean} ||= 0;
1875 next if $seen{$clean}++;
1876 push @emails, $entry;
1881 sub validate_patch {
1882 my ($fn, $xfer_encoding) = @_;
1885 my $validate_hook = catfile(catdir($repo->repo_path(), 'hooks'),
1886 'sendemail-validate');
1888 if (-x $validate_hook) {
1889 my $target = abs_path($fn);
1890 # The hook needs a correct cwd and GIT_DIR.
1891 my $cwd_save = cwd();
1892 chdir($repo->wc_path() or $repo->repo_path())
1893 or die("chdir: $!");
1894 local $ENV{"GIT_DIR"} = $repo->repo_path();
1895 $hook_error = "rejected by sendemail-validate hook"
1896 if system($validate_hook, $target);
1897 chdir($cwd_save) or die("chdir: $!");
1899 return $hook_error if $hook_error;
1902 # Any long lines will be automatically fixed if we use a suitable transfer
1904 unless ($xfer_encoding =~ /^(?:auto|quoted-printable|base64)$/) {
1905 open(my $fh, '<', $fn)
1906 or die sprintf(__("unable to open %s: %s\n"), $fn, $!);
1907 while (my $line = <$fh>) {
1908 if (length($line) > 998) {
1909 return sprintf(__("%s: patch contains a line longer than 998 characters"), $.);
1917 my ($last, $lastlen, $file, $known_suffix) = @_;
1918 my ($suffix, $skip);
1921 if (defined $last &&
1922 ($lastlen < length($file)) &&
1923 (substr($file, 0, $lastlen) eq $last) &&
1924 ($suffix = substr($file, $lastlen)) !~ /^[a-z0-9]/i) {
1925 if (defined $known_suffix && $suffix eq $known_suffix) {
1926 printf(__("Skipping %s with backup suffix '%s'.\n"), $file, $known_suffix);
1929 # TRANSLATORS: please keep "[y|N]" as is.
1930 my $answer = ask(sprintf(__("Do you really want to send %s? [y|N]: "), $file),
1931 valid_re => qr/^(?:y|n)/i,
1933 $skip = ($answer ne 'y');
1935 $known_suffix = $suffix;
1939 return ($skip, $known_suffix);
1942 sub handle_backup_files {
1944 my ($last, $lastlen, $known_suffix, $skip, @result);
1945 for my $file (@file) {
1946 ($skip, $known_suffix) = handle_backup($last, $lastlen,
1947 $file, $known_suffix);
1948 push @result, $file unless $skip;
1950 $lastlen = length($file);
1955 sub file_has_nonascii {
1957 open(my $fh, '<', $fn)
1958 or die sprintf(__("unable to open %s: %s\n"), $fn, $!);
1959 while (my $line = <$fh>) {
1960 return 1 if $line =~ /[^[:ascii:]]/;
1965 sub body_or_subject_has_nonascii {
1967 open(my $fh, '<', $fn)
1968 or die sprintf(__("unable to open %s: %s\n"), $fn, $!);
1969 while (my $line = <$fh>) {
1970 last if $line =~ /^$/;
1971 return 1 if $line =~ /^Subject.*[^[:ascii:]]/;
1973 while (my $line = <$fh>) {
1974 return 1 if $line =~ /[^[:ascii:]]/;