]> git.ipfire.org Git - thirdparty/git.git/blame - git-send-email.perl
send-email: Change from Mail::Sendmail to Net::SMTP
[thirdparty/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
RA
25use Email::Valid;
26
4bc87a28
EW
27# most mail servers generate the Date: header, but not all...
28$ENV{LC_ALL} = 'C';
29use POSIX qw/strftime/;
30
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:
da140f8b 40my (@to,@cc,@initial_cc,$initial_reply_to,$initial_subject,@files,$from,$compose);
83b24437 41
78488b2c 42# Behavior modification variables
a985d595 43my ($chain_reply_to, $smtp_server, $quiet, $suppress_from, $no_signed_off_cc) = (1, "localhost", 0, 0, 0);
78488b2c 44
9133261f 45# Example reply to:
83b24437 46#$initial_reply_to = ''; #<20050203173208.GA23964@foobar.com>';
83b24437
RA
47
48my $term = new Term::ReadLine 'git-send-email';
49
50# Begin by accumulating all the variables (defined above), that we will end up
51# needing, first, from the command line:
52
53my $rc = GetOptions("from=s" => \$from,
54 "in-reply-to=s" => \$initial_reply_to,
55 "subject=s" => \$initial_subject,
56 "to=s" => \@to,
da140f8b 57 "cc=s" => \@initial_cc,
78488b2c 58 "chain-reply-to!" => \$chain_reply_to,
3342d850 59 "smtp-server=s" => \$smtp_server,
1f038a0c 60 "compose" => \$compose,
30d08b34 61 "quiet" => \$quiet,
a985d595 62 "suppress-from" => \$suppress_from,
8e69b31e 63 "no-signed-off-cc|no-signed-off-by-cc" => \$no_signed_off_cc,
83b24437
RA
64 );
65
66# Now, let's fill any that aren't set in with defaults:
67
e415907d
JH
68sub gitvar {
69 my ($var) = @_;
70 my $fh;
71 my $pid = open($fh, '-|');
72 die "$!" unless defined $pid;
73 if (!$pid) {
74 exec('git-var', $var) or die "$!";
75 }
76 my ($val) = <$fh>;
77 close $fh or die "$!";
78 chomp($val);
79 return $val;
80}
83b24437 81
e415907d
JH
82sub gitvar_ident {
83 my ($name) = @_;
84 my $val = gitvar($name);
85 my @field = split(/\s+/, $val);
86 return join(' ', @field[0...(@field-3)]);
83b24437 87}
e415907d
JH
88
89my ($author) = gitvar_ident('GIT_AUTHOR_IDENT');
90my ($committer) = gitvar_ident('GIT_COMMITTER_IDENT');
83b24437 91
1f038a0c 92my $prompting = 0;
83b24437
RA
93if (!defined $from) {
94 $from = $author || $committer;
8037d1a3
RA
95 do {
96 $_ = $term->readline("Who should the emails appear to be from? ",
97 $from);
ca9a7d65 98 } while (!defined $_);
8037d1a3 99
83b24437
RA
100 $from = $_;
101 print "Emails will be sent from: ", $from, "\n";
1f038a0c 102 $prompting++;
83b24437
RA
103}
104
105if (!@to) {
8037d1a3 106 do {
5825e5b2 107 $_ = $term->readline("Who should the emails be sent to? ",
8037d1a3
RA
108 "");
109 } while (!defined $_);
83b24437
RA
110 my $to = $_;
111 push @to, split /,/, $to;
1f038a0c 112 $prompting++;
83b24437
RA
113}
114
1f038a0c 115if (!defined $initial_subject && $compose) {
8037d1a3 116 do {
5825e5b2 117 $_ = $term->readline("What subject should the emails start with? ",
8037d1a3
RA
118 $initial_subject);
119 } while (!defined $_);
83b24437 120 $initial_subject = $_;
1f038a0c 121 $prompting++;
83b24437
RA
122}
123
1f038a0c 124if (!defined $initial_reply_to && $prompting) {
8037d1a3 125 do {
1f038a0c 126 $_= $term->readline("Message-ID to be used as In-Reply-To for the first email? ",
8037d1a3
RA
127 $initial_reply_to);
128 } while (!defined $_);
129
83b24437 130 $initial_reply_to = $_;
78488b2c 131 $initial_reply_to =~ s/(^\s+|\s+$)//g;
83b24437
RA
132}
133
3342d850
RA
134if (!defined $smtp_server) {
135 $smtp_server = "localhost";
136}
137
1f038a0c
RA
138if ($compose) {
139 # Note that this does not need to be secure, but we will make a small
140 # effort to have it be unique
141 open(C,">",$compose_filename)
142 or die "Failed to open for writing $compose_filename: $!";
d366c703 143 print C "From $from # This line is ignored.\n";
1f038a0c
RA
144 printf C "Subject: %s\n\n", $initial_subject;
145 printf C <<EOT;
146GIT: Please enter your email below.
147GIT: Lines beginning in "GIT: " will be removed.
148GIT: Consider including an overall diffstat or table of contents
149GIT: for the patch you are writing.
150
151EOT
152 close(C);
153
154 my $editor = $ENV{EDITOR};
155 $editor = 'vi' unless defined $editor;
156 system($editor, $compose_filename);
157
158 open(C2,">",$compose_filename . ".final")
159 or die "Failed to open $compose_filename.final : " . $!;
160
161 open(C,"<",$compose_filename)
162 or die "Failed to open $compose_filename : " . $!;
163
164 while(<C>) {
165 next if m/^GIT: /;
166 print C2 $_;
167 }
168 close(C);
169 close(C2);
170
171 do {
172 $_ = $term->readline("Send this email? (y|n) ");
173 } while (!defined $_);
174
175 if (uc substr($_,0,1) ne 'Y') {
176 cleanup_compose_files();
177 exit(0);
178 }
179
180 @files = ($compose_filename . ".final");
181}
182
183
83b24437
RA
184# Now that all the defaults are set, process the rest of the command line
185# arguments and collect up the files that need to be processed.
186for my $f (@ARGV) {
187 if (-d $f) {
188 opendir(DH,$f)
189 or die "Failed to opendir $f: $!";
190
5825e5b2 191 push @files, grep { -f $_ } map { +$f . "/" . $_ }
8037d1a3
RA
192 sort readdir(DH);
193
83b24437
RA
194 } elsif (-f $f) {
195 push @files, $f;
196
197 } else {
198 print STDERR "Skipping $f - not found.\n";
199 }
200}
201
202if (@files) {
2718435b
RA
203 unless ($quiet) {
204 print $_,"\n" for (@files);
205 }
83b24437
RA
206} else {
207 print <<EOT;
215a7ad1 208git-send-email [options] <file | directory> [... file | directory ]
83b24437
RA
209Options:
210 --from Specify the "From:" line of the email to be sent.
1f038a0c 211
5825e5b2 212 --to Specify the primary "To:" line of the email.
1f038a0c 213
da140f8b
RA
214 --cc Specify an initial "Cc:" list for the entire series
215 of emails.
216
1f038a0c
RA
217 --compose Use \$EDITOR to edit an introductory message for the
218 patch series.
219
83b24437 220 --subject Specify the initial "Subject:" line.
1f038a0c
RA
221 Only necessary if --compose is also set. If --compose
222 is not set, this will be prompted for.
223
83b24437 224 --in-reply-to Specify the first "In-Reply-To:" header line.
1f038a0c
RA
225 Only used if --compose is also set. If --compose is not
226 set, this will be prompted for.
227
e205735d 228 --chain-reply-to If set, the replies will all be to the previous
5825e5b2
JH
229 email sent, rather than to the first email sent.
230 Defaults to on.
1f038a0c 231
a985d595
RA
232 --no-signed-off-cc Suppress the automatic addition of email addresses
233 that appear in a Signed-off-by: line, to the cc: list.
234 Note: Using this option is not recommended.
235
3342d850
RA
236 --smtp-server If set, specifies the outgoing SMTP server to use.
237 Defaults to localhost.
83b24437 238
a985d595
RA
239 --suppress-from Supress sending emails to yourself if your address
240 appears in a From: line.
241
2718435b
RA
242 --quiet Make git-send-email less verbose. One line per email should be
243 all that is output.
244
83b24437
RA
245Error: Please specify a file or a directory on the command line.
246EOT
247 exit(1);
248}
249
250# Variables we set as part of the loop over files
251our ($message_id, $cc, %mail, $subject, $reply_to, $message);
252
253
254# Usually don't need to change anything below here.
255
256# we make a "fake" message id by taking the current number
257# of seconds since the beginning of Unix time and tacking on
258# a random number to the end, in case we are called quicker than
259# 1 second since the last time we were called.
8037d1a3
RA
260
261# We'll setup a template for the message id, using the "from" address:
262my $message_id_from = Email::Valid->address($from);
e205735d 263my $message_id_template = "<%s-git-send-email-$message_id_from>";
8037d1a3 264
83b24437
RA
265sub make_message_id
266{
72095d5c 267 my $date = time;
83b24437 268 my $pseudo_rand = int (rand(4200));
8037d1a3
RA
269 $message_id = sprintf $message_id_template, "$date$pseudo_rand";
270 #print "new message id = $message_id\n"; # Was useful for debugging
83b24437
RA
271}
272
273
274
275$cc = "";
276
277sub send_message
278{
4bc87a28
EW
279 my @recipients = unique_email_list(@to);
280 my $to = join (",\n\t", @recipients);
281 @recipients = unique_email_list(@recipients,@cc);
282 my $date = strftime('%a, %d %b %Y %H:%M:%S %z', localtime(time));
283
284 my $header = "From: $from
285To: $to
286Cc: $cc
287Subject: $subject
288Reply-To: $from
289Date: $date
290Message-Id: $message_id
291X-Mailer: git-send-email @@GIT_VERSION@@
292";
293 $header .= "In-Reply-To: $reply_to\n" if $reply_to;
294
295 $smtp ||= Net::SMTP->new( $smtp_server );
296 $smtp->mail( $from ) or die $smtp->message;
297 $smtp->to( @recipients ) or die $smtp->message;
298 $smtp->data or die $smtp->message;
299 $smtp->datasend("$header\n$message") or die $smtp->message;
300 $smtp->dataend() or die $smtp->message;
301 $smtp->ok or die "Failed to send $subject\n".$smtp->message;
83b24437 302
2718435b
RA
303 if ($quiet) {
304 printf "Sent %s\n", $subject;
305 } else {
4bc87a28
EW
306 print "OK. Log says:
307Date: $date
308Server: $smtp_server Port: 25
309From: $from
310Subject: $subject
311Cc: $cc
312To: $to
313
314Result: ", $smtp->code, ' ', ($smtp->message =~ /\n([^\n]+\n)$/s), "\n";
30d08b34 315 }
83b24437
RA
316}
317
83b24437
RA
318$reply_to = $initial_reply_to;
319make_message_id();
320$subject = $initial_subject;
321
322foreach my $t (@files) {
83b24437
RA
323 open(F,"<",$t) or die "can't open file $t";
324
8a8e6235 325 my $author_not_sender = undef;
da140f8b 326 @cc = @initial_cc;
83b24437
RA
327 my $found_mbox = 0;
328 my $header_done = 0;
329 $message = "";
330 while(<F>) {
331 if (!$header_done) {
332 $found_mbox = 1, next if (/^From /);
333 chomp;
334
335 if ($found_mbox) {
336 if (/^Subject:\s+(.*)$/) {
337 $subject = $1;
338
339 } elsif (/^(Cc|From):\s+(.*)$/) {
8a8e6235
JH
340 if ($2 eq $from) {
341 next if ($suppress_from);
342 }
343 else {
344 $author_not_sender = $2;
345 }
83b24437 346 printf("(mbox) Adding cc: %s from line '%s'\n",
2718435b 347 $2, $_) unless $quiet;
83b24437
RA
348 push @cc, $2;
349 }
350
351 } else {
352 # In the traditional
353 # "send lots of email" format,
354 # line 1 = cc
355 # line 2 = subject
356 # So let's support that, too.
357 if (@cc == 0) {
358 printf("(non-mbox) Adding cc: %s from line '%s'\n",
2718435b 359 $_, $_) unless $quiet;
83b24437
RA
360
361 push @cc, $_;
362
363 } elsif (!defined $subject) {
364 $subject = $_;
365 }
366 }
5825e5b2 367
83b24437
RA
368 # A whitespace line will terminate the headers
369 if (m/^\s*$/) {
370 $header_done = 1;
371 }
372 } else {
373 $message .= $_;
a985d595 374 if (/^Signed-off-by: (.*)$/i && !$no_signed_off_cc) {
83b24437
RA
375 my $c = $1;
376 chomp $c;
377 push @cc, $c;
378 printf("(sob) Adding cc: %s from line '%s'\n",
2718435b 379 $c, $_) unless $quiet;
83b24437
RA
380 }
381 }
382 }
383 close F;
8a8e6235
JH
384 if (defined $author_not_sender) {
385 $message = "From: $author_not_sender\n\n$message";
386 }
83b24437 387
e205735d 388 $cc = join(", ", unique_email_list(@cc));
83b24437
RA
389
390 send_message();
391
392 # set up for the next message
78488b2c
RA
393 if ($chain_reply_to || length($reply_to) == 0) {
394 $reply_to = $message_id;
395 }
83b24437 396 make_message_id();
83b24437 397}
e205735d 398
1f038a0c
RA
399if ($compose) {
400 cleanup_compose_files();
401}
402
403sub cleanup_compose_files() {
404 unlink($compose_filename, $compose_filename . ".final");
405
406}
407
4bc87a28 408$smtp->quit if $smtp;
e205735d
RA
409
410sub unique_email_list(@) {
411 my %seen;
412 my @emails;
413
414 foreach my $entry (@_) {
415 my $clean = Email::Valid->address($entry);
416 next if $seen{$clean}++;
417 push @emails, $entry;
418 }
419 return @emails;
420}