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.
28 my ($class, $reason) = @_;
29 return bless \
$reason, shift;
33 die "Cannot use readline on FakeTerm: $$self";
40 git-send-email [options] <file | directory>...
42 --from Specify the "From:" line of the email to be sent.
44 --to Specify the primary "To:" line of the email.
46 --cc Specify an initial "Cc:" list for the entire series
49 --bcc Specify a list of email addresses that should be Bcc:
52 --compose Use \$EDITOR to edit an introductory message for the
55 --subject Specify the initial "Subject:" line.
56 Only necessary if --compose is also set. If --compose
57 is not set, this will be prompted for.
59 --in-reply-to Specify the first "In-Reply-To:" header line.
60 Only used if --compose is also set. If --compose is not
61 set, this will be prompted for.
63 --chain-reply-to If set, the replies will all be to the previous
64 email sent, rather than to the first email sent.
67 --no-signed-off-cc Suppress the automatic addition of email addresses
68 that appear in Signed-off-by: or Cc: lines to the cc:
69 list. Note: Using this option is not recommended.
71 --smtp-server If set, specifies the outgoing SMTP server to use.
72 Defaults to localhost.
74 --suppress-from Suppress sending emails to yourself if your address
75 appears in a From: line.
77 --quiet Make git-send-email less verbose. One line per email
78 should be all that is output.
80 --dry-run Do everything except actually send the emails.
86 # most mail servers generate the Date: header, but not all...
87 sub format_2822_time
{
89 my @localtm = localtime($time);
90 my @gmttm = gmtime($time);
91 my $localmin = $localtm[1] + $localtm[2] * 60;
92 my $gmtmin = $gmttm[1] + $gmttm[2] * 60;
93 if ($localtm[0] != $gmttm[0]) {
94 die "local zone differs from GMT by a non-minute interval\n";
96 if ((($gmttm[6] + 1) % 7) == $localtm[6]) {
98 } elsif ((($gmttm[6] - 1) % 7) == $localtm[6]) {
100 } elsif ($gmttm[6] != $localtm[6]) {
101 die "local time offset greater than or equal to 24 hours\n";
103 my $offset = $localmin - $gmtmin;
104 my $offhour = $offset / 60;
105 my $offmin = abs($offset % 60);
106 if (abs($offhour) >= 24) {
107 die ("local time offset greater than or equal to 24 hours\n");
110 return sprintf("%s, %2d %s %d %02d:%02d:%02d %s%02d%02d",
111 qw(Sun Mon Tue Wed Thu Fri Sat)[$localtm[6]],
113 qw(Jan Feb Mar Apr May Jun
114 Jul Aug Sep Oct Nov Dec)[$localtm[4]],
119 ($offset >= 0) ?
'+' : '-',
125 my $have_email_valid = eval { require Email
::Valid
; 1 };
128 sub unique_email_list
(@
);
129 sub cleanup_compose_files
();
131 # Constants (essentially)
132 my $compose_filename = ".msg.$$";
134 # Variables we fill in automatically, or via prompting:
135 my (@to,@cc,@initial_cc,@bcclist,@xh,
136 $initial_reply_to,$initial_subject,@files,$from,$compose,$time);
138 # Behavior modification variables
139 my ($chain_reply_to, $quiet, $suppress_from, $no_signed_off_cc,
140 $dry_run) = (1, 0, 0, 0, 0);
144 #$initial_reply_to = ''; #<20050203173208.GA23964@foobar.com>';
146 my $repo = Git
->repository();
148 new Term
::ReadLine
'git-send-email';
151 $term = new FakeTerm
"$@: going non-interactive";
154 my $def_chain = $repo->config_boolean('sendemail.chainreplyto');
155 if ($def_chain and $def_chain eq 'false') {
159 @bcclist = $repo->config('sendemail.bcc');
160 if (!@bcclist or !$bcclist[0]) {
164 # Begin by accumulating all the variables (defined above), that we will end up
165 # needing, first, from the command line:
167 my $rc = GetOptions
("from=s" => \
$from,
168 "in-reply-to=s" => \
$initial_reply_to,
169 "subject=s" => \
$initial_subject,
171 "cc=s" => \
@initial_cc,
172 "bcc=s" => \
@bcclist,
173 "chain-reply-to!" => \
$chain_reply_to,
174 "smtp-server=s" => \
$smtp_server,
175 "compose" => \
$compose,
177 "suppress-from" => \
$suppress_from,
178 "no-signed-off-cc|no-signed-off-by-cc" => \
$no_signed_off_cc,
179 "dry-run" => \
$dry_run,
186 # Verify the user input
188 foreach my $entry (@to) {
189 die "Comma in --to entry: $entry'\n" unless $entry !~ m/,/;
192 foreach my $entry (@initial_cc) {
193 die "Comma in --cc entry: $entry'\n" unless $entry !~ m/,/;
196 foreach my $entry (@bcclist) {
197 die "Comma in --bcclist entry: $entry'\n" unless $entry !~ m/,/;
200 # Now, let's fill any that aren't set in with defaults:
202 my ($author) = $repo->ident_person('author');
203 my ($committer) = $repo->ident_person('committer');
206 my @alias_files = $repo->config('sendemail.aliasesfile');
207 my $aliasfiletype = $repo->config('sendemail.aliasfiletype');
209 # multiline formats can be supported in the future
210 mutt
=> sub { my $fh = shift; while (<$fh>) {
211 if (/^alias\s+(\S+)\s+(.*)$/) {
212 my ($alias, $addr) = ($1, $2);
213 $addr =~ s/#.*$//; # mutt allows # comments
214 # commas delimit multiple addresses
215 $aliases{$alias} = [ split(/\s*,\s*/, $addr) ];
217 mailrc
=> sub { my $fh = shift; while (<$fh>) {
218 if (/^alias\s+(\S+)\s+(.*)$/) {
219 # spaces delimit multiple addresses
220 $aliases{$1} = [ split(/\s+/, $2) ];
222 pine
=> sub { my $fh = shift; while (<$fh>) {
223 if (/^(\S+)\s+(.*)$/) {
224 $aliases{$1} = [ split(/\s*,\s*/, $2) ];
226 gnus
=> sub { my $fh = shift; while (<$fh>) {
227 if (/\(define-mail-alias\s+"(\S+?)"\s+"(\S+?)"\)/) {
228 $aliases{$1} = [ $2 ];
232 if (@alias_files and $aliasfiletype and defined $parse_alias{$aliasfiletype}) {
233 foreach my $file (@alias_files) {
234 open my $fh, '<', $file or die "opening $file: $!\n";
235 $parse_alias{$aliasfiletype}->($fh);
241 if (!defined $from) {
242 $from = $author || $committer;
244 $_ = $term->readline("Who should the emails appear to be from? [$from] ");
245 } while (!defined $_);
248 print "Emails will be sent from: ", $from, "\n";
254 $_ = $term->readline("Who should the emails be sent to? ",
256 } while (!defined $_);
258 push @to, split /,/, $to;
267 @cur = map { $aliases{$_} ? @
{$aliases{$_}} : $_ } @last;
268 } while (join(',',@cur) ne join(',',@last));
272 @to = expand_aliases
(@to);
273 @initial_cc = expand_aliases
(@initial_cc);
274 @bcclist = expand_aliases
(@bcclist);
276 if (!defined $initial_subject && $compose) {
278 $_ = $term->readline("What subject should the emails start with? ",
280 } while (!defined $_);
281 $initial_subject = $_;
285 if (!defined $initial_reply_to && $prompting) {
287 $_= $term->readline("Message-ID to be used as In-Reply-To for the first email? ",
289 } while (!defined $_);
291 $initial_reply_to = $_;
292 $initial_reply_to =~ s/(^\s+|\s+$)//g;
296 $smtp_server = $repo->config('sendemail.smtpserver');
299 foreach (qw( /usr/sbin/sendmail /usr/lib/sendmail )) {
305 $smtp_server ||= 'localhost'; # could be 127.0.0.1, too... *shrug*
309 # Note that this does not need to be secure, but we will make a small
310 # effort to have it be unique
311 open(C
,">",$compose_filename)
312 or die "Failed to open for writing $compose_filename: $!";
313 print C
"From $from # This line is ignored.\n";
314 printf C
"Subject: %s\n\n", $initial_subject;
316 GIT: Please enter your email below.
317 GIT: Lines beginning in "GIT: " will be removed.
318 GIT: Consider including an overall diffstat or table of contents
319 GIT: for the patch you are writing.
324 my $editor = $ENV{EDITOR
};
325 $editor = 'vi' unless defined $editor;
326 system($editor, $compose_filename);
328 open(C2
,">",$compose_filename . ".final")
329 or die "Failed to open $compose_filename.final : " . $!;
331 open(C
,"<",$compose_filename)
332 or die "Failed to open $compose_filename : " . $!;
342 $_ = $term->readline("Send this email? (y|n) ");
343 } while (!defined $_);
345 if (uc substr($_,0,1) ne 'Y') {
346 cleanup_compose_files
();
350 @files = ($compose_filename . ".final");
354 # Now that all the defaults are set, process the rest of the command line
355 # arguments and collect up the files that need to be processed.
359 or die "Failed to opendir $f: $!";
361 push @files, grep { -f
$_ } map { +$f . "/" . $_ }
368 print STDERR
"Skipping $f - not found.\n";
374 print $_,"\n" for (@files);
377 print STDERR
"\nNo patch files specified!\n\n";
381 # Variables we set as part of the loop over files
382 our ($message_id, $cc, %mail, $subject, $reply_to, $references, $message);
384 sub extract_valid_address
{
386 my $local_part_regexp = '[^<>"\s@]+';
387 my $domain_regexp = '[^.<>"\s@]+(?:\.[^.<>"\s@]+)+';
389 # check for a local address:
390 return $address if ($address =~ /^($local_part_regexp)$/);
392 if ($have_email_valid) {
393 return scalar Email
::Valid
->address($address);
395 # less robust/correct than the monster regexp in Email::Valid,
396 # but still does a 99% job, and one less dependency
397 $address =~ /($local_part_regexp\@$domain_regexp)/;
402 # Usually don't need to change anything below here.
404 # we make a "fake" message id by taking the current number
405 # of seconds since the beginning of Unix time and tacking on
406 # a random number to the end, in case we are called quicker than
407 # 1 second since the last time we were called.
409 # We'll setup a template for the message id, using the "from" address:
410 my $message_id_from = extract_valid_address
($from);
411 my $message_id_template = "<%s-git-send-email-$message_id_from>";
416 my $pseudo_rand = int (rand(4200));
417 $message_id = sprintf $message_id_template, "$date$pseudo_rand";
418 #print "new message id = $message_id\n"; # Was useful for debugging
424 $time = time - scalar $#files;
426 sub unquote_rfc2047
{
428 if (s/=\?utf-8\?q\?(.*)\?=/$1/g) {
430 s/=([0-9A-F]{2})/chr(hex($1))/eg;
437 my @recipients = unique_email_list
(@to);
438 my $to = join (",\n\t", @recipients);
439 @recipients = unique_email_list
(@recipients,@cc,@bcclist);
440 my $date = format_2822_time
($time++);
441 my $gitversion = '@@GIT_VERSION@@';
442 if ($gitversion =~ m/..GIT_VERSION../) {
443 $gitversion = Git
::version
();
446 my ($author_name) = ($from =~ /^(.*?)\s+</);
447 if ($author_name && $author_name =~ /\./ && $author_name !~ /^".*"$/) {
448 my ($name, $addr) = ($from =~ /^(.*?)(\s+<.*)/);
449 $from = "\"$name\"$addr";
451 my $header = "From: $from
456 Message-Id: $message_id
457 X-Mailer: git-send-email $gitversion
461 $header .= "In-Reply-To: $reply_to\n";
462 $header .= "References: $references\n";
465 $header .= join("\n", @xh) . "\n";
469 # We don't want to send the email.
470 } elsif ($smtp_server =~ m
#^/#) {
471 my $pid = open my $sm, '|-';
472 defined $pid or die $!;
474 exec($smtp_server,'-i',
475 map { extract_valid_address
($_) }
476 @recipients) or die $!;
478 print $sm "$header\n$message";
482 $smtp ||= Net
::SMTP
->new( $smtp_server );
483 $smtp->mail( $from ) or die $smtp->message;
484 $smtp->to( @recipients ) or die $smtp->message;
485 $smtp->data or die $smtp->message;
486 $smtp->datasend("$header\n$message") or die $smtp->message;
487 $smtp->dataend() or die $smtp->message;
488 $smtp->ok or die "Failed to send $subject\n".$smtp->message;
491 printf (($dry_run ?
"Dry-" : "")."Sent %s\n", $subject);
493 print (($dry_run ?
"Dry-" : "")."OK. Log says:\nDate: $date\n");
495 print "Server: $smtp_server\n";
497 print "Sendmail: $smtp_server\n";
499 print "From: $from\nSubject: $subject\nCc: $cc\nTo: $to\n\n";
501 print "Result: ", $smtp->code, ' ',
502 ($smtp->message =~ /\n([^\n]+\n)$/s), "\n";
504 print "Result: OK\n";
509 $reply_to = $initial_reply_to;
510 $references = $initial_reply_to || '';
512 $subject = $initial_subject;
514 foreach my $t (@files) {
515 open(F
,"<",$t) or die "can't open file $t";
517 my $author_not_sender = undef;
520 my $input_format = undef;
526 $input_format = 'mbox';
530 if (!defined $input_format && /^[-A-Za-z]+:\s/) {
531 $input_format = 'mbox';
534 if (defined $input_format && $input_format eq 'mbox') {
535 if (/^Subject:\s+(.*)$/) {
538 } elsif (/^(Cc|From):\s+(.*)$/) {
540 next if ($suppress_from);
542 elsif ($1 eq 'From') {
543 $author_not_sender = $2;
545 printf("(mbox) Adding cc: %s from line '%s'\n",
546 $2, $_) unless $quiet;
549 elsif (!/^Date:\s/ && /^[-A-Za-z]+:\s+\S/) {
555 # "send lots of email" format,
558 # So let's support that, too.
559 $input_format = 'lots';
561 printf("(non-mbox) Adding cc: %s from line '%s'\n",
562 $_, $_) unless $quiet;
566 } elsif (!defined $subject) {
571 # A whitespace line will terminate the headers
577 if (/^(Signed-off-by|Cc): (.*)$/i && !$no_signed_off_cc) {
581 printf("(sob) Adding cc: %s from line '%s'\n",
582 $c, $_) unless $quiet;
587 if (defined $author_not_sender) {
588 $author_not_sender = unquote_rfc2047
($author_not_sender);
589 $message = "From: $author_not_sender\n\n$message";
592 $cc = join(", ", unique_email_list
(@cc));
596 # set up for the next message
597 if ($chain_reply_to || !defined $reply_to || length($reply_to) == 0) {
598 $reply_to = $message_id;
599 if (length $references > 0) {
600 $references .= "\n $message_id";
602 $references = "$message_id";
609 cleanup_compose_files
();
612 sub cleanup_compose_files
() {
613 unlink($compose_filename, $compose_filename . ".final");
617 $smtp->quit if $smtp;
619 sub unique_email_list
(@
) {
623 foreach my $entry (@_) {
624 if (my $clean = extract_valid_address
($entry)) {
626 next if $seen{$clean}++;
627 push @emails, $entry;
629 print STDERR
"W: unable to extract a valid address",