blob: 12ced288857c7531f964d2e08e9a7c3cc1136dbd [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;
Petr Baudis3cb8caf2006-07-03 22:47:58 +020024use Git;
Ryan Anderson83b24432005-07-31 04:17:25 -040025
Junio C Hamano280242d2006-07-02 16:03:59 -070026package FakeTerm;
27sub new {
28 my ($class, $reason) = @_;
29 return bless \$reason, shift;
30}
31sub readline {
32 my $self = shift;
33 die "Cannot use readline on FakeTerm: $$self";
34}
35package main;
36
Michael Coleman1b0baf12007-02-27 22:47:54 -060037
38sub usage {
39 print <<EOT;
40git-send-email [options] <file | directory>...
41Options:
42 --from Specify the "From:" line of the email to be sent.
43
44 --to Specify the primary "To:" line of the email.
45
46 --cc Specify an initial "Cc:" list for the entire series
47 of emails.
48
49 --bcc Specify a list of email addresses that should be Bcc:
50 on all the emails.
51
52 --compose Use \$EDITOR to edit an introductory message for the
53 patch series.
54
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.
58
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.
62
63 --chain-reply-to If set, the replies will all be to the previous
64 email sent, rather than to the first email sent.
65 Defaults to on.
66
67 --no-signed-off-cc Suppress the automatic addition of email addresses
J. Bruce Fieldsabec1002007-03-18 21:37:53 -040068 that appear in Signed-off-by: or Cc: lines to the cc:
69 list. Note: Using this option is not recommended.
Michael Coleman1b0baf12007-02-27 22:47:54 -060070
71 --smtp-server If set, specifies the outgoing SMTP server to use.
72 Defaults to localhost.
73
74 --suppress-from Suppress sending emails to yourself if your address
75 appears in a From: line.
76
77 --quiet Make git-send-email less verbose. One line per email
78 should be all that is output.
79
Robin H. Johnson238cc632007-04-25 19:37:15 -070080 --dry-run Do everything except actually send the emails.
81
Robin H. Johnsonf073a592007-04-25 19:37:22 -070082 --envelope-sender Specify the envelope sender used to send the emails.
83
Michael Coleman1b0baf12007-02-27 22:47:54 -060084EOT
85 exit(1);
86}
87
Eric Wong4bc87a22006-03-25 17:20:48 -080088# most mail servers generate the Date: header, but not all...
Jakub Narebski6bdca892006-07-07 20:57:55 +020089sub format_2822_time {
90 my ($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";
97 }
98 if ((($gmttm[6] + 1) % 7) == $localtm[6]) {
99 $localmin += 1440;
100 } elsif ((($gmttm[6] - 1) % 7) == $localtm[6]) {
101 $localmin -= 1440;
102 } elsif ($gmttm[6] != $localtm[6]) {
103 die "local time offset greater than or equal to 24 hours\n";
104 }
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");
110 }
111
112 return sprintf("%s, %2d %s %d %02d:%02d:%02d %s%02d%02d",
113 qw(Sun Mon Tue Wed Thu Fri Sat)[$localtm[6]],
114 $localtm[3],
115 qw(Jan Feb Mar Apr May Jun
116 Jul Aug Sep Oct Nov Dec)[$localtm[4]],
117 $localtm[5]+1900,
118 $localtm[2],
119 $localtm[1],
120 $localtm[0],
121 ($offset >= 0) ? '+' : '-',
122 abs($offhour),
123 $offmin,
124 );
125}
Eric Wong4bc87a22006-03-25 17:20:48 -0800126
Eric Wong567ffeb2006-03-25 16:47:12 -0800127my $have_email_valid = eval { require Email::Valid; 1 };
Eric Wong4bc87a22006-03-25 17:20:48 -0800128my $smtp;
129
Ryan Andersone2057352005-08-02 21:45:22 -0400130sub unique_email_list(@);
Ryan Anderson1f038a02005-09-05 01:13:07 -0400131sub cleanup_compose_files();
132
133# Constants (essentially)
134my $compose_filename = ".msg.$$";
Ryan Andersone2057352005-08-02 21:45:22 -0400135
Ryan Anderson83b24432005-07-31 04:17:25 -0400136# Variables we fill in automatically, or via prompting:
Junio C Hamanoce91c2f2006-10-05 16:36:49 -0700137my (@to,@cc,@initial_cc,@bcclist,@xh,
Ryan Anderson58063242006-05-29 12:30:13 -0700138 $initial_reply_to,$initial_subject,@files,$from,$compose,$time);
Ryan Anderson83b24432005-07-31 04:17:25 -0400139
Ryan Anderson78488b22005-07-31 20:04:24 -0400140# Behavior modification variables
Matthew Wilcox61302592006-10-10 08:58:23 -0600141my ($chain_reply_to, $quiet, $suppress_from, $no_signed_off_cc,
142 $dry_run) = (1, 0, 0, 0, 0);
Eric Wongaca7ad72006-05-15 02:34:44 -0700143my $smtp_server;
Robin H. Johnsonf073a592007-04-25 19:37:22 -0700144my $envelope_sender;
Ryan Anderson78488b22005-07-31 20:04:24 -0400145
Ryan Anderson91332612005-07-31 20:04:24 -0400146# Example reply to:
Ryan Anderson83b24432005-07-31 04:17:25 -0400147#$initial_reply_to = ''; #<20050203173208.GA23964@foobar.com>';
Ryan Anderson83b24432005-07-31 04:17:25 -0400148
Petr Baudis3cb8caf2006-07-03 22:47:58 +0200149my $repo = Git->repository();
Junio C Hamano280242d2006-07-02 16:03:59 -0700150my $term = eval {
151 new Term::ReadLine 'git-send-email';
152};
153if ($@) {
154 $term = new FakeTerm "$@: going non-interactive";
155}
Ryan Anderson83b24432005-07-31 04:17:25 -0400156
Avi Kivity4a62d3f2007-03-11 19:19:44 +0200157my $def_chain = $repo->config_boolean('sendemail.chainreplyto');
158if ($def_chain and $def_chain eq 'false') {
159 $chain_reply_to = 0;
160}
161
162@bcclist = $repo->config('sendemail.bcc');
163if (!@bcclist or !$bcclist[0]) {
164 @bcclist = ();
165}
166
Ryan Anderson83b24432005-07-31 04:17:25 -0400167# Begin by accumulating all the variables (defined above), that we will end up
168# needing, first, from the command line:
169
170my $rc = GetOptions("from=s" => \$from,
171 "in-reply-to=s" => \$initial_reply_to,
172 "subject=s" => \$initial_subject,
173 "to=s" => \@to,
Ryan Andersonda140f82006-02-13 03:05:15 -0500174 "cc=s" => \@initial_cc,
Ryan Anderson58063242006-05-29 12:30:13 -0700175 "bcc=s" => \@bcclist,
Ryan Anderson78488b22005-07-31 20:04:24 -0400176 "chain-reply-to!" => \$chain_reply_to,
Ryan Anderson3342d852005-07-31 20:04:24 -0400177 "smtp-server=s" => \$smtp_server,
Ryan Anderson1f038a02005-09-05 01:13:07 -0400178 "compose" => \$compose,
Ryan Anderson30d08b32006-02-02 11:56:06 -0500179 "quiet" => \$quiet,
Ryan Andersona985d592006-02-13 02:57:09 -0500180 "suppress-from" => \$suppress_from,
Eric Wong8e69b312006-03-03 01:28:48 -0800181 "no-signed-off-cc|no-signed-off-by-cc" => \$no_signed_off_cc,
Matthew Wilcox61302592006-10-10 08:58:23 -0600182 "dry-run" => \$dry_run,
Robin H. Johnsonf073a592007-04-25 19:37:22 -0700183 "envelope-sender=s" => \$envelope_sender,
Ryan Anderson83b24432005-07-31 04:17:25 -0400184 );
185
Michael Coleman1b0baf12007-02-27 22:47:54 -0600186unless ($rc) {
187 usage();
188}
189
Eric W. Biederman79ee5552006-06-21 07:17:31 -0600190# Verify the user input
191
192foreach my $entry (@to) {
193 die "Comma in --to entry: $entry'\n" unless $entry !~ m/,/;
194}
195
196foreach my $entry (@initial_cc) {
197 die "Comma in --cc entry: $entry'\n" unless $entry !~ m/,/;
198}
199
200foreach my $entry (@bcclist) {
201 die "Comma in --bcclist entry: $entry'\n" unless $entry !~ m/,/;
202}
203
Ryan Anderson83b24432005-07-31 04:17:25 -0400204# Now, let's fill any that aren't set in with defaults:
205
Petr Baudisc7a30e52006-07-03 22:48:01 +0200206my ($author) = $repo->ident_person('author');
207my ($committer) = $repo->ident_person('committer');
Ryan Anderson83b24432005-07-31 04:17:25 -0400208
Eric Wong994d6c62006-05-14 19:13:44 -0700209my %aliases;
Petr Baudis3cb8caf2006-07-03 22:47:58 +0200210my @alias_files = $repo->config('sendemail.aliasesfile');
211my $aliasfiletype = $repo->config('sendemail.aliasfiletype');
Eric Wong994d6c62006-05-14 19:13:44 -0700212my %parse_alias = (
213 # multiline formats can be supported in the future
214 mutt => sub { my $fh = shift; while (<$fh>) {
215 if (/^alias\s+(\S+)\s+(.*)$/) {
216 my ($alias, $addr) = ($1, $2);
217 $addr =~ s/#.*$//; # mutt allows # comments
218 # commas delimit multiple addresses
219 $aliases{$alias} = [ split(/\s*,\s*/, $addr) ];
220 }}},
221 mailrc => sub { my $fh = shift; while (<$fh>) {
222 if (/^alias\s+(\S+)\s+(.*)$/) {
223 # spaces delimit multiple addresses
224 $aliases{$1} = [ split(/\s+/, $2) ];
225 }}},
226 pine => sub { my $fh = shift; while (<$fh>) {
227 if (/^(\S+)\s+(.*)$/) {
228 $aliases{$1} = [ split(/\s*,\s*/, $2) ];
229 }}},
230 gnus => sub { my $fh = shift; while (<$fh>) {
231 if (/\(define-mail-alias\s+"(\S+?)"\s+"(\S+?)"\)/) {
232 $aliases{$1} = [ $2 ];
233 }}}
234);
235
Petr Baudis3cb8caf2006-07-03 22:47:58 +0200236if (@alias_files and $aliasfiletype and defined $parse_alias{$aliasfiletype}) {
Eric Wong994d6c62006-05-14 19:13:44 -0700237 foreach my $file (@alias_files) {
238 open my $fh, '<', $file or die "opening $file: $!\n";
239 $parse_alias{$aliasfiletype}->($fh);
240 close $fh;
241 }
242}
243
Ryan Anderson1f038a02005-09-05 01:13:07 -0400244my $prompting = 0;
Ryan Anderson83b24432005-07-31 04:17:25 -0400245if (!defined $from) {
246 $from = $author || $committer;
Ryan Anderson8037d1a2005-07-31 20:04:24 -0400247 do {
Quy Tonthat2ef58052006-12-28 01:16:21 +1100248 $_ = $term->readline("Who should the emails appear to be from? [$from] ");
Ryan Andersonca9a7d62005-07-31 20:04:25 -0400249 } while (!defined $_);
Ryan Anderson8037d1a2005-07-31 20:04:24 -0400250
Quy Tonthat2ef58052006-12-28 01:16:21 +1100251 $from = $_ if ($_);
Ryan Anderson83b24432005-07-31 04:17:25 -0400252 print "Emails will be sent from: ", $from, "\n";
Ryan Anderson1f038a02005-09-05 01:13:07 -0400253 $prompting++;
Ryan Anderson83b24432005-07-31 04:17:25 -0400254}
255
256if (!@to) {
Ryan Anderson8037d1a2005-07-31 20:04:24 -0400257 do {
Junio C Hamano5825e5b2005-07-31 23:05:16 -0700258 $_ = $term->readline("Who should the emails be sent to? ",
Ryan Anderson8037d1a2005-07-31 20:04:24 -0400259 "");
260 } while (!defined $_);
Ryan Anderson83b24432005-07-31 04:17:25 -0400261 my $to = $_;
262 push @to, split /,/, $to;
Ryan Anderson1f038a02005-09-05 01:13:07 -0400263 $prompting++;
Ryan Anderson83b24432005-07-31 04:17:25 -0400264}
265
Eric Wong994d6c62006-05-14 19:13:44 -0700266sub expand_aliases {
267 my @cur = @_;
268 my @last;
269 do {
270 @last = @cur;
271 @cur = map { $aliases{$_} ? @{$aliases{$_}} : $_ } @last;
272 } while (join(',',@cur) ne join(',',@last));
273 return @cur;
274}
275
276@to = expand_aliases(@to);
Robin H. Johnsonbf7af112007-04-25 21:53:22 -0700277@to = (map { sanitize_address_rfc822($_) } @to);
Eric Wong994d6c62006-05-14 19:13:44 -0700278@initial_cc = expand_aliases(@initial_cc);
Ryan Anderson58063242006-05-29 12:30:13 -0700279@bcclist = expand_aliases(@bcclist);
Eric Wong994d6c62006-05-14 19:13:44 -0700280
Ryan Anderson1f038a02005-09-05 01:13:07 -0400281if (!defined $initial_subject && $compose) {
Ryan Anderson8037d1a2005-07-31 20:04:24 -0400282 do {
Junio C Hamano5825e5b2005-07-31 23:05:16 -0700283 $_ = $term->readline("What subject should the emails start with? ",
Ryan Anderson8037d1a2005-07-31 20:04:24 -0400284 $initial_subject);
285 } while (!defined $_);
Ryan Anderson83b24432005-07-31 04:17:25 -0400286 $initial_subject = $_;
Ryan Anderson1f038a02005-09-05 01:13:07 -0400287 $prompting++;
Ryan Anderson83b24432005-07-31 04:17:25 -0400288}
289
Ryan Anderson1f038a02005-09-05 01:13:07 -0400290if (!defined $initial_reply_to && $prompting) {
Ryan Anderson8037d1a2005-07-31 20:04:24 -0400291 do {
Ryan Anderson1f038a02005-09-05 01:13:07 -0400292 $_= $term->readline("Message-ID to be used as In-Reply-To for the first email? ",
Ryan Anderson8037d1a2005-07-31 20:04:24 -0400293 $initial_reply_to);
294 } while (!defined $_);
295
Ryan Anderson83b24432005-07-31 04:17:25 -0400296 $initial_reply_to = $_;
Ryan Anderson78488b22005-07-31 20:04:24 -0400297 $initial_reply_to =~ s/(^\s+|\s+$)//g;
Ryan Anderson83b24432005-07-31 04:17:25 -0400298}
299
Eric Wongaca7ad72006-05-15 02:34:44 -0700300if (!$smtp_server) {
Sergey Vlasov6dcfa302006-10-29 22:31:39 +0300301 $smtp_server = $repo->config('sendemail.smtpserver');
302}
303if (!$smtp_server) {
Eric Wongaca7ad72006-05-15 02:34:44 -0700304 foreach (qw( /usr/sbin/sendmail /usr/lib/sendmail )) {
305 if (-x $_) {
306 $smtp_server = $_;
307 last;
308 }
309 }
310 $smtp_server ||= 'localhost'; # could be 127.0.0.1, too... *shrug*
Ryan Anderson3342d852005-07-31 20:04:24 -0400311}
312
Ryan Anderson1f038a02005-09-05 01:13:07 -0400313if ($compose) {
314 # Note that this does not need to be secure, but we will make a small
315 # effort to have it be unique
316 open(C,">",$compose_filename)
317 or die "Failed to open for writing $compose_filename: $!";
Ryan Andersond366c702006-02-02 11:56:06 -0500318 print C "From $from # This line is ignored.\n";
Ryan Anderson1f038a02005-09-05 01:13:07 -0400319 printf C "Subject: %s\n\n", $initial_subject;
320 printf C <<EOT;
321GIT: Please enter your email below.
322GIT: Lines beginning in "GIT: " will be removed.
323GIT: Consider including an overall diffstat or table of contents
324GIT: for the patch you are writing.
325
326EOT
327 close(C);
328
329 my $editor = $ENV{EDITOR};
330 $editor = 'vi' unless defined $editor;
331 system($editor, $compose_filename);
332
333 open(C2,">",$compose_filename . ".final")
334 or die "Failed to open $compose_filename.final : " . $!;
335
336 open(C,"<",$compose_filename)
337 or die "Failed to open $compose_filename : " . $!;
338
339 while(<C>) {
340 next if m/^GIT: /;
341 print C2 $_;
342 }
343 close(C);
344 close(C2);
345
346 do {
347 $_ = $term->readline("Send this email? (y|n) ");
348 } while (!defined $_);
349
350 if (uc substr($_,0,1) ne 'Y') {
351 cleanup_compose_files();
352 exit(0);
353 }
354
355 @files = ($compose_filename . ".final");
356}
357
358
Ryan Anderson83b24432005-07-31 04:17:25 -0400359# Now that all the defaults are set, process the rest of the command line
360# arguments and collect up the files that need to be processed.
361for my $f (@ARGV) {
362 if (-d $f) {
363 opendir(DH,$f)
364 or die "Failed to opendir $f: $!";
365
Junio C Hamano5825e5b2005-07-31 23:05:16 -0700366 push @files, grep { -f $_ } map { +$f . "/" . $_ }
Ryan Anderson8037d1a2005-07-31 20:04:24 -0400367 sort readdir(DH);
368
Ryan Anderson83b24432005-07-31 04:17:25 -0400369 } elsif (-f $f) {
370 push @files, $f;
371
372 } else {
373 print STDERR "Skipping $f - not found.\n";
374 }
375}
376
377if (@files) {
Ryan Anderson27184352006-02-05 20:13:52 -0500378 unless ($quiet) {
379 print $_,"\n" for (@files);
380 }
Ryan Anderson83b24432005-07-31 04:17:25 -0400381} else {
Michael Coleman1b0baf12007-02-27 22:47:54 -0600382 print STDERR "\nNo patch files specified!\n\n";
383 usage();
Ryan Anderson83b24432005-07-31 04:17:25 -0400384}
385
386# Variables we set as part of the loop over files
Robin H. Johnsonaf068d22007-04-25 19:37:18 -0700387our ($message_id, %mail, $subject, $reply_to, $references, $message);
Ryan Anderson83b24432005-07-31 04:17:25 -0400388
Eric Wong567ffeb2006-03-25 16:47:12 -0800389sub extract_valid_address {
390 my $address = shift;
Junio C Hamanoad9c18f2006-06-06 00:05:56 -0700391 my $local_part_regexp = '[^<>"\s@]+';
Junio C Hamano09302e12006-06-06 14:12:46 -0700392 my $domain_regexp = '[^.<>"\s@]+(?:\.[^.<>"\s@]+)+';
Eric Wongdb3106b2006-05-15 02:41:01 -0700393
394 # check for a local address:
Junio C Hamanoad9c18f2006-06-06 00:05:56 -0700395 return $address if ($address =~ /^($local_part_regexp)$/);
Eric Wongdb3106b2006-05-15 02:41:01 -0700396
Eric Wong567ffeb2006-03-25 16:47:12 -0800397 if ($have_email_valid) {
Junio C Hamanoad9c18f2006-06-06 00:05:56 -0700398 return scalar Email::Valid->address($address);
Eric Wong567ffeb2006-03-25 16:47:12 -0800399 } else {
400 # less robust/correct than the monster regexp in Email::Valid,
401 # but still does a 99% job, and one less dependency
Junio C Hamanoad9c18f2006-06-06 00:05:56 -0700402 $address =~ /($local_part_regexp\@$domain_regexp)/;
Horst H. von Brande96fd302006-06-03 13:11:48 -0400403 return $1;
Eric Wong567ffeb2006-03-25 16:47:12 -0800404 }
405}
Ryan Anderson83b24432005-07-31 04:17:25 -0400406
407# Usually don't need to change anything below here.
408
409# we make a "fake" message id by taking the current number
410# of seconds since the beginning of Unix time and tacking on
411# a random number to the end, in case we are called quicker than
412# 1 second since the last time we were called.
Ryan Anderson8037d1a2005-07-31 20:04:24 -0400413
414# We'll setup a template for the message id, using the "from" address:
Eric Wong567ffeb2006-03-25 16:47:12 -0800415my $message_id_from = extract_valid_address($from);
Ryan Andersone2057352005-08-02 21:45:22 -0400416my $message_id_template = "<%s-git-send-email-$message_id_from>";
Ryan Anderson8037d1a2005-07-31 20:04:24 -0400417
Ryan Anderson83b24432005-07-31 04:17:25 -0400418sub make_message_id
419{
Eric Wong72095d52006-03-25 02:43:31 -0800420 my $date = time;
Ryan Anderson83b24432005-07-31 04:17:25 -0400421 my $pseudo_rand = int (rand(4200));
Ryan Anderson8037d1a2005-07-31 20:04:24 -0400422 $message_id = sprintf $message_id_template, "$date$pseudo_rand";
423 #print "new message id = $message_id\n"; # Was useful for debugging
Ryan Anderson83b24432005-07-31 04:17:25 -0400424}
425
426
427
Eric Wonga5370b12006-03-25 03:01:01 -0800428$time = time - scalar $#files;
Ryan Anderson83b24432005-07-31 04:17:25 -0400429
Jürgen Rühle374c5902007-01-10 13:36:39 -0800430sub unquote_rfc2047 {
431 local ($_) = @_;
432 if (s/=\?utf-8\?q\?(.*)\?=/$1/g) {
433 s/_/ /g;
434 s/=([0-9A-F]{2})/chr(hex($1))/eg;
435 }
Junio C Hamano3740b042007-01-30 02:22:37 -0800436 return "$_";
Jürgen Rühle374c5902007-01-10 13:36:39 -0800437}
438
Robin H. Johnson732263d2007-04-25 19:37:19 -0700439# If an address contains a . in the name portion, the name must be quoted.
440sub sanitize_address_rfc822
441{
442 my ($recipient) = @_;
443 my ($recipient_name) = ($recipient =~ /^(.*?)\s+</);
444 if ($recipient_name && $recipient_name =~ /\./ && $recipient_name !~ /^".*"$/) {
445 my ($name, $addr) = ($recipient =~ /^(.*?)(\s+<.*)/);
446 $recipient = "\"$name\"$addr";
447 }
448 return $recipient;
449}
450
Ryan Anderson83b24432005-07-31 04:17:25 -0400451sub send_message
452{
Eric Wong4bc87a22006-03-25 17:20:48 -0800453 my @recipients = unique_email_list(@to);
Robin H. Johnson732263d2007-04-25 19:37:19 -0700454 @cc = (map { sanitize_address_rfc822($_) } @cc);
Eric Wong4bc87a22006-03-25 17:20:48 -0800455 my $to = join (",\n\t", @recipients);
Ryan Anderson58063242006-05-29 12:30:13 -0700456 @recipients = unique_email_list(@recipients,@cc,@bcclist);
Robin H. Johnsonc38f0242007-04-25 19:37:20 -0700457 @recipients = (map { extract_valid_address($_) } @recipients);
Jakub Narebski6bdca892006-07-07 20:57:55 +0200458 my $date = format_2822_time($time++);
Martin Langhoffe923eff2006-05-03 09:44:36 +1200459 my $gitversion = '@@GIT_VERSION@@';
460 if ($gitversion =~ m/..GIT_VERSION../) {
Petr Baudis3cb8caf2006-07-03 22:47:58 +0200461 $gitversion = Git::version();
Martin Langhoffe923eff2006-05-03 09:44:36 +1200462 }
Ryan Anderson83b24432005-07-31 04:17:25 -0400463
Robin H. Johnsonaf068d22007-04-25 19:37:18 -0700464 my $cc = join(", ", unique_email_list(@cc));
Robin H. Johnson732263d2007-04-25 19:37:19 -0700465 $from = sanitize_address_rfc822($from);
Eric Wong4bc87a22006-03-25 17:20:48 -0800466 my $header = "From: $from
467To: $to
468Cc: $cc
469Subject: $subject
Eric Wong4bc87a22006-03-25 17:20:48 -0800470Date: $date
471Message-Id: $message_id
Martin Langhoffe923eff2006-05-03 09:44:36 +1200472X-Mailer: git-send-email $gitversion
Eric Wong4bc87a22006-03-25 17:20:48 -0800473";
Ryan Anderson7ccf7922006-05-29 12:30:12 -0700474 if ($reply_to) {
475
476 $header .= "In-Reply-To: $reply_to\n";
477 $header .= "References: $references\n";
478 }
Junio C Hamanoce91c2f2006-10-05 16:36:49 -0700479 if (@xh) {
480 $header .= join("\n", @xh) . "\n";
481 }
Ryan Anderson83b24432005-07-31 04:17:25 -0400482
Robin H. Johnsonc38f0242007-04-25 19:37:20 -0700483 my @sendmail_parameters = ('-i', @recipients);
Robin H. Johnsonf073a592007-04-25 19:37:22 -0700484 my $raw_from = $from;
485 $raw_from = $envelope_sender if (defined $envelope_sender);
486 $raw_from = extract_valid_address($raw_from);
487 unshift (@sendmail_parameters,
488 '-f', $raw_from) if(defined $envelope_sender);
Robin H. Johnson8e3d4362007-04-25 19:37:17 -0700489
Matthew Wilcox61302592006-10-10 08:58:23 -0600490 if ($dry_run) {
491 # We don't want to send the email.
492 } elsif ($smtp_server =~ m#^/#) {
Eric Wongaca7ad72006-05-15 02:34:44 -0700493 my $pid = open my $sm, '|-';
494 defined $pid or die $!;
495 if (!$pid) {
Robin H. Johnson8e3d4362007-04-25 19:37:17 -0700496 exec($smtp_server, @sendmail_parameters) or die $!;
Eric Wongaca7ad72006-05-15 02:34:44 -0700497 }
498 print $sm "$header\n$message";
499 close $sm or die $?;
500 } else {
Johannes Schindelin87840622006-06-01 00:55:47 +0200501 require Net::SMTP;
Eric Wongaca7ad72006-05-15 02:34:44 -0700502 $smtp ||= Net::SMTP->new( $smtp_server );
Robin H. Johnson2b69bfc2007-04-25 19:37:21 -0700503 $smtp->mail( $raw_from ) or die $smtp->message;
Eric Wongaca7ad72006-05-15 02:34:44 -0700504 $smtp->to( @recipients ) or die $smtp->message;
505 $smtp->data or die $smtp->message;
506 $smtp->datasend("$header\n$message") or die $smtp->message;
507 $smtp->dataend() or die $smtp->message;
508 $smtp->ok or die "Failed to send $subject\n".$smtp->message;
509 }
Ryan Anderson27184352006-02-05 20:13:52 -0500510 if ($quiet) {
Robin H. Johnson71c7da92007-04-25 19:37:16 -0700511 printf (($dry_run ? "Dry-" : "")."Sent %s\n", $subject);
Ryan Anderson27184352006-02-05 20:13:52 -0500512 } else {
Robin H. Johnson71c7da92007-04-25 19:37:16 -0700513 print (($dry_run ? "Dry-" : "")."OK. Log says:\nDate: $date\n");
Robin H. Johnson2b69bfc2007-04-25 19:37:21 -0700514 if ($smtp_server !~ m#^/#) {
Eric Wongaca7ad72006-05-15 02:34:44 -0700515 print "Server: $smtp_server\n";
Robin H. Johnson2b69bfc2007-04-25 19:37:21 -0700516 print "MAIL FROM:<$raw_from>\n";
517 print "RCPT TO:".join(',',(map { "<$_>" } @recipients))."\n";
Eric Wongaca7ad72006-05-15 02:34:44 -0700518 } else {
Robin H. Johnson8e3d4362007-04-25 19:37:17 -0700519 print "Sendmail: $smtp_server ".join(' ',@sendmail_parameters)."\n";
Eric Wongaca7ad72006-05-15 02:34:44 -0700520 }
521 print "From: $from\nSubject: $subject\nCc: $cc\nTo: $to\n\n";
522 if ($smtp) {
523 print "Result: ", $smtp->code, ' ',
524 ($smtp->message =~ /\n([^\n]+\n)$/s), "\n";
525 } else {
526 print "Result: OK\n";
527 }
Ryan Anderson30d08b32006-02-02 11:56:06 -0500528 }
Ryan Anderson83b24432005-07-31 04:17:25 -0400529}
530
Ryan Anderson83b24432005-07-31 04:17:25 -0400531$reply_to = $initial_reply_to;
Junio C Hamano2186d562006-05-29 23:53:13 -0700532$references = $initial_reply_to || '';
Ryan Anderson83b24432005-07-31 04:17:25 -0400533make_message_id();
534$subject = $initial_subject;
535
536foreach my $t (@files) {
Ryan Anderson83b24432005-07-31 04:17:25 -0400537 open(F,"<",$t) or die "can't open file $t";
538
Junio C Hamano8a8e6232006-03-23 23:43:52 -0800539 my $author_not_sender = undef;
Ryan Andersonda140f82006-02-13 03:05:15 -0500540 @cc = @initial_cc;
Junio C Hamanoce91c2f2006-10-05 16:36:49 -0700541 @xh = ();
Junio C Hamanoe6b09642006-10-07 03:09:05 -0700542 my $input_format = undef;
Ryan Anderson83b24432005-07-31 04:17:25 -0400543 my $header_done = 0;
544 $message = "";
545 while(<F>) {
546 if (!$header_done) {
Junio C Hamanoe6b09642006-10-07 03:09:05 -0700547 if (/^From /) {
548 $input_format = 'mbox';
549 next;
550 }
Ryan Anderson83b24432005-07-31 04:17:25 -0400551 chomp;
Junio C Hamanoe6b09642006-10-07 03:09:05 -0700552 if (!defined $input_format && /^[-A-Za-z]+:\s/) {
553 $input_format = 'mbox';
554 }
Ryan Anderson83b24432005-07-31 04:17:25 -0400555
Junio C Hamanoe6b09642006-10-07 03:09:05 -0700556 if (defined $input_format && $input_format eq 'mbox') {
Ryan Anderson83b24432005-07-31 04:17:25 -0400557 if (/^Subject:\s+(.*)$/) {
558 $subject = $1;
559
560 } elsif (/^(Cc|From):\s+(.*)$/) {
Junio C Hamano8a8e6232006-03-23 23:43:52 -0800561 if ($2 eq $from) {
562 next if ($suppress_from);
563 }
Haavard Skinnemoen68d42c42006-08-23 03:02:59 -0700564 elsif ($1 eq 'From') {
Junio C Hamano8a8e6232006-03-23 23:43:52 -0800565 $author_not_sender = $2;
566 }
Ryan Anderson83b24432005-07-31 04:17:25 -0400567 printf("(mbox) Adding cc: %s from line '%s'\n",
Ryan Anderson27184352006-02-05 20:13:52 -0500568 $2, $_) unless $quiet;
Ryan Anderson83b24432005-07-31 04:17:25 -0400569 push @cc, $2;
570 }
Eric Wong1d6a0032006-10-23 00:46:37 -0700571 elsif (!/^Date:\s/ && /^[-A-Za-z]+:\s+\S/) {
Junio C Hamanoce91c2f2006-10-05 16:36:49 -0700572 push @xh, $_;
573 }
Ryan Anderson83b24432005-07-31 04:17:25 -0400574
575 } else {
576 # In the traditional
577 # "send lots of email" format,
578 # line 1 = cc
579 # line 2 = subject
580 # So let's support that, too.
Junio C Hamanoe6b09642006-10-07 03:09:05 -0700581 $input_format = 'lots';
Ryan Anderson83b24432005-07-31 04:17:25 -0400582 if (@cc == 0) {
583 printf("(non-mbox) Adding cc: %s from line '%s'\n",
Ryan Anderson27184352006-02-05 20:13:52 -0500584 $_, $_) unless $quiet;
Ryan Anderson83b24432005-07-31 04:17:25 -0400585
586 push @cc, $_;
587
588 } elsif (!defined $subject) {
589 $subject = $_;
590 }
591 }
Junio C Hamano5825e5b2005-07-31 23:05:16 -0700592
Ryan Anderson83b24432005-07-31 04:17:25 -0400593 # A whitespace line will terminate the headers
594 if (m/^\s*$/) {
595 $header_done = 1;
596 }
597 } else {
598 $message .= $_;
J. Bruce Fieldsabec1002007-03-18 21:37:53 -0400599 if (/^(Signed-off-by|Cc): (.*)$/i && !$no_signed_off_cc) {
600 my $c = $2;
Ryan Anderson83b24432005-07-31 04:17:25 -0400601 chomp $c;
602 push @cc, $c;
603 printf("(sob) Adding cc: %s from line '%s'\n",
Ryan Anderson27184352006-02-05 20:13:52 -0500604 $c, $_) unless $quiet;
Ryan Anderson83b24432005-07-31 04:17:25 -0400605 }
606 }
607 }
608 close F;
Junio C Hamano8a8e6232006-03-23 23:43:52 -0800609 if (defined $author_not_sender) {
Jürgen Rühle374c5902007-01-10 13:36:39 -0800610 $author_not_sender = unquote_rfc2047($author_not_sender);
Junio C Hamano8a8e6232006-03-23 23:43:52 -0800611 $message = "From: $author_not_sender\n\n$message";
612 }
Ryan Anderson83b24432005-07-31 04:17:25 -0400613
Ryan Anderson83b24432005-07-31 04:17:25 -0400614
615 send_message();
616
617 # set up for the next message
Junio C Hamanobc108f62006-10-05 16:36:15 -0700618 if ($chain_reply_to || !defined $reply_to || length($reply_to) == 0) {
Ryan Anderson78488b22005-07-31 20:04:24 -0400619 $reply_to = $message_id;
Ryan Anderson7ccf7922006-05-29 12:30:12 -0700620 if (length $references > 0) {
YOSHIFUJI Hideaki / 吉藤英明a925b892007-04-06 08:50:24 +0900621 $references .= "\n $message_id";
Ryan Anderson7ccf7922006-05-29 12:30:12 -0700622 } else {
623 $references = "$message_id";
624 }
Ryan Anderson78488b22005-07-31 20:04:24 -0400625 }
Ryan Anderson83b24432005-07-31 04:17:25 -0400626 make_message_id();
Ryan Anderson83b24432005-07-31 04:17:25 -0400627}
Ryan Andersone2057352005-08-02 21:45:22 -0400628
Ryan Anderson1f038a02005-09-05 01:13:07 -0400629if ($compose) {
630 cleanup_compose_files();
631}
632
633sub cleanup_compose_files() {
634 unlink($compose_filename, $compose_filename . ".final");
635
636}
637
Eric Wong4bc87a22006-03-25 17:20:48 -0800638$smtp->quit if $smtp;
Ryan Andersone2057352005-08-02 21:45:22 -0400639
640sub unique_email_list(@) {
641 my %seen;
642 my @emails;
643
644 foreach my $entry (@_) {
Eric Wongdb3106b2006-05-15 02:41:01 -0700645 if (my $clean = extract_valid_address($entry)) {
646 $seen{$clean} ||= 0;
647 next if $seen{$clean}++;
648 push @emails, $entry;
649 } else {
650 print STDERR "W: unable to extract a valid address",
651 " from: $entry\n";
652 }
Ryan Andersone2057352005-08-02 21:45:22 -0400653 }
654 return @emails;
655}