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.
25 use File::Temp qw/ tempdir /;
29 Getopt::Long::Configure qw/ pass_through /;
33 my ($class, $reason) = @_;
34 return bless \$reason, shift;
38 die "Cannot use readline on FakeTerm: $$self";
45 git send-email [options] <file | directory | rev-list options >
48 --from <str> * Email From:
49 --to <str> * Email To:
50 --cc <str> * Email Cc:
51 --bcc <str> * Email Bcc:
52 --subject <str> * Email "Subject:"
53 --in-reply-to <str> * Email "In-Reply-To:"
54 --annotate * Review each patch that will be sent in an editor.
55 --compose * Open an editor for introduction.
58 --envelope-sender <str> * Email envelope sender.
59 --smtp-server <str:int> * Outgoing SMTP server to use. The port
60 is optional. Default 'localhost'.
61 --smtp-server-port <int> * Outgoing SMTP server port.
62 --smtp-user <str> * Username for SMTP-AUTH.
63 --smtp-pass <str> * Password for SMTP-AUTH; not necessary.
64 --smtp-encryption <str> * tls or ssl; anything else disables.
65 --smtp-ssl * Deprecated. Use '--smtp-encryption ssl'.
68 --identity <str> * Use the sendemail.<id> options.
69 --cc-cmd <str> * Email Cc: via `<str> \$patch_path`
70 --suppress-cc <str> * author, self, sob, cccmd, all.
71 --[no-]signed-off-by-cc * Send to Cc: and Signed-off-by:
72 addresses. Default on.
73 --[no-]suppress-from * Send to self. Default off.
74 --[no-]chain-reply-to * Chain In-Reply-To: fields. Default on.
75 --[no-]thread * Use In-Reply-To: field. Default on.
78 --quiet * Output one line of info per email.
79 --dry-run * Don't actually send the emails.
80 --[no-]validate * Perform patch sanity checks. Default on.
81 --[no-]format-patch * understand any non optional arguments as
82 `git format-patch` ones.
88 # most mail servers generate the Date: header, but not all...
89 sub format_2822_time {
91 my @localtm = localtime($time);
92 my @gmttm = gmtime($time);
93 my $localmin = $localtm[1] + $localtm[2] * 60;
94 my $gmtmin = $gmttm[1] + $gmttm[2] * 60;
95 if ($localtm[0] != $gmttm[0]) {
96 die "local zone differs from GMT by a non-minute interval\n";
98 if ((($gmttm[6] + 1) % 7) == $localtm[6]) {
100 } elsif ((($gmttm[6] - 1) % 7) == $localtm[6]) {
102 } elsif ($gmttm[6] != $localtm[6]) {
103 die "local time offset greater than or equal to 24 hours\n";
105 my $offset = $localmin - $gmtmin;
106 my $offhour = $offset / 60;
107 my $offmin = abs($offset % 60);
108 if (abs($offhour) >= 24) {
109 die ("local time offset greater than or equal to 24 hours\n");
112 return sprintf("%s, %2d %s %d %02d:%02d:%02d %s%02d%02d",
113 qw(Sun Mon Tue Wed Thu Fri Sat)[$localtm[6]],
115 qw(Jan Feb Mar Apr May Jun
116 Jul Aug Sep Oct Nov Dec)[$localtm[4]],
121 ($offset >= 0) ? '+' : '-',
127 my $have_email_valid = eval { require Email::Valid; 1 };
131 sub unique_email_list(@);
132 sub cleanup_compose_files();
134 # Variables we fill in automatically, or via prompting:
135 my (@to,@cc,@initial_cc,@bcclist,@xh,
136 $initial_reply_to,$initial_subject,@files,
137 $author,$sender,$smtp_authpass,$annotate,$compose,$time);
142 #$initial_reply_to = ''; #<20050203173208.GA23964@foobar.com>';
144 my $repo = eval { Git->repository() };
145 my @repo = $repo ? ($repo) : ();
147 $ENV{"GIT_SEND_EMAIL_NOTTY"}
148 ? new Term::ReadLine 'git-send-email', \*STDIN, \*STDOUT
149 : new Term::ReadLine 'git-send-email';
152 $term = new FakeTerm "$@: going non-interactive";
155 # Behavior modification variables
156 my ($quiet, $dry_run) = (0, 0);
158 my $compose_filename = $repo->repo_path() . "/.gitsendemail.msg.$$";
160 # Handle interactive edition of files.
162 my $editor = $ENV{GIT_EDITOR} || Git::config(@repo, "core.editor") || $ENV{VISUAL} || $ENV{EDITOR} || "vi";
164 if (defined($multiedit) && !$multiedit) {
166 system('sh', '-c', $editor.' "$@"', $editor, $_);
167 if (($? & 127) || ($? >> 8)) {
168 die("the editor exited uncleanly, aborting everything");
172 system('sh', '-c', $editor.' "$@"', $editor, @_);
173 if (($? & 127) || ($? >> 8)) {
174 die("the editor exited uncleanly, aborting everything");
179 # Variables with corresponding config settings
180 my ($thread, $chain_reply_to, $suppress_from, $signed_off_by_cc, $cc_cmd);
181 my ($smtp_server, $smtp_server_port, $smtp_authuser, $smtp_encryption);
182 my ($identity, $aliasfiletype, @alias_files, @smtp_host_parts);
186 my %config_bool_settings = (
187 "thread" => [\$thread, 1],
188 "chainreplyto" => [\$chain_reply_to, 1],
189 "suppressfrom" => [\$suppress_from, undef],
190 "signedoffbycc" => [\$signed_off_by_cc, undef],
191 "signedoffcc" => [\$signed_off_by_cc, undef], # Deprecated
192 "validate" => [\$validate, 1],
195 my %config_settings = (
196 "smtpserver" => \$smtp_server,
197 "smtpserverport" => \$smtp_server_port,
198 "smtpuser" => \$smtp_authuser,
199 "smtppass" => \$smtp_authpass,
201 "cc" => \@initial_cc,
203 "aliasfiletype" => \$aliasfiletype,
205 "aliasesfile" => \@alias_files,
206 "suppresscc" => \@suppress_cc,
207 "envelopesender" => \$envelope_sender,
208 "multiedit" => \$multiedit,
211 # Handle Uncouth Termination
215 print color("reset"), "\n";
217 # SMTP password masked
220 # tmp files from --compose
221 if (-e $compose_filename) {
222 print "'$compose_filename' contains an intermediate version of the email you were composing.\n";
224 if (-e ($compose_filename . ".final")) {
225 print "'$compose_filename.final' contains the composed email.\n"
231 $SIG{TERM} = \&signal_handler;
232 $SIG{INT} = \&signal_handler;
234 # Begin by accumulating all the variables (defined above), that we will end up
235 # needing, first, from the command line:
237 my $rc = GetOptions("sender|from=s" => \$sender,
238 "in-reply-to=s" => \$initial_reply_to,
239 "subject=s" => \$initial_subject,
241 "cc=s" => \@initial_cc,
242 "bcc=s" => \@bcclist,
243 "chain-reply-to!" => \$chain_reply_to,
244 "smtp-server=s" => \$smtp_server,
245 "smtp-server-port=s" => \$smtp_server_port,
246 "smtp-user=s" => \$smtp_authuser,
247 "smtp-pass:s" => \$smtp_authpass,
248 "smtp-ssl" => sub { $smtp_encryption = 'ssl' },
249 "smtp-encryption=s" => \$smtp_encryption,
250 "identity=s" => \$identity,
251 "annotate" => \$annotate,
252 "compose" => \$compose,
254 "cc-cmd=s" => \$cc_cmd,
255 "suppress-from!" => \$suppress_from,
256 "suppress-cc=s" => \@suppress_cc,
257 "signed-off-cc|signed-off-by-cc!" => \$signed_off_by_cc,
258 "dry-run" => \$dry_run,
259 "envelope-sender=s" => \$envelope_sender,
260 "thread!" => \$thread,
261 "validate!" => \$validate,
262 "format-patch!" => \$format_patch,
269 # Now, let's fill any that aren't set in with defaults:
274 foreach my $setting (keys %config_bool_settings) {
275 my $target = $config_bool_settings{$setting}->[0];
276 $$target = Git::config_bool(@repo, "$prefix.$setting") unless (defined $$target);
279 foreach my $setting (keys %config_settings) {
280 my $target = $config_settings{$setting};
281 if (ref($target) eq "ARRAY") {
283 my @values = Git::config(@repo, "$prefix.$setting");
284 @$target = @values if (@values && defined $values[0]);
288 $$target = Git::config(@repo, "$prefix.$setting") unless (defined $$target);
292 if (!defined $smtp_encryption) {
293 my $enc = Git::config(@repo, "$prefix.smtpencryption");
295 $smtp_encryption = $enc;
296 } elsif (Git::config_bool(@repo, "$prefix.smtpssl")) {
297 $smtp_encryption = 'ssl';
302 # read configuration from [sendemail "$identity"], fall back on [sendemail]
303 $identity = Git::config(@repo, "sendemail.identity") unless (defined $identity);
304 read_config("sendemail.$identity") if (defined $identity);
305 read_config("sendemail");
307 # fall back on builtin bool defaults
308 foreach my $setting (values %config_bool_settings) {
309 ${$setting->[0]} = $setting->[1] unless (defined (${$setting->[0]}));
312 # 'default' encryption is none -- this only prevents a warning
313 $smtp_encryption = '' unless (defined $smtp_encryption);
315 # Set CC suppressions
318 foreach my $entry (@suppress_cc) {
319 die "Unknown --suppress-cc field: '$entry'\n"
320 unless $entry =~ /^(all|cccmd|cc|author|self|sob)$/;
321 $suppress_cc{$entry} = 1;
325 if ($suppress_cc{'all'}) {
326 foreach my $entry (qw (ccmd cc author self sob)) {
327 $suppress_cc{$entry} = 1;
329 delete $suppress_cc{'all'};
332 # If explicit old-style ones are specified, they trump --suppress-cc.
333 $suppress_cc{'self'} = $suppress_from if defined $suppress_from;
334 $suppress_cc{'sob'} = !$signed_off_by_cc if defined $signed_off_by_cc;
336 # Debugging, print out the suppressions.
338 print "suppressions:\n";
339 foreach my $entry (keys %suppress_cc) {
340 printf " %-5s -> $suppress_cc{$entry}\n", $entry;
344 my ($repoauthor, $repocommitter);
345 ($repoauthor) = Git::ident_person(@repo, 'author');
346 ($repocommitter) = Git::ident_person(@repo, 'committer');
348 # Verify the user input
350 foreach my $entry (@to) {
351 die "Comma in --to entry: $entry'\n" unless $entry !~ m/,/;
354 foreach my $entry (@initial_cc) {
355 die "Comma in --cc entry: $entry'\n" unless $entry !~ m/,/;
358 foreach my $entry (@bcclist) {
359 die "Comma in --bcclist entry: $entry'\n" unless $entry !~ m/,/;
364 # multiline formats can be supported in the future
365 mutt => sub { my $fh = shift; while (<$fh>) {
366 if (/^\s*alias\s+(\S+)\s+(.*)$/) {
367 my ($alias, $addr) = ($1, $2);
368 $addr =~ s/#.*$//; # mutt allows # comments
369 # commas delimit multiple addresses
370 $aliases{$alias} = [ split(/\s*,\s*/, $addr) ];
372 mailrc => sub { my $fh = shift; while (<$fh>) {
373 if (/^alias\s+(\S+)\s+(.*)$/) {
374 # spaces delimit multiple addresses
375 $aliases{$1} = [ split(/\s+/, $2) ];
377 pine => sub { my $fh = shift; my $f='\t[^\t]*';
378 for (my $x = ''; defined($x); $x = $_) {
380 $x .= $1 while(defined($_ = <$fh>) && /^ +(.*)$/);
381 $x =~ /^(\S+)$f\t\(?([^\t]+?)\)?(:?$f){0,2}$/ or next;
382 $aliases{$1} = [ split(/\s*,\s*/, $2) ];
384 gnus => sub { my $fh = shift; while (<$fh>) {
385 if (/\(define-mail-alias\s+"(\S+?)"\s+"(\S+?)"\)/) {
386 $aliases{$1} = [ $2 ];
390 if (@alias_files and $aliasfiletype and defined $parse_alias{$aliasfiletype}) {
391 foreach my $file (@alias_files) {
392 open my $fh, '<', $file or die "opening $file: $!\n";
393 $parse_alias{$aliasfiletype}->($fh);
398 ($sender) = expand_aliases($sender) if defined $sender;
400 # returns 1 if the conflict must be solved using it as a format-patch argument
401 sub check_file_rev_conflict($) {
404 $repo->command('rev-parse', '--verify', '--quiet', $f);
405 if (defined($format_patch)) {
407 return $format_patch;
410 File '$f' exists but it could also be the range of commits
411 to produce patches for. Please disambiguate by...
413 * Saying "./$f" if you mean a file; or
414 * Giving --format-patch option if you mean a range.
416 } catch Git::Error::Command with {
421 # Now that all the defaults are set, process the rest of the command line
422 # arguments and collect up the files that need to be processed.
424 while (defined(my $f = shift @ARGV)) {
426 push @rev_list_opts, "--", @ARGV;
428 } elsif (-d $f and !check_file_rev_conflict($f)) {
430 or die "Failed to opendir $f: $!";
432 push @files, grep { -f $_ } map { +$f . "/" . $_ }
435 } elsif ((-f $f or -p $f) and !check_file_rev_conflict($f)) {
438 push @rev_list_opts, $f;
442 if (@rev_list_opts) {
443 push @files, $repo->command('format-patch', '-o', tempdir(CLEANUP => 1), @rev_list_opts);
447 foreach my $f (@files) {
449 my $error = validate_patch($f);
450 $error and die "fatal: $f: $error\nwarning: no patches were sent\n";
457 print $_,"\n" for (@files);
460 print STDERR "\nNo patch files specified!\n\n";
464 sub get_patch_subject($) {
466 open (my $fh, '<', $fn);
467 while (my $line = <$fh>) {
468 next unless ($line =~ /^Subject: (.*)$/);
473 die "No subject line in $fn ?";
477 # Note that this does not need to be secure, but we will make a small
478 # effort to have it be unique
479 open(C,">",$compose_filename)
480 or die "Failed to open for writing $compose_filename: $!";
483 my $tpl_sender = $sender || $repoauthor || $repocommitter || '';
484 my $tpl_subject = $initial_subject || '';
485 my $tpl_reply_to = $initial_reply_to || '';
488 From $tpl_sender # This line is ignored.
489 GIT: Lines beginning in "GIT: " will be removed.
490 GIT: Consider including an overall diffstat or table of contents
491 GIT: for the patch you are writing.
493 GIT: Clear the body content if you don't wish to send a summary.
495 Subject: $tpl_subject
496 In-Reply-To: $tpl_reply_to
500 print C get_patch_subject($f);
504 my $editor = $ENV{GIT_EDITOR} || Git::config(@repo, "core.editor") || $ENV{VISUAL} || $ENV{EDITOR} || "vi";
507 do_edit($compose_filename, @files);
509 do_edit($compose_filename);
512 open(C2,">",$compose_filename . ".final")
513 or die "Failed to open $compose_filename.final : " . $!;
515 open(C,"<",$compose_filename)
516 or die "Failed to open $compose_filename : " . $!;
518 my $need_8bit_cte = file_has_nonascii($compose_filename);
520 my $summary_empty = 1;
524 $summary_empty = 0 unless (/^\n$/);
527 if ($need_8bit_cte) {
528 print C2 "MIME-Version: 1.0\n",
529 "Content-Type: text/plain; ",
531 "Content-Transfer-Encoding: 8bit\n";
533 } elsif (/^MIME-Version:/i) {
535 } elsif (/^Subject:\s*(.+)\s*$/i) {
536 $initial_subject = $1;
537 my $subject = $initial_subject;
539 ($subject =~ /[^[:ascii:]]/ ?
540 quote_rfc2047($subject) :
543 } elsif (/^In-Reply-To:\s*(.+)\s*$/i) {
544 $initial_reply_to = $1;
546 } elsif (/^From:\s*(.+)\s*$/i) {
549 } elsif (/^(?:To|Cc|Bcc):/i) {
550 print "To/Cc/Bcc fields are not interpreted yet, they have been ignored\n";
558 if ($summary_empty) {
559 print "Summary email is empty, skipping it\n";
562 } elsif ($annotate) {
567 if (!defined $sender) {
568 $sender = $repoauthor || $repocommitter || '';
571 $_ = $term->readline("Who should the emails appear to be from? [$sender] ");
576 $sender = $_ if ($_);
577 print "Emails will be sent from: ", $sender, "\n";
585 $_ = $term->readline("Who should the emails be sent to? ", "");
591 push @to, split /,\s*/, $to;
600 @cur = map { $aliases{$_} ? @{$aliases{$_}} : $_ } @last;
601 } while (join(',',@cur) ne join(',',@last));
605 @to = expand_aliases(@to);
606 @to = (map { sanitize_address($_) } @to);
607 @initial_cc = expand_aliases(@initial_cc);
608 @bcclist = expand_aliases(@bcclist);
610 if ($thread && !defined $initial_reply_to && $prompting) {
612 $_= $term->readline("Message-ID to be used as In-Reply-To for the first email? ", $initial_reply_to);
617 $initial_reply_to = $_;
619 if (defined $initial_reply_to) {
620 $initial_reply_to =~ s/^\s*<?//;
621 $initial_reply_to =~ s/>?\s*$//;
622 $initial_reply_to = "<$initial_reply_to>" if $initial_reply_to ne '';
625 if (!defined $smtp_server) {
626 foreach (qw( /usr/sbin/sendmail /usr/lib/sendmail )) {
632 $smtp_server ||= 'localhost'; # could be 127.0.0.1, too... *shrug*
637 $_ = $term->readline("Send this email? (y|n) ");
642 if (uc substr($_,0,1) ne 'Y') {
643 cleanup_compose_files();
648 @files = ($compose_filename . ".final", @files);
652 # Variables we set as part of the loop over files
653 our ($message_id, %mail, $subject, $reply_to, $references, $message);
655 sub extract_valid_address {
657 my $local_part_regexp = '[^<>"\s@]+';
658 my $domain_regexp = '[^.<>"\s@]+(?:\.[^.<>"\s@]+)+';
660 # check for a local address:
661 return $address if ($address =~ /^($local_part_regexp)$/);
663 $address =~ s/^\s*<(.*)>\s*$/$1/;
664 if ($have_email_valid) {
665 return scalar Email::Valid->address($address);
667 # less robust/correct than the monster regexp in Email::Valid,
668 # but still does a 99% job, and one less dependency
669 $address =~ /($local_part_regexp\@$domain_regexp)/;
674 # Usually don't need to change anything below here.
676 # we make a "fake" message id by taking the current number
677 # of seconds since the beginning of Unix time and tacking on
678 # a random number to the end, in case we are called quicker than
679 # 1 second since the last time we were called.
681 # We'll setup a template for the message id, using the "from" address:
683 my ($message_id_stamp, $message_id_serial);
687 if (!defined $message_id_stamp) {
688 $message_id_stamp = sprintf("%s-%s", time, $$);
689 $message_id_serial = 0;
691 $message_id_serial++;
692 $uniq = "$message_id_stamp-$message_id_serial";
695 for ($sender, $repocommitter, $repoauthor) {
696 $du_part = extract_valid_address(sanitize_address($_));
697 last if (defined $du_part and $du_part ne '');
699 if (not defined $du_part or $du_part eq '') {
700 use Sys::Hostname qw();
701 $du_part = 'user@' . Sys::Hostname::hostname();
703 my $message_id_template = "<%s-git-send-email-%s>";
704 $message_id = sprintf($message_id_template, $uniq, $du_part);
705 #print "new message id = $message_id\n"; # Was useful for debugging
710 $time = time - scalar $#files;
712 sub unquote_rfc2047 {
715 if (s/=\?([^?]+)\?q\?(.*)\?=/$2/g) {
718 s/=([0-9A-F]{2})/chr(hex($1))/eg;
720 return wantarray ? ($_, $encoding) : $_;
725 my $encoding = shift || 'utf-8';
726 s/([^-a-zA-Z0-9!*+\/])/sprintf("=%02X", ord($1))/eg;
727 s/(.*)/=\?$encoding\?q\?$1\?=/;
731 # use the simplest quoting being able to handle the recipient
734 my ($recipient) = @_;
735 my ($recipient_name, $recipient_addr) = ($recipient =~ /^(.*?)\s*(<.*)/);
737 if (not $recipient_name) {
741 # if recipient_name is already quoted, do nothing
742 if ($recipient_name =~ /^(".*"|=\?utf-8\?q\?.*\?=)$/) {
746 # rfc2047 is needed if a non-ascii char is included
747 if ($recipient_name =~ /[^[:ascii:]]/) {
748 $recipient_name = quote_rfc2047($recipient_name);
751 # double quotes are needed if specials or CTLs are included
752 elsif ($recipient_name =~ /[][()<>@,;:\\".\000-\037\177]/) {
753 $recipient_name =~ s/(["\\\r])/\\$1/g;
754 $recipient_name = "\"$recipient_name\"";
757 return "$recipient_name $recipient_addr";
763 my @recipients = unique_email_list(@to);
764 @cc = (grep { my $cc = extract_valid_address($_);
765 not grep { $cc eq $_ } @recipients
767 map { sanitize_address($_) }
769 my $to = join (",\n\t", @recipients);
770 @recipients = unique_email_list(@recipients,@cc,@bcclist);
771 @recipients = (map { extract_valid_address($_) } @recipients);
772 my $date = format_2822_time($time++);
773 my $gitversion = '@@GIT_VERSION@@';
774 if ($gitversion =~ m/..GIT_VERSION../) {
775 $gitversion = Git::version();
778 my $cc = join(", ", unique_email_list(@cc));
781 $ccline = "\nCc: $cc";
783 my $sanitized_sender = sanitize_address($sender);
784 make_message_id() unless defined($message_id);
786 my $header = "From: $sanitized_sender
790 Message-Id: $message_id
791 X-Mailer: git-send-email $gitversion
793 if ($thread && $reply_to) {
795 $header .= "In-Reply-To: $reply_to\n";
796 $header .= "References: $references\n";
799 $header .= join("\n", @xh) . "\n";
802 my @sendmail_parameters = ('-i', @recipients);
803 my $raw_from = $sanitized_sender;
804 $raw_from = $envelope_sender if (defined $envelope_sender);
805 $raw_from = extract_valid_address($raw_from);
806 unshift (@sendmail_parameters,
807 '-f', $raw_from) if(defined $envelope_sender);
810 # We don't want to send the email.
811 } elsif ($smtp_server =~ m#^/#) {
812 my $pid = open my $sm, '|-';
813 defined $pid or die $!;
815 exec($smtp_server, @sendmail_parameters) or die $!;
817 print $sm "$header\n$message";
821 if (!defined $smtp_server) {
822 die "The required SMTP server is not properly defined."
825 if ($smtp_encryption eq 'ssl') {
826 $smtp_server_port ||= 465; # ssmtp
827 require Net::SMTP::SSL;
828 $smtp ||= Net::SMTP::SSL->new($smtp_server, Port => $smtp_server_port);
832 $smtp ||= Net::SMTP->new((defined $smtp_server_port)
833 ? "$smtp_server:$smtp_server_port"
835 if ($smtp_encryption eq 'tls') {
836 require Net::SMTP::SSL;
837 $smtp->command('STARTTLS');
839 if ($smtp->code == 220) {
840 $smtp = Net::SMTP::SSL->start_SSL($smtp)
841 or die "STARTTLS failed! ".$smtp->message;
842 $smtp_encryption = '';
843 # Send EHLO again to receive fresh
847 die "Server does not support STARTTLS! ".$smtp->message;
853 die "Unable to initialize SMTP properly. Is there something wrong with your config?";
856 if (defined $smtp_authuser) {
858 if (!defined $smtp_authpass) {
866 } while (!defined $_);
868 chomp($smtp_authpass = $_);
873 $auth ||= $smtp->auth( $smtp_authuser, $smtp_authpass ) or die $smtp->message;
876 $smtp->mail( $raw_from ) or die $smtp->message;
877 $smtp->to( @recipients ) or die $smtp->message;
878 $smtp->data or die $smtp->message;
879 $smtp->datasend("$header\n$message") or die $smtp->message;
880 $smtp->dataend() or die $smtp->message;
881 $smtp->ok or die "Failed to send $subject\n".$smtp->message;
884 printf (($dry_run ? "Dry-" : "")."Sent %s\n", $subject);
886 print (($dry_run ? "Dry-" : "")."OK. Log says:\n");
887 if ($smtp_server !~ m#^/#) {
888 print "Server: $smtp_server\n";
889 print "MAIL FROM:<$raw_from>\n";
890 print "RCPT TO:".join(',',(map { "<$_>" } @recipients))."\n";
892 print "Sendmail: $smtp_server ".join(' ',@sendmail_parameters)."\n";
896 print "Result: ", $smtp->code, ' ',
897 ($smtp->message =~ /\n([^\n]+\n)$/s), "\n";
899 print "Result: OK\n";
904 $reply_to = $initial_reply_to;
905 $references = $initial_reply_to || '';
906 $subject = $initial_subject;
908 foreach my $t (@files) {
909 open(F,"<",$t) or die "can't open file $t";
913 my $has_content_type;
917 my $input_format = undef;
923 $input_format = 'mbox';
927 if (!defined $input_format && /^[-A-Za-z]+:\s/) {
928 $input_format = 'mbox';
931 if (defined $input_format && $input_format eq 'mbox') {
932 if (/^Subject:\s+(.*)$/) {
935 } elsif (/^(Cc|From):\s+(.*)$/) {
936 if (unquote_rfc2047($2) eq $sender) {
937 next if ($suppress_cc{'self'});
939 elsif ($1 eq 'From') {
940 ($author, $author_encoding)
941 = unquote_rfc2047($2);
942 next if ($suppress_cc{'author'});
944 next if ($suppress_cc{'cc'});
946 printf("(mbox) Adding cc: %s from line '%s'\n",
947 $2, $_) unless $quiet;
950 elsif (/^Content-type:/i) {
951 $has_content_type = 1;
952 if (/charset="?([^ "]+)/) {
957 elsif (/^Message-Id: (.*)/i) {
960 elsif (!/^Date:\s/ && /^[-A-Za-z]+:\s+\S/) {
966 # "send lots of email" format,
969 # So let's support that, too.
970 $input_format = 'lots';
971 if (@cc == 0 && !$suppress_cc{'cc'}) {
972 printf("(non-mbox) Adding cc: %s from line '%s'\n",
973 $_, $_) unless $quiet;
977 } elsif (!defined $subject) {
982 # A whitespace line will terminate the headers
988 if (/^(Signed-off-by|Cc): (.*)$/i) {
989 next if ($suppress_cc{'sob'});
993 next if ($c eq $sender and $suppress_cc{'self'});
995 printf("(sob) Adding cc: %s from line '%s'\n",
996 $c, $_) unless $quiet;
1002 if (defined $cc_cmd && !$suppress_cc{'cccmd'}) {
1003 open(F, "$cc_cmd $t |")
1004 or die "(cc-cmd) Could not execute '$cc_cmd'";
1009 next if ($c eq $sender and $suppress_from);
1011 printf("(cc-cmd) Adding cc: %s from: '%s'\n",
1012 $c, $cc_cmd) unless $quiet;
1015 or die "(cc-cmd) failed to close pipe to '$cc_cmd'";
1018 if (defined $author) {
1019 $message = "From: $author\n\n$message";
1020 if (defined $author_encoding) {
1021 if ($has_content_type) {
1022 if ($body_encoding eq $author_encoding) {
1023 # ok, we already have the right encoding
1026 # uh oh, we should re-encode
1031 'MIME-Version: 1.0',
1032 "Content-Type: text/plain; charset=$author_encoding",
1033 'Content-Transfer-Encoding: 8bit';
1040 # set up for the next message
1041 if ($chain_reply_to || !defined $reply_to || length($reply_to) == 0) {
1042 $reply_to = $message_id;
1043 if (length $references > 0) {
1044 $references .= "\n $message_id";
1046 $references = "$message_id";
1049 $message_id = undef;
1053 cleanup_compose_files();
1056 sub cleanup_compose_files() {
1057 unlink($compose_filename, $compose_filename . ".final");
1061 $smtp->quit if $smtp;
1063 sub unique_email_list(@) {
1067 foreach my $entry (@_) {
1068 if (my $clean = extract_valid_address($entry)) {
1069 $seen{$clean} ||= 0;
1070 next if $seen{$clean}++;
1071 push @emails, $entry;
1073 print STDERR "W: unable to extract a valid address",
1080 sub validate_patch {
1082 open(my $fh, '<', $fn)
1083 or die "unable to open $fn: $!\n";
1084 while (my $line = <$fh>) {
1085 if (length($line) > 998) {
1086 return "$.: patch contains a line longer than 998 characters";
1092 sub file_has_nonascii {
1094 open(my $fh, '<', $fn)
1095 or die "unable to open $fn: $!\n";
1096 while (my $line = <$fh>) {
1097 return 1 if $line =~ /[^[:ascii:]]/;