send-email: allow sendmail binary to be used instead of SMTP
[git/git.git] / git-send-email.perl
CommitLineData
83b24437 1#!/usr/bin/perl -w
83b24437 2#
f3d9f354
RA
3# Copyright 2002,2005 Greg Kroah-Hartman <greg@kroah.com>
4# Copyright 2005 Ryan Anderson <ryan@michonline.com>
83b24437
RA
5#
6# GPL v2 (See COPYING)
5825e5b2 7#
83b24437
RA
8# Ported to support git "mbox" format files by Ryan Anderson <ryan@michonline.com>
9#
f3d9f354 10# Sends a collection of emails to the given email addresses, disturbingly fast.
5825e5b2 11#
f3d9f354
RA
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:
5825e5b2 15# first line of the message is who to CC,
f3d9f354 16# and second line is the subject of the message.
5825e5b2 17#
83b24437
RA
18
19use strict;
20use warnings;
21use Term::ReadLine;
83b24437
RA
22use Getopt::Long;
23use Data::Dumper;
4bc87a28 24use Net::SMTP;
83b24437 25
4bc87a28
EW
26# most mail servers generate the Date: header, but not all...
27$ENV{LC_ALL} = 'C';
28use POSIX qw/strftime/;
29
567ffeb7 30my $have_email_valid = eval { require Email::Valid; 1 };
4bc87a28
EW
31my $smtp;
32
e205735d 33sub unique_email_list(@);
1f038a0c
RA
34sub cleanup_compose_files();
35
36# Constants (essentially)
37my $compose_filename = ".msg.$$";
e205735d 38
83b24437 39# Variables we fill in automatically, or via prompting:
a5370b16 40my (@to,@cc,@initial_cc,$initial_reply_to,$initial_subject,@files,$from,$compose,$time);
83b24437 41
78488b2c 42# Behavior modification variables
aca7ad76
EW
43my ($chain_reply_to, $quiet, $suppress_from, $no_signed_off_cc) = (1, 0, 0, 0);
44my $smtp_server;
78488b2c 45
9133261f 46# Example reply to:
83b24437 47#$initial_reply_to = ''; #<20050203173208.GA23964@foobar.com>';
83b24437
RA
48
49my $term = new Term::ReadLine 'git-send-email';
50
51# Begin by accumulating all the variables (defined above), that we will end up
52# needing, first, from the command line:
53
54my $rc = GetOptions("from=s" => \$from,
55 "in-reply-to=s" => \$initial_reply_to,
56 "subject=s" => \$initial_subject,
57 "to=s" => \@to,
da140f8b 58 "cc=s" => \@initial_cc,
78488b2c 59 "chain-reply-to!" => \$chain_reply_to,
3342d850 60 "smtp-server=s" => \$smtp_server,
1f038a0c 61 "compose" => \$compose,
30d08b34 62 "quiet" => \$quiet,
a985d595 63 "suppress-from" => \$suppress_from,
8e69b31e 64 "no-signed-off-cc|no-signed-off-by-cc" => \$no_signed_off_cc,
83b24437
RA
65 );
66
67# Now, let's fill any that aren't set in with defaults:
68
e415907d
JH
69sub gitvar {
70 my ($var) = @_;
71 my $fh;
72 my $pid = open($fh, '-|');
73 die "$!" unless defined $pid;
74 if (!$pid) {
75 exec('git-var', $var) or die "$!";
76 }
77 my ($val) = <$fh>;
78 close $fh or die "$!";
79 chomp($val);
80 return $val;
81}
83b24437 82
e415907d
JH
83sub gitvar_ident {
84 my ($name) = @_;
85 my $val = gitvar($name);
86 my @field = split(/\s+/, $val);
87 return join(' ', @field[0...(@field-3)]);
83b24437 88}
e415907d
JH
89
90my ($author) = gitvar_ident('GIT_AUTHOR_IDENT');
91my ($committer) = gitvar_ident('GIT_COMMITTER_IDENT');
83b24437 92
994d6c66
EW
93my %aliases;
94chomp(my @alias_files = `git-repo-config --get-all sendemail.aliasesfile`);
95chomp(my $aliasfiletype = `git-repo-config sendemail.aliasfiletype`);
96my %parse_alias = (
97 # multiline formats can be supported in the future
98 mutt => sub { my $fh = shift; while (<$fh>) {
99 if (/^alias\s+(\S+)\s+(.*)$/) {
100 my ($alias, $addr) = ($1, $2);
101 $addr =~ s/#.*$//; # mutt allows # comments
102 # commas delimit multiple addresses
103 $aliases{$alias} = [ split(/\s*,\s*/, $addr) ];
104 }}},
105 mailrc => sub { my $fh = shift; while (<$fh>) {
106 if (/^alias\s+(\S+)\s+(.*)$/) {
107 # spaces delimit multiple addresses
108 $aliases{$1} = [ split(/\s+/, $2) ];
109 }}},
110 pine => sub { my $fh = shift; while (<$fh>) {
111 if (/^(\S+)\s+(.*)$/) {
112 $aliases{$1} = [ split(/\s*,\s*/, $2) ];
113 }}},
114 gnus => sub { my $fh = shift; while (<$fh>) {
115 if (/\(define-mail-alias\s+"(\S+?)"\s+"(\S+?)"\)/) {
116 $aliases{$1} = [ $2 ];
117 }}}
118);
119
120if (@alias_files && defined $parse_alias{$aliasfiletype}) {
121 foreach my $file (@alias_files) {
122 open my $fh, '<', $file or die "opening $file: $!\n";
123 $parse_alias{$aliasfiletype}->($fh);
124 close $fh;
125 }
126}
127
1f038a0c 128my $prompting = 0;
83b24437
RA
129if (!defined $from) {
130 $from = $author || $committer;
8037d1a3
RA
131 do {
132 $_ = $term->readline("Who should the emails appear to be from? ",
133 $from);
ca9a7d65 134 } while (!defined $_);
8037d1a3 135
83b24437
RA
136 $from = $_;
137 print "Emails will be sent from: ", $from, "\n";
1f038a0c 138 $prompting++;
83b24437
RA
139}
140
141if (!@to) {
8037d1a3 142 do {
5825e5b2 143 $_ = $term->readline("Who should the emails be sent to? ",
8037d1a3
RA
144 "");
145 } while (!defined $_);
83b24437
RA
146 my $to = $_;
147 push @to, split /,/, $to;
1f038a0c 148 $prompting++;
83b24437
RA
149}
150
994d6c66
EW
151sub expand_aliases {
152 my @cur = @_;
153 my @last;
154 do {
155 @last = @cur;
156 @cur = map { $aliases{$_} ? @{$aliases{$_}} : $_ } @last;
157 } while (join(',',@cur) ne join(',',@last));
158 return @cur;
159}
160
161@to = expand_aliases(@to);
162@initial_cc = expand_aliases(@initial_cc);
163
1f038a0c 164if (!defined $initial_subject && $compose) {
8037d1a3 165 do {
5825e5b2 166 $_ = $term->readline("What subject should the emails start with? ",
8037d1a3
RA
167 $initial_subject);
168 } while (!defined $_);
83b24437 169 $initial_subject = $_;
1f038a0c 170 $prompting++;
83b24437
RA
171}
172
1f038a0c 173if (!defined $initial_reply_to && $prompting) {
8037d1a3 174 do {
1f038a0c 175 $_= $term->readline("Message-ID to be used as In-Reply-To for the first email? ",
8037d1a3
RA
176 $initial_reply_to);
177 } while (!defined $_);
178
83b24437 179 $initial_reply_to = $_;
78488b2c 180 $initial_reply_to =~ s/(^\s+|\s+$)//g;
83b24437
RA
181}
182
aca7ad76
EW
183if (!$smtp_server) {
184 foreach (qw( /usr/sbin/sendmail /usr/lib/sendmail )) {
185 if (-x $_) {
186 $smtp_server = $_;
187 last;
188 }
189 }
190 $smtp_server ||= 'localhost'; # could be 127.0.0.1, too... *shrug*
3342d850
RA
191}
192
1f038a0c
RA
193if ($compose) {
194 # Note that this does not need to be secure, but we will make a small
195 # effort to have it be unique
196 open(C,">",$compose_filename)
197 or die "Failed to open for writing $compose_filename: $!";
d366c703 198 print C "From $from # This line is ignored.\n";
1f038a0c
RA
199 printf C "Subject: %s\n\n", $initial_subject;
200 printf C <<EOT;
201GIT: Please enter your email below.
202GIT: Lines beginning in "GIT: " will be removed.
203GIT: Consider including an overall diffstat or table of contents
204GIT: for the patch you are writing.
205
206EOT
207 close(C);
208
209 my $editor = $ENV{EDITOR};
210 $editor = 'vi' unless defined $editor;
211 system($editor, $compose_filename);
212
213 open(C2,">",$compose_filename . ".final")
214 or die "Failed to open $compose_filename.final : " . $!;
215
216 open(C,"<",$compose_filename)
217 or die "Failed to open $compose_filename : " . $!;
218
219 while(<C>) {
220 next if m/^GIT: /;
221 print C2 $_;
222 }
223 close(C);
224 close(C2);
225
226 do {
227 $_ = $term->readline("Send this email? (y|n) ");
228 } while (!defined $_);
229
230 if (uc substr($_,0,1) ne 'Y') {
231 cleanup_compose_files();
232 exit(0);
233 }
234
235 @files = ($compose_filename . ".final");
236}
237
238
83b24437
RA
239# Now that all the defaults are set, process the rest of the command line
240# arguments and collect up the files that need to be processed.
241for my $f (@ARGV) {
242 if (-d $f) {
243 opendir(DH,$f)
244 or die "Failed to opendir $f: $!";
245
5825e5b2 246 push @files, grep { -f $_ } map { +$f . "/" . $_ }
8037d1a3
RA
247 sort readdir(DH);
248
83b24437
RA
249 } elsif (-f $f) {
250 push @files, $f;
251
252 } else {
253 print STDERR "Skipping $f - not found.\n";
254 }
255}
256
257if (@files) {
2718435b
RA
258 unless ($quiet) {
259 print $_,"\n" for (@files);
260 }
83b24437
RA
261} else {
262 print <<EOT;
215a7ad1 263git-send-email [options] <file | directory> [... file | directory ]
83b24437
RA
264Options:
265 --from Specify the "From:" line of the email to be sent.
1f038a0c 266
5825e5b2 267 --to Specify the primary "To:" line of the email.
1f038a0c 268
da140f8b
RA
269 --cc Specify an initial "Cc:" list for the entire series
270 of emails.
271
1f038a0c
RA
272 --compose Use \$EDITOR to edit an introductory message for the
273 patch series.
274
83b24437 275 --subject Specify the initial "Subject:" line.
1f038a0c
RA
276 Only necessary if --compose is also set. If --compose
277 is not set, this will be prompted for.
278
83b24437 279 --in-reply-to Specify the first "In-Reply-To:" header line.
1f038a0c
RA
280 Only used if --compose is also set. If --compose is not
281 set, this will be prompted for.
282
e205735d 283 --chain-reply-to If set, the replies will all be to the previous
5825e5b2
JH
284 email sent, rather than to the first email sent.
285 Defaults to on.
1f038a0c 286
a985d595
RA
287 --no-signed-off-cc Suppress the automatic addition of email addresses
288 that appear in a Signed-off-by: line, to the cc: list.
289 Note: Using this option is not recommended.
290
3342d850
RA
291 --smtp-server If set, specifies the outgoing SMTP server to use.
292 Defaults to localhost.
83b24437 293
a985d595
RA
294 --suppress-from Supress sending emails to yourself if your address
295 appears in a From: line.
296
2718435b
RA
297 --quiet Make git-send-email less verbose. One line per email should be
298 all that is output.
299
83b24437
RA
300Error: Please specify a file or a directory on the command line.
301EOT
302 exit(1);
303}
304
305# Variables we set as part of the loop over files
306our ($message_id, $cc, %mail, $subject, $reply_to, $message);
307
567ffeb7
EW
308sub extract_valid_address {
309 my $address = shift;
310 if ($have_email_valid) {
311 return Email::Valid->address($address);
312 } else {
313 # less robust/correct than the monster regexp in Email::Valid,
314 # but still does a 99% job, and one less dependency
315 return ($address =~ /([^\"<>\s]+@[^<>\s]+)/);
316 }
317}
83b24437
RA
318
319# Usually don't need to change anything below here.
320
321# we make a "fake" message id by taking the current number
322# of seconds since the beginning of Unix time and tacking on
323# a random number to the end, in case we are called quicker than
324# 1 second since the last time we were called.
8037d1a3
RA
325
326# We'll setup a template for the message id, using the "from" address:
567ffeb7 327my $message_id_from = extract_valid_address($from);
e205735d 328my $message_id_template = "<%s-git-send-email-$message_id_from>";
8037d1a3 329
83b24437
RA
330sub make_message_id
331{
72095d5c 332 my $date = time;
83b24437 333 my $pseudo_rand = int (rand(4200));
8037d1a3
RA
334 $message_id = sprintf $message_id_template, "$date$pseudo_rand";
335 #print "new message id = $message_id\n"; # Was useful for debugging
83b24437
RA
336}
337
338
339
340$cc = "";
a5370b16 341$time = time - scalar $#files;
83b24437
RA
342
343sub send_message
344{
4bc87a28
EW
345 my @recipients = unique_email_list(@to);
346 my $to = join (",\n\t", @recipients);
347 @recipients = unique_email_list(@recipients,@cc);
a5370b16 348 my $date = strftime('%a, %d %b %Y %H:%M:%S %z', localtime($time++));
e923effb
ML
349 my $gitversion = '@@GIT_VERSION@@';
350 if ($gitversion =~ m/..GIT_VERSION../) {
351 $gitversion = `git --version`;
352 chomp $gitversion;
353 # keep only what's after the last space
354 $gitversion =~ s/^.* //;
355 }
4bc87a28
EW
356
357 my $header = "From: $from
358To: $to
359Cc: $cc
360Subject: $subject
361Reply-To: $from
362Date: $date
363Message-Id: $message_id
e923effb 364X-Mailer: git-send-email $gitversion
4bc87a28
EW
365";
366 $header .= "In-Reply-To: $reply_to\n" if $reply_to;
367
aca7ad76
EW
368 if ($smtp_server =~ m#^/#) {
369 my $pid = open my $sm, '|-';
370 defined $pid or die $!;
371 if (!$pid) {
372 exec($smtp_server,'-i',@recipients) or die $!;
373 }
374 print $sm "$header\n$message";
375 close $sm or die $?;
376 } else {
377 $smtp ||= Net::SMTP->new( $smtp_server );
378 $smtp->mail( $from ) or die $smtp->message;
379 $smtp->to( @recipients ) or die $smtp->message;
380 $smtp->data or die $smtp->message;
381 $smtp->datasend("$header\n$message") or die $smtp->message;
382 $smtp->dataend() or die $smtp->message;
383 $smtp->ok or die "Failed to send $subject\n".$smtp->message;
384 }
2718435b
RA
385 if ($quiet) {
386 printf "Sent %s\n", $subject;
387 } else {
aca7ad76
EW
388 print "OK. Log says:\nDate: $date\n";
389 if ($smtp) {
390 print "Server: $smtp_server\n";
391 } else {
392 print "Sendmail: $smtp_server\n";
393 }
394 print "From: $from\nSubject: $subject\nCc: $cc\nTo: $to\n\n";
395 if ($smtp) {
396 print "Result: ", $smtp->code, ' ',
397 ($smtp->message =~ /\n([^\n]+\n)$/s), "\n";
398 } else {
399 print "Result: OK\n";
400 }
30d08b34 401 }
83b24437
RA
402}
403
83b24437
RA
404$reply_to = $initial_reply_to;
405make_message_id();
406$subject = $initial_subject;
407
408foreach my $t (@files) {
83b24437
RA
409 open(F,"<",$t) or die "can't open file $t";
410
8a8e6235 411 my $author_not_sender = undef;
da140f8b 412 @cc = @initial_cc;
83b24437
RA
413 my $found_mbox = 0;
414 my $header_done = 0;
415 $message = "";
416 while(<F>) {
417 if (!$header_done) {
418 $found_mbox = 1, next if (/^From /);
419 chomp;
420
421 if ($found_mbox) {
422 if (/^Subject:\s+(.*)$/) {
423 $subject = $1;
424
425 } elsif (/^(Cc|From):\s+(.*)$/) {
8a8e6235
JH
426 if ($2 eq $from) {
427 next if ($suppress_from);
428 }
429 else {
430 $author_not_sender = $2;
431 }
83b24437 432 printf("(mbox) Adding cc: %s from line '%s'\n",
2718435b 433 $2, $_) unless $quiet;
83b24437
RA
434 push @cc, $2;
435 }
436
437 } else {
438 # In the traditional
439 # "send lots of email" format,
440 # line 1 = cc
441 # line 2 = subject
442 # So let's support that, too.
443 if (@cc == 0) {
444 printf("(non-mbox) Adding cc: %s from line '%s'\n",
2718435b 445 $_, $_) unless $quiet;
83b24437
RA
446
447 push @cc, $_;
448
449 } elsif (!defined $subject) {
450 $subject = $_;
451 }
452 }
5825e5b2 453
83b24437
RA
454 # A whitespace line will terminate the headers
455 if (m/^\s*$/) {
456 $header_done = 1;
457 }
458 } else {
459 $message .= $_;
a985d595 460 if (/^Signed-off-by: (.*)$/i && !$no_signed_off_cc) {
83b24437
RA
461 my $c = $1;
462 chomp $c;
463 push @cc, $c;
464 printf("(sob) Adding cc: %s from line '%s'\n",
2718435b 465 $c, $_) unless $quiet;
83b24437
RA
466 }
467 }
468 }
469 close F;
8a8e6235
JH
470 if (defined $author_not_sender) {
471 $message = "From: $author_not_sender\n\n$message";
472 }
83b24437 473
e205735d 474 $cc = join(", ", unique_email_list(@cc));
83b24437
RA
475
476 send_message();
477
478 # set up for the next message
78488b2c
RA
479 if ($chain_reply_to || length($reply_to) == 0) {
480 $reply_to = $message_id;
481 }
83b24437 482 make_message_id();
83b24437 483}
e205735d 484
1f038a0c
RA
485if ($compose) {
486 cleanup_compose_files();
487}
488
489sub cleanup_compose_files() {
490 unlink($compose_filename, $compose_filename . ".final");
491
492}
493
4bc87a28 494$smtp->quit if $smtp;
e205735d
RA
495
496sub unique_email_list(@) {
497 my %seen;
498 my @emails;
499
500 foreach my $entry (@_) {
567ffeb7 501 my $clean = extract_valid_address($entry);
e205735d
RA
502 next if $seen{$clean}++;
503 push @emails, $entry;
504 }
505 return @emails;
506}