blob: 746c525079317491e35827f331922e17e5fbd089 [file] [log] [blame]
Ryan Anderson83b24432005-07-31 04:17:25 -04001#!/usr/bin/perl -w
Ryan Anderson83b24432005-07-31 04:17:25 -04002#
Ryan Andersonf3d9f352005-07-31 20:04:24 -04003# Copyright 2002,2005 Greg Kroah-Hartman <greg@kroah.com>
4# Copyright 2005 Ryan Anderson <ryan@michonline.com>
Ryan Anderson83b24432005-07-31 04:17:25 -04005#
6# GPL v2 (See COPYING)
Junio C Hamano5825e5b2005-07-31 23:05:16 -07007#
Ryan Anderson83b24432005-07-31 04:17:25 -04008# Ported to support git "mbox" format files by Ryan Anderson <ryan@michonline.com>
9#
Ryan Andersonf3d9f352005-07-31 20:04:24 -040010# Sends a collection of emails to the given email addresses, disturbingly fast.
Junio C Hamano5825e5b2005-07-31 23:05:16 -070011#
Ryan Andersonf3d9f352005-07-31 20:04:24 -040012# 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:
Junio C Hamano5825e5b2005-07-31 23:05:16 -070015# first line of the message is who to CC,
Ryan Andersonf3d9f352005-07-31 20:04:24 -040016# and second line is the subject of the message.
Junio C Hamano5825e5b2005-07-31 23:05:16 -070017#
Ryan Anderson83b24432005-07-31 04:17:25 -040018
19use strict;
20use warnings;
21use Term::ReadLine;
Ryan Anderson83b24432005-07-31 04:17:25 -040022use Getopt::Long;
23use Data::Dumper;
Ryan Anderson83b24432005-07-31 04:17:25 -040024
Junio C Hamano280242d2006-07-02 16:03:59 -070025package FakeTerm;
26sub new {
27 my ($class, $reason) = @_;
28 return bless \$reason, shift;
29}
30sub readline {
31 my $self = shift;
32 die "Cannot use readline on FakeTerm: $$self";
33}
34package main;
35
Eric Wong4bc87a22006-03-25 17:20:48 -080036# most mail servers generate the Date: header, but not all...
Jakub Narebski6bdca892006-07-07 20:57:55 +020037sub format_2822_time {
38 my ($time) = @_;
39 my @localtm = localtime($time);
40 my @gmttm = gmtime($time);
41 my $localmin = $localtm[1] + $localtm[2] * 60;
42 my $gmtmin = $gmttm[1] + $gmttm[2] * 60;
43 if ($localtm[0] != $gmttm[0]) {
44 die "local zone differs from GMT by a non-minute interval\n";
45 }
46 if ((($gmttm[6] + 1) % 7) == $localtm[6]) {
47 $localmin += 1440;
48 } elsif ((($gmttm[6] - 1) % 7) == $localtm[6]) {
49 $localmin -= 1440;
50 } elsif ($gmttm[6] != $localtm[6]) {
51 die "local time offset greater than or equal to 24 hours\n";
52 }
53 my $offset = $localmin - $gmtmin;
54 my $offhour = $offset / 60;
55 my $offmin = abs($offset % 60);
56 if (abs($offhour) >= 24) {
57 die ("local time offset greater than or equal to 24 hours\n");
58 }
59
60 return sprintf("%s, %2d %s %d %02d:%02d:%02d %s%02d%02d",
61 qw(Sun Mon Tue Wed Thu Fri Sat)[$localtm[6]],
62 $localtm[3],
63 qw(Jan Feb Mar Apr May Jun
64 Jul Aug Sep Oct Nov Dec)[$localtm[4]],
65 $localtm[5]+1900,
66 $localtm[2],
67 $localtm[1],
68 $localtm[0],
69 ($offset >= 0) ? '+' : '-',
70 abs($offhour),
71 $offmin,
72 );
73}
Eric Wong4bc87a22006-03-25 17:20:48 -080074
Eric Wong567ffeb2006-03-25 16:47:12 -080075my $have_email_valid = eval { require Email::Valid; 1 };
Eric Wong4bc87a22006-03-25 17:20:48 -080076my $smtp;
77
Ryan Andersone2057352005-08-02 21:45:22 -040078sub unique_email_list(@);
Ryan Anderson1f038a02005-09-05 01:13:07 -040079sub cleanup_compose_files();
80
81# Constants (essentially)
82my $compose_filename = ".msg.$$";
Ryan Andersone2057352005-08-02 21:45:22 -040083
Ryan Anderson83b24432005-07-31 04:17:25 -040084# Variables we fill in automatically, or via prompting:
Ryan Anderson58063242006-05-29 12:30:13 -070085my (@to,@cc,@initial_cc,@bcclist,
86 $initial_reply_to,$initial_subject,@files,$from,$compose,$time);
Ryan Anderson83b24432005-07-31 04:17:25 -040087
Ryan Anderson78488b22005-07-31 20:04:24 -040088# Behavior modification variables
Eric Wongaca7ad72006-05-15 02:34:44 -070089my ($chain_reply_to, $quiet, $suppress_from, $no_signed_off_cc) = (1, 0, 0, 0);
90my $smtp_server;
Ryan Anderson78488b22005-07-31 20:04:24 -040091
Ryan Anderson91332612005-07-31 20:04:24 -040092# Example reply to:
Ryan Anderson83b24432005-07-31 04:17:25 -040093#$initial_reply_to = ''; #<20050203173208.GA23964@foobar.com>';
Ryan Anderson83b24432005-07-31 04:17:25 -040094
Junio C Hamano280242d2006-07-02 16:03:59 -070095my $term = eval {
96 new Term::ReadLine 'git-send-email';
97};
98if ($@) {
99 $term = new FakeTerm "$@: going non-interactive";
100}
Ryan Anderson83b24432005-07-31 04:17:25 -0400101
102# Begin by accumulating all the variables (defined above), that we will end up
103# needing, first, from the command line:
104
105my $rc = GetOptions("from=s" => \$from,
106 "in-reply-to=s" => \$initial_reply_to,
107 "subject=s" => \$initial_subject,
108 "to=s" => \@to,
Ryan Andersonda140f82006-02-13 03:05:15 -0500109 "cc=s" => \@initial_cc,
Ryan Anderson58063242006-05-29 12:30:13 -0700110 "bcc=s" => \@bcclist,
Ryan Anderson78488b22005-07-31 20:04:24 -0400111 "chain-reply-to!" => \$chain_reply_to,
Ryan Anderson3342d852005-07-31 20:04:24 -0400112 "smtp-server=s" => \$smtp_server,
Ryan Anderson1f038a02005-09-05 01:13:07 -0400113 "compose" => \$compose,
Ryan Anderson30d08b32006-02-02 11:56:06 -0500114 "quiet" => \$quiet,
Ryan Andersona985d592006-02-13 02:57:09 -0500115 "suppress-from" => \$suppress_from,
Eric Wong8e69b312006-03-03 01:28:48 -0800116 "no-signed-off-cc|no-signed-off-by-cc" => \$no_signed_off_cc,
Ryan Anderson83b24432005-07-31 04:17:25 -0400117 );
118
Eric W. Biederman79ee5552006-06-21 07:17:31 -0600119# Verify the user input
120
121foreach my $entry (@to) {
122 die "Comma in --to entry: $entry'\n" unless $entry !~ m/,/;
123}
124
125foreach my $entry (@initial_cc) {
126 die "Comma in --cc entry: $entry'\n" unless $entry !~ m/,/;
127}
128
129foreach my $entry (@bcclist) {
130 die "Comma in --bcclist entry: $entry'\n" unless $entry !~ m/,/;
131}
132
Ryan Anderson83b24432005-07-31 04:17:25 -0400133# Now, let's fill any that aren't set in with defaults:
134
Junio C Hamanoe4159072006-02-20 14:23:51 -0800135sub gitvar {
136 my ($var) = @_;
137 my $fh;
138 my $pid = open($fh, '-|');
139 die "$!" unless defined $pid;
140 if (!$pid) {
141 exec('git-var', $var) or die "$!";
142 }
143 my ($val) = <$fh>;
144 close $fh or die "$!";
145 chomp($val);
146 return $val;
Ryan Anderson83b24432005-07-31 04:17:25 -0400147}
Junio C Hamanoe4159072006-02-20 14:23:51 -0800148
149sub gitvar_ident {
150 my ($name) = @_;
151 my $val = gitvar($name);
152 my @field = split(/\s+/, $val);
153 return join(' ', @field[0...(@field-3)]);
154}
155
156my ($author) = gitvar_ident('GIT_AUTHOR_IDENT');
157my ($committer) = gitvar_ident('GIT_COMMITTER_IDENT');
Ryan Anderson83b24432005-07-31 04:17:25 -0400158
Eric Wong994d6c62006-05-14 19:13:44 -0700159my %aliases;
160chomp(my @alias_files = `git-repo-config --get-all sendemail.aliasesfile`);
161chomp(my $aliasfiletype = `git-repo-config sendemail.aliasfiletype`);
162my %parse_alias = (
163 # multiline formats can be supported in the future
164 mutt => sub { my $fh = shift; while (<$fh>) {
165 if (/^alias\s+(\S+)\s+(.*)$/) {
166 my ($alias, $addr) = ($1, $2);
167 $addr =~ s/#.*$//; # mutt allows # comments
168 # commas delimit multiple addresses
169 $aliases{$alias} = [ split(/\s*,\s*/, $addr) ];
170 }}},
171 mailrc => sub { my $fh = shift; while (<$fh>) {
172 if (/^alias\s+(\S+)\s+(.*)$/) {
173 # spaces delimit multiple addresses
174 $aliases{$1} = [ split(/\s+/, $2) ];
175 }}},
176 pine => sub { my $fh = shift; while (<$fh>) {
177 if (/^(\S+)\s+(.*)$/) {
178 $aliases{$1} = [ split(/\s*,\s*/, $2) ];
179 }}},
180 gnus => sub { my $fh = shift; while (<$fh>) {
181 if (/\(define-mail-alias\s+"(\S+?)"\s+"(\S+?)"\)/) {
182 $aliases{$1} = [ $2 ];
183 }}}
184);
185
186if (@alias_files && defined $parse_alias{$aliasfiletype}) {
187 foreach my $file (@alias_files) {
188 open my $fh, '<', $file or die "opening $file: $!\n";
189 $parse_alias{$aliasfiletype}->($fh);
190 close $fh;
191 }
192}
193
Ryan Anderson1f038a02005-09-05 01:13:07 -0400194my $prompting = 0;
Ryan Anderson83b24432005-07-31 04:17:25 -0400195if (!defined $from) {
196 $from = $author || $committer;
Ryan Anderson8037d1a2005-07-31 20:04:24 -0400197 do {
198 $_ = $term->readline("Who should the emails appear to be from? ",
199 $from);
Ryan Andersonca9a7d62005-07-31 20:04:25 -0400200 } while (!defined $_);
Ryan Anderson8037d1a2005-07-31 20:04:24 -0400201
Ryan Anderson83b24432005-07-31 04:17:25 -0400202 $from = $_;
203 print "Emails will be sent from: ", $from, "\n";
Ryan Anderson1f038a02005-09-05 01:13:07 -0400204 $prompting++;
Ryan Anderson83b24432005-07-31 04:17:25 -0400205}
206
207if (!@to) {
Ryan Anderson8037d1a2005-07-31 20:04:24 -0400208 do {
Junio C Hamano5825e5b2005-07-31 23:05:16 -0700209 $_ = $term->readline("Who should the emails be sent to? ",
Ryan Anderson8037d1a2005-07-31 20:04:24 -0400210 "");
211 } while (!defined $_);
Ryan Anderson83b24432005-07-31 04:17:25 -0400212 my $to = $_;
213 push @to, split /,/, $to;
Ryan Anderson1f038a02005-09-05 01:13:07 -0400214 $prompting++;
Ryan Anderson83b24432005-07-31 04:17:25 -0400215}
216
Eric Wong994d6c62006-05-14 19:13:44 -0700217sub expand_aliases {
218 my @cur = @_;
219 my @last;
220 do {
221 @last = @cur;
222 @cur = map { $aliases{$_} ? @{$aliases{$_}} : $_ } @last;
223 } while (join(',',@cur) ne join(',',@last));
224 return @cur;
225}
226
227@to = expand_aliases(@to);
228@initial_cc = expand_aliases(@initial_cc);
Ryan Anderson58063242006-05-29 12:30:13 -0700229@bcclist = expand_aliases(@bcclist);
Eric Wong994d6c62006-05-14 19:13:44 -0700230
Ryan Anderson1f038a02005-09-05 01:13:07 -0400231if (!defined $initial_subject && $compose) {
Ryan Anderson8037d1a2005-07-31 20:04:24 -0400232 do {
Junio C Hamano5825e5b2005-07-31 23:05:16 -0700233 $_ = $term->readline("What subject should the emails start with? ",
Ryan Anderson8037d1a2005-07-31 20:04:24 -0400234 $initial_subject);
235 } while (!defined $_);
Ryan Anderson83b24432005-07-31 04:17:25 -0400236 $initial_subject = $_;
Ryan Anderson1f038a02005-09-05 01:13:07 -0400237 $prompting++;
Ryan Anderson83b24432005-07-31 04:17:25 -0400238}
239
Ryan Anderson1f038a02005-09-05 01:13:07 -0400240if (!defined $initial_reply_to && $prompting) {
Ryan Anderson8037d1a2005-07-31 20:04:24 -0400241 do {
Ryan Anderson1f038a02005-09-05 01:13:07 -0400242 $_= $term->readline("Message-ID to be used as In-Reply-To for the first email? ",
Ryan Anderson8037d1a2005-07-31 20:04:24 -0400243 $initial_reply_to);
244 } while (!defined $_);
245
Ryan Anderson83b24432005-07-31 04:17:25 -0400246 $initial_reply_to = $_;
Ryan Anderson78488b22005-07-31 20:04:24 -0400247 $initial_reply_to =~ s/(^\s+|\s+$)//g;
Ryan Anderson83b24432005-07-31 04:17:25 -0400248}
249
Eric Wongaca7ad72006-05-15 02:34:44 -0700250if (!$smtp_server) {
251 foreach (qw( /usr/sbin/sendmail /usr/lib/sendmail )) {
252 if (-x $_) {
253 $smtp_server = $_;
254 last;
255 }
256 }
257 $smtp_server ||= 'localhost'; # could be 127.0.0.1, too... *shrug*
Ryan Anderson3342d852005-07-31 20:04:24 -0400258}
259
Ryan Anderson1f038a02005-09-05 01:13:07 -0400260if ($compose) {
261 # Note that this does not need to be secure, but we will make a small
262 # effort to have it be unique
263 open(C,">",$compose_filename)
264 or die "Failed to open for writing $compose_filename: $!";
Ryan Andersond366c702006-02-02 11:56:06 -0500265 print C "From $from # This line is ignored.\n";
Ryan Anderson1f038a02005-09-05 01:13:07 -0400266 printf C "Subject: %s\n\n", $initial_subject;
267 printf C <<EOT;
268GIT: Please enter your email below.
269GIT: Lines beginning in "GIT: " will be removed.
270GIT: Consider including an overall diffstat or table of contents
271GIT: for the patch you are writing.
272
273EOT
274 close(C);
275
276 my $editor = $ENV{EDITOR};
277 $editor = 'vi' unless defined $editor;
278 system($editor, $compose_filename);
279
280 open(C2,">",$compose_filename . ".final")
281 or die "Failed to open $compose_filename.final : " . $!;
282
283 open(C,"<",$compose_filename)
284 or die "Failed to open $compose_filename : " . $!;
285
286 while(<C>) {
287 next if m/^GIT: /;
288 print C2 $_;
289 }
290 close(C);
291 close(C2);
292
293 do {
294 $_ = $term->readline("Send this email? (y|n) ");
295 } while (!defined $_);
296
297 if (uc substr($_,0,1) ne 'Y') {
298 cleanup_compose_files();
299 exit(0);
300 }
301
302 @files = ($compose_filename . ".final");
303}
304
305
Ryan Anderson83b24432005-07-31 04:17:25 -0400306# Now that all the defaults are set, process the rest of the command line
307# arguments and collect up the files that need to be processed.
308for my $f (@ARGV) {
309 if (-d $f) {
310 opendir(DH,$f)
311 or die "Failed to opendir $f: $!";
312
Junio C Hamano5825e5b2005-07-31 23:05:16 -0700313 push @files, grep { -f $_ } map { +$f . "/" . $_ }
Ryan Anderson8037d1a2005-07-31 20:04:24 -0400314 sort readdir(DH);
315
Ryan Anderson83b24432005-07-31 04:17:25 -0400316 } elsif (-f $f) {
317 push @files, $f;
318
319 } else {
320 print STDERR "Skipping $f - not found.\n";
321 }
322}
323
324if (@files) {
Ryan Anderson27184352006-02-05 20:13:52 -0500325 unless ($quiet) {
326 print $_,"\n" for (@files);
327 }
Ryan Anderson83b24432005-07-31 04:17:25 -0400328} else {
329 print <<EOT;
Junio C Hamano215a7ad2005-09-07 17:26:23 -0700330git-send-email [options] <file | directory> [... file | directory ]
Ryan Anderson83b24432005-07-31 04:17:25 -0400331Options:
332 --from Specify the "From:" line of the email to be sent.
Ryan Anderson1f038a02005-09-05 01:13:07 -0400333
Junio C Hamano5825e5b2005-07-31 23:05:16 -0700334 --to Specify the primary "To:" line of the email.
Ryan Anderson1f038a02005-09-05 01:13:07 -0400335
Ryan Andersonda140f82006-02-13 03:05:15 -0500336 --cc Specify an initial "Cc:" list for the entire series
337 of emails.
338
Ryan Anderson58063242006-05-29 12:30:13 -0700339 --bcc Specify a list of email addresses that should be Bcc:
340 on all the emails.
341
Ryan Anderson1f038a02005-09-05 01:13:07 -0400342 --compose Use \$EDITOR to edit an introductory message for the
343 patch series.
344
Ryan Anderson83b24432005-07-31 04:17:25 -0400345 --subject Specify the initial "Subject:" line.
Ryan Anderson1f038a02005-09-05 01:13:07 -0400346 Only necessary if --compose is also set. If --compose
347 is not set, this will be prompted for.
348
Ryan Anderson83b24432005-07-31 04:17:25 -0400349 --in-reply-to Specify the first "In-Reply-To:" header line.
Ryan Anderson1f038a02005-09-05 01:13:07 -0400350 Only used if --compose is also set. If --compose is not
351 set, this will be prompted for.
352
Ryan Andersone2057352005-08-02 21:45:22 -0400353 --chain-reply-to If set, the replies will all be to the previous
Junio C Hamano5825e5b2005-07-31 23:05:16 -0700354 email sent, rather than to the first email sent.
355 Defaults to on.
Ryan Anderson1f038a02005-09-05 01:13:07 -0400356
Ryan Andersona985d592006-02-13 02:57:09 -0500357 --no-signed-off-cc Suppress the automatic addition of email addresses
358 that appear in a Signed-off-by: line, to the cc: list.
359 Note: Using this option is not recommended.
360
Ryan Anderson3342d852005-07-31 20:04:24 -0400361 --smtp-server If set, specifies the outgoing SMTP server to use.
362 Defaults to localhost.
Ryan Anderson83b24432005-07-31 04:17:25 -0400363
Pavel Roskin82e5a822006-07-10 01:50:18 -0400364 --suppress-from Suppress sending emails to yourself if your address
Ryan Andersona985d592006-02-13 02:57:09 -0500365 appears in a From: line.
366
Ryan Anderson27184352006-02-05 20:13:52 -0500367 --quiet Make git-send-email less verbose. One line per email should be
368 all that is output.
369
Ryan Anderson83b24432005-07-31 04:17:25 -0400370Error: Please specify a file or a directory on the command line.
371EOT
372 exit(1);
373}
374
375# Variables we set as part of the loop over files
Ryan Anderson7ccf7922006-05-29 12:30:12 -0700376our ($message_id, $cc, %mail, $subject, $reply_to, $references, $message);
Ryan Anderson83b24432005-07-31 04:17:25 -0400377
Eric Wong567ffeb2006-03-25 16:47:12 -0800378sub extract_valid_address {
379 my $address = shift;
Junio C Hamanoad9c18f2006-06-06 00:05:56 -0700380 my $local_part_regexp = '[^<>"\s@]+';
Junio C Hamano09302e12006-06-06 14:12:46 -0700381 my $domain_regexp = '[^.<>"\s@]+(?:\.[^.<>"\s@]+)+';
Eric Wongdb3106b2006-05-15 02:41:01 -0700382
383 # check for a local address:
Junio C Hamanoad9c18f2006-06-06 00:05:56 -0700384 return $address if ($address =~ /^($local_part_regexp)$/);
Eric Wongdb3106b2006-05-15 02:41:01 -0700385
Eric Wong567ffeb2006-03-25 16:47:12 -0800386 if ($have_email_valid) {
Junio C Hamanoad9c18f2006-06-06 00:05:56 -0700387 return scalar Email::Valid->address($address);
Eric Wong567ffeb2006-03-25 16:47:12 -0800388 } else {
389 # less robust/correct than the monster regexp in Email::Valid,
390 # but still does a 99% job, and one less dependency
Junio C Hamanoad9c18f2006-06-06 00:05:56 -0700391 $address =~ /($local_part_regexp\@$domain_regexp)/;
Horst H. von Brande96fd302006-06-03 13:11:48 -0400392 return $1;
Eric Wong567ffeb2006-03-25 16:47:12 -0800393 }
394}
Ryan Anderson83b24432005-07-31 04:17:25 -0400395
396# Usually don't need to change anything below here.
397
398# we make a "fake" message id by taking the current number
399# of seconds since the beginning of Unix time and tacking on
400# a random number to the end, in case we are called quicker than
401# 1 second since the last time we were called.
Ryan Anderson8037d1a2005-07-31 20:04:24 -0400402
403# We'll setup a template for the message id, using the "from" address:
Eric Wong567ffeb2006-03-25 16:47:12 -0800404my $message_id_from = extract_valid_address($from);
Ryan Andersone2057352005-08-02 21:45:22 -0400405my $message_id_template = "<%s-git-send-email-$message_id_from>";
Ryan Anderson8037d1a2005-07-31 20:04:24 -0400406
Ryan Anderson83b24432005-07-31 04:17:25 -0400407sub make_message_id
408{
Eric Wong72095d52006-03-25 02:43:31 -0800409 my $date = time;
Ryan Anderson83b24432005-07-31 04:17:25 -0400410 my $pseudo_rand = int (rand(4200));
Ryan Anderson8037d1a2005-07-31 20:04:24 -0400411 $message_id = sprintf $message_id_template, "$date$pseudo_rand";
412 #print "new message id = $message_id\n"; # Was useful for debugging
Ryan Anderson83b24432005-07-31 04:17:25 -0400413}
414
415
416
417$cc = "";
Eric Wonga5370b12006-03-25 03:01:01 -0800418$time = time - scalar $#files;
Ryan Anderson83b24432005-07-31 04:17:25 -0400419
420sub send_message
421{
Eric Wong4bc87a22006-03-25 17:20:48 -0800422 my @recipients = unique_email_list(@to);
423 my $to = join (",\n\t", @recipients);
Ryan Anderson58063242006-05-29 12:30:13 -0700424 @recipients = unique_email_list(@recipients,@cc,@bcclist);
Jakub Narebski6bdca892006-07-07 20:57:55 +0200425 my $date = format_2822_time($time++);
Martin Langhoffe923eff2006-05-03 09:44:36 +1200426 my $gitversion = '@@GIT_VERSION@@';
427 if ($gitversion =~ m/..GIT_VERSION../) {
428 $gitversion = `git --version`;
429 chomp $gitversion;
430 # keep only what's after the last space
431 $gitversion =~ s/^.* //;
432 }
Ryan Anderson83b24432005-07-31 04:17:25 -0400433
Eric Wong4bc87a22006-03-25 17:20:48 -0800434 my $header = "From: $from
435To: $to
436Cc: $cc
437Subject: $subject
Eric Wong4bc87a22006-03-25 17:20:48 -0800438Date: $date
439Message-Id: $message_id
Martin Langhoffe923eff2006-05-03 09:44:36 +1200440X-Mailer: git-send-email $gitversion
Eric Wong4bc87a22006-03-25 17:20:48 -0800441";
Ryan Anderson7ccf7922006-05-29 12:30:12 -0700442 if ($reply_to) {
443
444 $header .= "In-Reply-To: $reply_to\n";
445 $header .= "References: $references\n";
446 }
Ryan Anderson83b24432005-07-31 04:17:25 -0400447
Eric Wongaca7ad72006-05-15 02:34:44 -0700448 if ($smtp_server =~ m#^/#) {
449 my $pid = open my $sm, '|-';
450 defined $pid or die $!;
451 if (!$pid) {
Junio C Hamano2186d562006-05-29 23:53:13 -0700452 exec($smtp_server,'-i',
Junio C Hamanoad9c18f2006-06-06 00:05:56 -0700453 map { extract_valid_address($_) }
Junio C Hamano2186d562006-05-29 23:53:13 -0700454 @recipients) or die $!;
Eric Wongaca7ad72006-05-15 02:34:44 -0700455 }
456 print $sm "$header\n$message";
457 close $sm or die $?;
458 } else {
Johannes Schindelin87840622006-06-01 00:55:47 +0200459 require Net::SMTP;
Eric Wongaca7ad72006-05-15 02:34:44 -0700460 $smtp ||= Net::SMTP->new( $smtp_server );
461 $smtp->mail( $from ) or die $smtp->message;
462 $smtp->to( @recipients ) or die $smtp->message;
463 $smtp->data or die $smtp->message;
464 $smtp->datasend("$header\n$message") or die $smtp->message;
465 $smtp->dataend() or die $smtp->message;
466 $smtp->ok or die "Failed to send $subject\n".$smtp->message;
467 }
Ryan Anderson27184352006-02-05 20:13:52 -0500468 if ($quiet) {
469 printf "Sent %s\n", $subject;
470 } else {
Eric Wongaca7ad72006-05-15 02:34:44 -0700471 print "OK. Log says:\nDate: $date\n";
472 if ($smtp) {
473 print "Server: $smtp_server\n";
474 } else {
475 print "Sendmail: $smtp_server\n";
476 }
477 print "From: $from\nSubject: $subject\nCc: $cc\nTo: $to\n\n";
478 if ($smtp) {
479 print "Result: ", $smtp->code, ' ',
480 ($smtp->message =~ /\n([^\n]+\n)$/s), "\n";
481 } else {
482 print "Result: OK\n";
483 }
Ryan Anderson30d08b32006-02-02 11:56:06 -0500484 }
Ryan Anderson83b24432005-07-31 04:17:25 -0400485}
486
Ryan Anderson83b24432005-07-31 04:17:25 -0400487$reply_to = $initial_reply_to;
Junio C Hamano2186d562006-05-29 23:53:13 -0700488$references = $initial_reply_to || '';
Ryan Anderson83b24432005-07-31 04:17:25 -0400489make_message_id();
490$subject = $initial_subject;
491
492foreach my $t (@files) {
Ryan Anderson83b24432005-07-31 04:17:25 -0400493 open(F,"<",$t) or die "can't open file $t";
494
Junio C Hamano8a8e6232006-03-23 23:43:52 -0800495 my $author_not_sender = undef;
Ryan Andersonda140f82006-02-13 03:05:15 -0500496 @cc = @initial_cc;
Ryan Anderson83b24432005-07-31 04:17:25 -0400497 my $found_mbox = 0;
498 my $header_done = 0;
499 $message = "";
500 while(<F>) {
501 if (!$header_done) {
502 $found_mbox = 1, next if (/^From /);
503 chomp;
504
505 if ($found_mbox) {
506 if (/^Subject:\s+(.*)$/) {
507 $subject = $1;
508
509 } elsif (/^(Cc|From):\s+(.*)$/) {
Junio C Hamano8a8e6232006-03-23 23:43:52 -0800510 if ($2 eq $from) {
511 next if ($suppress_from);
512 }
Haavard Skinnemoen68d42c42006-08-23 03:02:59 -0700513 elsif ($1 eq 'From') {
Junio C Hamano8a8e6232006-03-23 23:43:52 -0800514 $author_not_sender = $2;
515 }
Ryan Anderson83b24432005-07-31 04:17:25 -0400516 printf("(mbox) Adding cc: %s from line '%s'\n",
Ryan Anderson27184352006-02-05 20:13:52 -0500517 $2, $_) unless $quiet;
Ryan Anderson83b24432005-07-31 04:17:25 -0400518 push @cc, $2;
519 }
520
521 } else {
522 # In the traditional
523 # "send lots of email" format,
524 # line 1 = cc
525 # line 2 = subject
526 # So let's support that, too.
527 if (@cc == 0) {
528 printf("(non-mbox) Adding cc: %s from line '%s'\n",
Ryan Anderson27184352006-02-05 20:13:52 -0500529 $_, $_) unless $quiet;
Ryan Anderson83b24432005-07-31 04:17:25 -0400530
531 push @cc, $_;
532
533 } elsif (!defined $subject) {
534 $subject = $_;
535 }
536 }
Junio C Hamano5825e5b2005-07-31 23:05:16 -0700537
Ryan Anderson83b24432005-07-31 04:17:25 -0400538 # A whitespace line will terminate the headers
539 if (m/^\s*$/) {
540 $header_done = 1;
541 }
542 } else {
543 $message .= $_;
Ryan Andersona985d592006-02-13 02:57:09 -0500544 if (/^Signed-off-by: (.*)$/i && !$no_signed_off_cc) {
Ryan Anderson83b24432005-07-31 04:17:25 -0400545 my $c = $1;
546 chomp $c;
547 push @cc, $c;
548 printf("(sob) Adding cc: %s from line '%s'\n",
Ryan Anderson27184352006-02-05 20:13:52 -0500549 $c, $_) unless $quiet;
Ryan Anderson83b24432005-07-31 04:17:25 -0400550 }
551 }
552 }
553 close F;
Junio C Hamano8a8e6232006-03-23 23:43:52 -0800554 if (defined $author_not_sender) {
555 $message = "From: $author_not_sender\n\n$message";
556 }
Ryan Anderson83b24432005-07-31 04:17:25 -0400557
Ryan Andersone2057352005-08-02 21:45:22 -0400558 $cc = join(", ", unique_email_list(@cc));
Ryan Anderson83b24432005-07-31 04:17:25 -0400559
560 send_message();
561
562 # set up for the next message
Ryan Anderson78488b22005-07-31 20:04:24 -0400563 if ($chain_reply_to || length($reply_to) == 0) {
564 $reply_to = $message_id;
Ryan Anderson7ccf7922006-05-29 12:30:12 -0700565 if (length $references > 0) {
566 $references .= " $message_id";
567 } else {
568 $references = "$message_id";
569 }
Ryan Anderson78488b22005-07-31 20:04:24 -0400570 }
Ryan Anderson83b24432005-07-31 04:17:25 -0400571 make_message_id();
Ryan Anderson83b24432005-07-31 04:17:25 -0400572}
Ryan Andersone2057352005-08-02 21:45:22 -0400573
Ryan Anderson1f038a02005-09-05 01:13:07 -0400574if ($compose) {
575 cleanup_compose_files();
576}
577
578sub cleanup_compose_files() {
579 unlink($compose_filename, $compose_filename . ".final");
580
581}
582
Eric Wong4bc87a22006-03-25 17:20:48 -0800583$smtp->quit if $smtp;
Ryan Andersone2057352005-08-02 21:45:22 -0400584
585sub unique_email_list(@) {
586 my %seen;
587 my @emails;
588
589 foreach my $entry (@_) {
Eric Wongdb3106b2006-05-15 02:41:01 -0700590 if (my $clean = extract_valid_address($entry)) {
591 $seen{$clean} ||= 0;
592 next if $seen{$clean}++;
593 push @emails, $entry;
594 } else {
595 print STDERR "W: unable to extract a valid address",
596 " from: $entry\n";
597 }
Ryan Andersone2057352005-08-02 21:45:22 -0400598 }
599 return @emails;
600}