]>
Commit | Line | Data |
---|---|---|
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 | |
19 | use strict; | |
20 | use warnings; | |
21 | use Term::ReadLine; | |
83b24437 RA |
22 | use Getopt::Long; |
23 | use Data::Dumper; | |
3cb8caf7 | 24 | use Git; |
83b24437 | 25 | |
280242d1 JH |
26 | package FakeTerm; |
27 | sub new { | |
28 | my ($class, $reason) = @_; | |
29 | return bless \$reason, shift; | |
30 | } | |
31 | sub readline { | |
32 | my $self = shift; | |
33 | die "Cannot use readline on FakeTerm: $$self"; | |
34 | } | |
35 | package main; | |
36 | ||
1b0baf14 MC |
37 | |
38 | sub usage { | |
39 | print <<EOT; | |
40 | git-send-email [options] <file | directory>... | |
41 | Options: | |
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 | ||
ef0c2abf AR |
52 | --compose Use \$GIT_EDITOR, core.editor, \$EDITOR, or \$VISUAL to edit |
53 | an introductory message for the patch series. | |
1b0baf14 MC |
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 | ||
5483c71d AR |
67 | --signed-off-cc Automatically add email addresses that appear in |
68 | Signed-off-by: or Cc: lines to the cc: list. Defaults to on. | |
1b0baf14 MC |
69 | |
70 | --smtp-server If set, specifies the outgoing SMTP server to use. | |
71 | Defaults to localhost. | |
72 | ||
73 | --suppress-from Suppress sending emails to yourself if your address | |
5483c71d | 74 | appears in a From: line. Defaults to off. |
1b0baf14 | 75 | |
5483c71d | 76 | --thread Specify that the "In-Reply-To:" header should be set on all |
e46f7a0e AR |
77 | emails. Defaults to on. |
78 | ||
1b0baf14 MC |
79 | --quiet Make git-send-email less verbose. One line per email |
80 | should be all that is output. | |
81 | ||
238cc635 RJ |
82 | --dry-run Do everything except actually send the emails. |
83 | ||
f073a592 RJ |
84 | --envelope-sender Specify the envelope sender used to send the emails. |
85 | ||
1b0baf14 MC |
86 | EOT |
87 | exit(1); | |
88 | } | |
89 | ||
4bc87a28 | 90 | # most mail servers generate the Date: header, but not all... |
6bdca890 JN |
91 | sub format_2822_time { |
92 | my ($time) = @_; | |
93 | my @localtm = localtime($time); | |
94 | my @gmttm = gmtime($time); | |
95 | my $localmin = $localtm[1] + $localtm[2] * 60; | |
96 | my $gmtmin = $gmttm[1] + $gmttm[2] * 60; | |
97 | if ($localtm[0] != $gmttm[0]) { | |
98 | die "local zone differs from GMT by a non-minute interval\n"; | |
99 | } | |
100 | if ((($gmttm[6] + 1) % 7) == $localtm[6]) { | |
101 | $localmin += 1440; | |
102 | } elsif ((($gmttm[6] - 1) % 7) == $localtm[6]) { | |
103 | $localmin -= 1440; | |
104 | } elsif ($gmttm[6] != $localtm[6]) { | |
105 | die "local time offset greater than or equal to 24 hours\n"; | |
106 | } | |
107 | my $offset = $localmin - $gmtmin; | |
108 | my $offhour = $offset / 60; | |
109 | my $offmin = abs($offset % 60); | |
110 | if (abs($offhour) >= 24) { | |
111 | die ("local time offset greater than or equal to 24 hours\n"); | |
112 | } | |
113 | ||
114 | return sprintf("%s, %2d %s %d %02d:%02d:%02d %s%02d%02d", | |
115 | qw(Sun Mon Tue Wed Thu Fri Sat)[$localtm[6]], | |
116 | $localtm[3], | |
117 | qw(Jan Feb Mar Apr May Jun | |
118 | Jul Aug Sep Oct Nov Dec)[$localtm[4]], | |
119 | $localtm[5]+1900, | |
120 | $localtm[2], | |
121 | $localtm[1], | |
122 | $localtm[0], | |
123 | ($offset >= 0) ? '+' : '-', | |
124 | abs($offhour), | |
125 | $offmin, | |
126 | ); | |
127 | } | |
4bc87a28 | 128 | |
567ffeb7 | 129 | my $have_email_valid = eval { require Email::Valid; 1 }; |
4bc87a28 EW |
130 | my $smtp; |
131 | ||
e205735d | 132 | sub unique_email_list(@); |
1f038a0c RA |
133 | sub cleanup_compose_files(); |
134 | ||
135 | # Constants (essentially) | |
136 | my $compose_filename = ".msg.$$"; | |
e205735d | 137 | |
83b24437 | 138 | # Variables we fill in automatically, or via prompting: |
ce91c2f6 | 139 | my (@to,@cc,@initial_cc,@bcclist,@xh, |
58063245 | 140 | $initial_reply_to,$initial_subject,@files,$from,$compose,$time); |
83b24437 | 141 | |
aca7ad76 | 142 | my $smtp_server; |
f073a592 | 143 | my $envelope_sender; |
78488b2c | 144 | |
9133261f | 145 | # Example reply to: |
83b24437 | 146 | #$initial_reply_to = ''; #<20050203173208.GA23964@foobar.com>'; |
83b24437 | 147 | |
3cb8caf7 | 148 | my $repo = Git->repository(); |
280242d1 JH |
149 | my $term = eval { |
150 | new Term::ReadLine 'git-send-email'; | |
151 | }; | |
152 | if ($@) { | |
153 | $term = new FakeTerm "$@: going non-interactive"; | |
154 | } | |
83b24437 | 155 | |
5483c71d AR |
156 | # Behavior modification variables |
157 | my ($quiet, $dry_run) = (0, 0); | |
158 | ||
159 | # Variables with corresponding config settings | |
160 | my ($thread, $chain_reply_to, $suppress_from, $signed_off_cc); | |
161 | ||
e46f7a0e | 162 | my %config_settings = ( |
5483c71d AR |
163 | "thread" => [\$thread, 1], |
164 | "chainreplyto" => [\$chain_reply_to, 1], | |
165 | "suppressfrom" => [\$suppress_from, 0], | |
166 | "signedoffcc" => [\$signed_off_cc, 1], | |
e46f7a0e AR |
167 | ); |
168 | ||
169 | foreach my $setting (keys %config_settings) { | |
5483c71d AR |
170 | my $config = $repo->config_bool("sendemail.$setting"); |
171 | ${$config_settings{$setting}->[0]} = (defined $config) ? $config : $config_settings{$setting}->[1]; | |
4a62d3f5 AK |
172 | } |
173 | ||
174 | @bcclist = $repo->config('sendemail.bcc'); | |
175 | if (!@bcclist or !$bcclist[0]) { | |
176 | @bcclist = (); | |
177 | } | |
178 | ||
83b24437 RA |
179 | # Begin by accumulating all the variables (defined above), that we will end up |
180 | # needing, first, from the command line: | |
181 | ||
182 | my $rc = GetOptions("from=s" => \$from, | |
183 | "in-reply-to=s" => \$initial_reply_to, | |
184 | "subject=s" => \$initial_subject, | |
185 | "to=s" => \@to, | |
da140f8b | 186 | "cc=s" => \@initial_cc, |
58063245 | 187 | "bcc=s" => \@bcclist, |
78488b2c | 188 | "chain-reply-to!" => \$chain_reply_to, |
3342d850 | 189 | "smtp-server=s" => \$smtp_server, |
1f038a0c | 190 | "compose" => \$compose, |
30d08b34 | 191 | "quiet" => \$quiet, |
5483c71d AR |
192 | "suppress-from!" => \$suppress_from, |
193 | "signed-off-cc|signed-off-by-cc!" => \$signed_off_cc, | |
6130259c | 194 | "dry-run" => \$dry_run, |
f073a592 | 195 | "envelope-sender=s" => \$envelope_sender, |
5483c71d | 196 | "thread!" => \$thread, |
83b24437 RA |
197 | ); |
198 | ||
1b0baf14 MC |
199 | unless ($rc) { |
200 | usage(); | |
201 | } | |
202 | ||
79ee555b EB |
203 | # Verify the user input |
204 | ||
205 | foreach my $entry (@to) { | |
206 | die "Comma in --to entry: $entry'\n" unless $entry !~ m/,/; | |
207 | } | |
208 | ||
209 | foreach my $entry (@initial_cc) { | |
210 | die "Comma in --cc entry: $entry'\n" unless $entry !~ m/,/; | |
211 | } | |
212 | ||
213 | foreach my $entry (@bcclist) { | |
214 | die "Comma in --bcclist entry: $entry'\n" unless $entry !~ m/,/; | |
215 | } | |
216 | ||
83b24437 RA |
217 | # Now, let's fill any that aren't set in with defaults: |
218 | ||
c7a30e56 PB |
219 | my ($author) = $repo->ident_person('author'); |
220 | my ($committer) = $repo->ident_person('committer'); | |
83b24437 | 221 | |
994d6c66 | 222 | my %aliases; |
3cb8caf7 PB |
223 | my @alias_files = $repo->config('sendemail.aliasesfile'); |
224 | my $aliasfiletype = $repo->config('sendemail.aliasfiletype'); | |
994d6c66 EW |
225 | my %parse_alias = ( |
226 | # multiline formats can be supported in the future | |
227 | mutt => sub { my $fh = shift; while (<$fh>) { | |
504ceab6 | 228 | if (/^\s*alias\s+(\S+)\s+(.*)$/) { |
994d6c66 EW |
229 | my ($alias, $addr) = ($1, $2); |
230 | $addr =~ s/#.*$//; # mutt allows # comments | |
231 | # commas delimit multiple addresses | |
232 | $aliases{$alias} = [ split(/\s*,\s*/, $addr) ]; | |
233 | }}}, | |
234 | mailrc => sub { my $fh = shift; while (<$fh>) { | |
235 | if (/^alias\s+(\S+)\s+(.*)$/) { | |
236 | # spaces delimit multiple addresses | |
237 | $aliases{$1} = [ split(/\s+/, $2) ]; | |
238 | }}}, | |
239 | pine => sub { my $fh = shift; while (<$fh>) { | |
2d8ae400 | 240 | if (/^(\S+)\t.*\t(.*)$/) { |
994d6c66 EW |
241 | $aliases{$1} = [ split(/\s*,\s*/, $2) ]; |
242 | }}}, | |
243 | gnus => sub { my $fh = shift; while (<$fh>) { | |
244 | if (/\(define-mail-alias\s+"(\S+?)"\s+"(\S+?)"\)/) { | |
245 | $aliases{$1} = [ $2 ]; | |
246 | }}} | |
247 | ); | |
248 | ||
3cb8caf7 | 249 | if (@alias_files and $aliasfiletype and defined $parse_alias{$aliasfiletype}) { |
994d6c66 EW |
250 | foreach my $file (@alias_files) { |
251 | open my $fh, '<', $file or die "opening $file: $!\n"; | |
252 | $parse_alias{$aliasfiletype}->($fh); | |
253 | close $fh; | |
254 | } | |
255 | } | |
256 | ||
ae740a58 MH |
257 | ($from) = expand_aliases($from) if defined $from; |
258 | ||
1f038a0c | 259 | my $prompting = 0; |
83b24437 RA |
260 | if (!defined $from) { |
261 | $from = $author || $committer; | |
8037d1a3 | 262 | do { |
2ef58055 | 263 | $_ = $term->readline("Who should the emails appear to be from? [$from] "); |
ca9a7d65 | 264 | } while (!defined $_); |
8037d1a3 | 265 | |
2ef58055 | 266 | $from = $_ if ($_); |
83b24437 | 267 | print "Emails will be sent from: ", $from, "\n"; |
1f038a0c | 268 | $prompting++; |
83b24437 RA |
269 | } |
270 | ||
271 | if (!@to) { | |
8037d1a3 | 272 | do { |
5825e5b2 | 273 | $_ = $term->readline("Who should the emails be sent to? ", |
8037d1a3 RA |
274 | ""); |
275 | } while (!defined $_); | |
83b24437 RA |
276 | my $to = $_; |
277 | push @to, split /,/, $to; | |
1f038a0c | 278 | $prompting++; |
83b24437 RA |
279 | } |
280 | ||
994d6c66 EW |
281 | sub expand_aliases { |
282 | my @cur = @_; | |
283 | my @last; | |
284 | do { | |
285 | @last = @cur; | |
286 | @cur = map { $aliases{$_} ? @{$aliases{$_}} : $_ } @last; | |
287 | } while (join(',',@cur) ne join(',',@last)); | |
288 | return @cur; | |
289 | } | |
290 | ||
291 | @to = expand_aliases(@to); | |
bf7af116 | 292 | @to = (map { sanitize_address_rfc822($_) } @to); |
994d6c66 | 293 | @initial_cc = expand_aliases(@initial_cc); |
58063245 | 294 | @bcclist = expand_aliases(@bcclist); |
994d6c66 | 295 | |
1f038a0c | 296 | if (!defined $initial_subject && $compose) { |
8037d1a3 | 297 | do { |
5825e5b2 | 298 | $_ = $term->readline("What subject should the emails start with? ", |
8037d1a3 RA |
299 | $initial_subject); |
300 | } while (!defined $_); | |
83b24437 | 301 | $initial_subject = $_; |
1f038a0c | 302 | $prompting++; |
83b24437 RA |
303 | } |
304 | ||
5483c71d | 305 | if ($thread && !defined $initial_reply_to && $prompting) { |
8037d1a3 | 306 | do { |
1f038a0c | 307 | $_= $term->readline("Message-ID to be used as In-Reply-To for the first email? ", |
8037d1a3 RA |
308 | $initial_reply_to); |
309 | } while (!defined $_); | |
310 | ||
83b24437 | 311 | $initial_reply_to = $_; |
78488b2c | 312 | $initial_reply_to =~ s/(^\s+|\s+$)//g; |
83b24437 RA |
313 | } |
314 | ||
6dcfa306 SV |
315 | if (!$smtp_server) { |
316 | $smtp_server = $repo->config('sendemail.smtpserver'); | |
317 | } | |
aca7ad76 EW |
318 | if (!$smtp_server) { |
319 | foreach (qw( /usr/sbin/sendmail /usr/lib/sendmail )) { | |
320 | if (-x $_) { | |
321 | $smtp_server = $_; | |
322 | last; | |
323 | } | |
324 | } | |
325 | $smtp_server ||= 'localhost'; # could be 127.0.0.1, too... *shrug* | |
3342d850 RA |
326 | } |
327 | ||
1f038a0c RA |
328 | if ($compose) { |
329 | # Note that this does not need to be secure, but we will make a small | |
330 | # effort to have it be unique | |
331 | open(C,">",$compose_filename) | |
332 | or die "Failed to open for writing $compose_filename: $!"; | |
d366c703 | 333 | print C "From $from # This line is ignored.\n"; |
1f038a0c RA |
334 | printf C "Subject: %s\n\n", $initial_subject; |
335 | printf C <<EOT; | |
336 | GIT: Please enter your email below. | |
337 | GIT: Lines beginning in "GIT: " will be removed. | |
338 | GIT: Consider including an overall diffstat or table of contents | |
339 | GIT: for the patch you are writing. | |
340 | ||
341 | EOT | |
342 | close(C); | |
343 | ||
ef0c2abf | 344 | my $editor = $ENV{GIT_EDITOR} || $repo->config("core.editor") || $ENV{VISUAL} || $ENV{EDITOR} || "vi"; |
1f038a0c RA |
345 | system($editor, $compose_filename); |
346 | ||
347 | open(C2,">",$compose_filename . ".final") | |
348 | or die "Failed to open $compose_filename.final : " . $!; | |
349 | ||
350 | open(C,"<",$compose_filename) | |
351 | or die "Failed to open $compose_filename : " . $!; | |
352 | ||
353 | while(<C>) { | |
354 | next if m/^GIT: /; | |
355 | print C2 $_; | |
356 | } | |
357 | close(C); | |
358 | close(C2); | |
359 | ||
360 | do { | |
361 | $_ = $term->readline("Send this email? (y|n) "); | |
362 | } while (!defined $_); | |
363 | ||
364 | if (uc substr($_,0,1) ne 'Y') { | |
365 | cleanup_compose_files(); | |
366 | exit(0); | |
367 | } | |
368 | ||
369 | @files = ($compose_filename . ".final"); | |
370 | } | |
371 | ||
372 | ||
83b24437 RA |
373 | # Now that all the defaults are set, process the rest of the command line |
374 | # arguments and collect up the files that need to be processed. | |
375 | for my $f (@ARGV) { | |
376 | if (-d $f) { | |
377 | opendir(DH,$f) | |
378 | or die "Failed to opendir $f: $!"; | |
379 | ||
5825e5b2 | 380 | push @files, grep { -f $_ } map { +$f . "/" . $_ } |
8037d1a3 RA |
381 | sort readdir(DH); |
382 | ||
83b24437 RA |
383 | } elsif (-f $f) { |
384 | push @files, $f; | |
385 | ||
386 | } else { | |
387 | print STDERR "Skipping $f - not found.\n"; | |
388 | } | |
389 | } | |
390 | ||
391 | if (@files) { | |
2718435b RA |
392 | unless ($quiet) { |
393 | print $_,"\n" for (@files); | |
394 | } | |
83b24437 | 395 | } else { |
1b0baf14 MC |
396 | print STDERR "\nNo patch files specified!\n\n"; |
397 | usage(); | |
83b24437 RA |
398 | } |
399 | ||
400 | # Variables we set as part of the loop over files | |
af068d27 | 401 | our ($message_id, %mail, $subject, $reply_to, $references, $message); |
83b24437 | 402 | |
567ffeb7 EW |
403 | sub extract_valid_address { |
404 | my $address = shift; | |
ad9c18f5 | 405 | my $local_part_regexp = '[^<>"\s@]+'; |
09302e17 | 406 | my $domain_regexp = '[^.<>"\s@]+(?:\.[^.<>"\s@]+)+'; |
db3106b2 EW |
407 | |
408 | # check for a local address: | |
ad9c18f5 | 409 | return $address if ($address =~ /^($local_part_regexp)$/); |
db3106b2 | 410 | |
567ffeb7 | 411 | if ($have_email_valid) { |
689b4d55 | 412 | $address =~ s/^\s*<(.*)>\s*$/$1/; |
ad9c18f5 | 413 | return scalar Email::Valid->address($address); |
567ffeb7 EW |
414 | } else { |
415 | # less robust/correct than the monster regexp in Email::Valid, | |
416 | # but still does a 99% job, and one less dependency | |
ad9c18f5 | 417 | $address =~ /($local_part_regexp\@$domain_regexp)/; |
e96fd305 | 418 | return $1; |
567ffeb7 EW |
419 | } |
420 | } | |
83b24437 RA |
421 | |
422 | # Usually don't need to change anything below here. | |
423 | ||
424 | # we make a "fake" message id by taking the current number | |
425 | # of seconds since the beginning of Unix time and tacking on | |
426 | # a random number to the end, in case we are called quicker than | |
427 | # 1 second since the last time we were called. | |
8037d1a3 RA |
428 | |
429 | # We'll setup a template for the message id, using the "from" address: | |
8037d1a3 | 430 | |
83b24437 RA |
431 | sub make_message_id |
432 | { | |
72095d5c | 433 | my $date = time; |
83b24437 | 434 | my $pseudo_rand = int (rand(4200)); |
aeb59328 JH |
435 | my $du_part; |
436 | for ($from, $committer, $author) { | |
437 | $du_part = extract_valid_address($_); | |
438 | last if ($du_part ne ''); | |
439 | } | |
440 | if ($du_part eq '') { | |
441 | use Sys::Hostname qw(); | |
442 | $du_part = 'user@' . Sys::Hostname::hostname(); | |
443 | } | |
444 | my $message_id_template = "<%s-git-send-email-$du_part>"; | |
8037d1a3 RA |
445 | $message_id = sprintf $message_id_template, "$date$pseudo_rand"; |
446 | #print "new message id = $message_id\n"; # Was useful for debugging | |
83b24437 RA |
447 | } |
448 | ||
449 | ||
450 | ||
a5370b16 | 451 | $time = time - scalar $#files; |
83b24437 | 452 | |
374c5905 JR |
453 | sub unquote_rfc2047 { |
454 | local ($_) = @_; | |
455 | if (s/=\?utf-8\?q\?(.*)\?=/$1/g) { | |
456 | s/_/ /g; | |
457 | s/=([0-9A-F]{2})/chr(hex($1))/eg; | |
458 | } | |
3740b04f | 459 | return "$_"; |
374c5905 JR |
460 | } |
461 | ||
732263d4 RJ |
462 | # If an address contains a . in the name portion, the name must be quoted. |
463 | sub sanitize_address_rfc822 | |
464 | { | |
465 | my ($recipient) = @_; | |
466 | my ($recipient_name) = ($recipient =~ /^(.*?)\s+</); | |
467 | if ($recipient_name && $recipient_name =~ /\./ && $recipient_name !~ /^".*"$/) { | |
468 | my ($name, $addr) = ($recipient =~ /^(.*?)(\s+<.*)/); | |
469 | $recipient = "\"$name\"$addr"; | |
470 | } | |
471 | return $recipient; | |
472 | } | |
473 | ||
83b24437 RA |
474 | sub send_message |
475 | { | |
4bc87a28 | 476 | my @recipients = unique_email_list(@to); |
732263d4 | 477 | @cc = (map { sanitize_address_rfc822($_) } @cc); |
4bc87a28 | 478 | my $to = join (",\n\t", @recipients); |
58063245 | 479 | @recipients = unique_email_list(@recipients,@cc,@bcclist); |
c38f0247 | 480 | @recipients = (map { extract_valid_address($_) } @recipients); |
6bdca890 | 481 | my $date = format_2822_time($time++); |
e923effb ML |
482 | my $gitversion = '@@GIT_VERSION@@'; |
483 | if ($gitversion =~ m/..GIT_VERSION../) { | |
3cb8caf7 | 484 | $gitversion = Git::version(); |
e923effb | 485 | } |
4bc87a28 | 486 | |
af068d27 | 487 | my $cc = join(", ", unique_email_list(@cc)); |
f06a6a49 JH |
488 | my $ccline = ""; |
489 | if ($cc ne '') { | |
490 | $ccline = "\nCc: $cc"; | |
491 | } | |
732263d4 | 492 | $from = sanitize_address_rfc822($from); |
aeb59328 JH |
493 | make_message_id(); |
494 | ||
4bc87a28 | 495 | my $header = "From: $from |
f06a6a49 | 496 | To: $to${ccline} |
4bc87a28 | 497 | Subject: $subject |
4bc87a28 EW |
498 | Date: $date |
499 | Message-Id: $message_id | |
e923effb | 500 | X-Mailer: git-send-email $gitversion |
4bc87a28 | 501 | "; |
5483c71d | 502 | if ($thread && $reply_to) { |
7ccf7927 RA |
503 | |
504 | $header .= "In-Reply-To: $reply_to\n"; | |
505 | $header .= "References: $references\n"; | |
506 | } | |
ce91c2f6 JH |
507 | if (@xh) { |
508 | $header .= join("\n", @xh) . "\n"; | |
509 | } | |
4bc87a28 | 510 | |
c38f0247 | 511 | my @sendmail_parameters = ('-i', @recipients); |
f073a592 RJ |
512 | my $raw_from = $from; |
513 | $raw_from = $envelope_sender if (defined $envelope_sender); | |
514 | $raw_from = extract_valid_address($raw_from); | |
515 | unshift (@sendmail_parameters, | |
516 | '-f', $raw_from) if(defined $envelope_sender); | |
8e3d436b | 517 | |
6130259c MW |
518 | if ($dry_run) { |
519 | # We don't want to send the email. | |
520 | } elsif ($smtp_server =~ m#^/#) { | |
aca7ad76 EW |
521 | my $pid = open my $sm, '|-'; |
522 | defined $pid or die $!; | |
523 | if (!$pid) { | |
8e3d436b | 524 | exec($smtp_server, @sendmail_parameters) or die $!; |
aca7ad76 EW |
525 | } |
526 | print $sm "$header\n$message"; | |
527 | close $sm or die $?; | |
528 | } else { | |
87840620 | 529 | require Net::SMTP; |
aca7ad76 | 530 | $smtp ||= Net::SMTP->new( $smtp_server ); |
2b69bfc2 | 531 | $smtp->mail( $raw_from ) or die $smtp->message; |
aca7ad76 EW |
532 | $smtp->to( @recipients ) or die $smtp->message; |
533 | $smtp->data or die $smtp->message; | |
534 | $smtp->datasend("$header\n$message") or die $smtp->message; | |
535 | $smtp->dataend() or die $smtp->message; | |
536 | $smtp->ok or die "Failed to send $subject\n".$smtp->message; | |
537 | } | |
2718435b | 538 | if ($quiet) { |
71c7da94 | 539 | printf (($dry_run ? "Dry-" : "")."Sent %s\n", $subject); |
2718435b | 540 | } else { |
71c7da94 | 541 | print (($dry_run ? "Dry-" : "")."OK. Log says:\nDate: $date\n"); |
2b69bfc2 | 542 | if ($smtp_server !~ m#^/#) { |
aca7ad76 | 543 | print "Server: $smtp_server\n"; |
2b69bfc2 RJ |
544 | print "MAIL FROM:<$raw_from>\n"; |
545 | print "RCPT TO:".join(',',(map { "<$_>" } @recipients))."\n"; | |
aca7ad76 | 546 | } else { |
8e3d436b | 547 | print "Sendmail: $smtp_server ".join(' ',@sendmail_parameters)."\n"; |
aca7ad76 EW |
548 | } |
549 | print "From: $from\nSubject: $subject\nCc: $cc\nTo: $to\n\n"; | |
550 | if ($smtp) { | |
551 | print "Result: ", $smtp->code, ' ', | |
552 | ($smtp->message =~ /\n([^\n]+\n)$/s), "\n"; | |
553 | } else { | |
554 | print "Result: OK\n"; | |
555 | } | |
30d08b34 | 556 | } |
83b24437 RA |
557 | } |
558 | ||
83b24437 | 559 | $reply_to = $initial_reply_to; |
2186d566 | 560 | $references = $initial_reply_to || ''; |
83b24437 RA |
561 | $subject = $initial_subject; |
562 | ||
563 | foreach my $t (@files) { | |
83b24437 RA |
564 | open(F,"<",$t) or die "can't open file $t"; |
565 | ||
8a8e6235 | 566 | my $author_not_sender = undef; |
da140f8b | 567 | @cc = @initial_cc; |
ce91c2f6 | 568 | @xh = (); |
e6b0964a | 569 | my $input_format = undef; |
83b24437 RA |
570 | my $header_done = 0; |
571 | $message = ""; | |
572 | while(<F>) { | |
573 | if (!$header_done) { | |
e6b0964a JH |
574 | if (/^From /) { |
575 | $input_format = 'mbox'; | |
576 | next; | |
577 | } | |
83b24437 | 578 | chomp; |
e6b0964a JH |
579 | if (!defined $input_format && /^[-A-Za-z]+:\s/) { |
580 | $input_format = 'mbox'; | |
581 | } | |
83b24437 | 582 | |
e6b0964a | 583 | if (defined $input_format && $input_format eq 'mbox') { |
83b24437 RA |
584 | if (/^Subject:\s+(.*)$/) { |
585 | $subject = $1; | |
586 | ||
587 | } elsif (/^(Cc|From):\s+(.*)$/) { | |
2cf69cf6 KH |
588 | if (unquote_rfc2047($2) eq $from) { |
589 | $from = $2; | |
8a8e6235 JH |
590 | next if ($suppress_from); |
591 | } | |
68d42c41 | 592 | elsif ($1 eq 'From') { |
8a8e6235 JH |
593 | $author_not_sender = $2; |
594 | } | |
83b24437 | 595 | printf("(mbox) Adding cc: %s from line '%s'\n", |
2718435b | 596 | $2, $_) unless $quiet; |
83b24437 RA |
597 | push @cc, $2; |
598 | } | |
1d6a003a | 599 | elsif (!/^Date:\s/ && /^[-A-Za-z]+:\s+\S/) { |
ce91c2f6 JH |
600 | push @xh, $_; |
601 | } | |
83b24437 RA |
602 | |
603 | } else { | |
604 | # In the traditional | |
605 | # "send lots of email" format, | |
606 | # line 1 = cc | |
607 | # line 2 = subject | |
608 | # So let's support that, too. | |
e6b0964a | 609 | $input_format = 'lots'; |
83b24437 RA |
610 | if (@cc == 0) { |
611 | printf("(non-mbox) Adding cc: %s from line '%s'\n", | |
2718435b | 612 | $_, $_) unless $quiet; |
83b24437 RA |
613 | |
614 | push @cc, $_; | |
615 | ||
616 | } elsif (!defined $subject) { | |
617 | $subject = $_; | |
618 | } | |
619 | } | |
5825e5b2 | 620 | |
83b24437 RA |
621 | # A whitespace line will terminate the headers |
622 | if (m/^\s*$/) { | |
623 | $header_done = 1; | |
624 | } | |
625 | } else { | |
626 | $message .= $_; | |
5483c71d | 627 | if (/^(Signed-off-by|Cc): (.*)$/i && $signed_off_cc) { |
abec100c | 628 | my $c = $2; |
83b24437 RA |
629 | chomp $c; |
630 | push @cc, $c; | |
631 | printf("(sob) Adding cc: %s from line '%s'\n", | |
2718435b | 632 | $c, $_) unless $quiet; |
83b24437 RA |
633 | } |
634 | } | |
635 | } | |
636 | close F; | |
8a8e6235 | 637 | if (defined $author_not_sender) { |
374c5905 | 638 | $author_not_sender = unquote_rfc2047($author_not_sender); |
8a8e6235 JH |
639 | $message = "From: $author_not_sender\n\n$message"; |
640 | } | |
83b24437 | 641 | |
83b24437 RA |
642 | |
643 | send_message(); | |
644 | ||
645 | # set up for the next message | |
bc108f63 | 646 | if ($chain_reply_to || !defined $reply_to || length($reply_to) == 0) { |
78488b2c | 647 | $reply_to = $message_id; |
7ccf7927 | 648 | if (length $references > 0) { |
a925b89c | 649 | $references .= "\n $message_id"; |
7ccf7927 RA |
650 | } else { |
651 | $references = "$message_id"; | |
652 | } | |
78488b2c | 653 | } |
83b24437 | 654 | } |
e205735d | 655 | |
1f038a0c RA |
656 | if ($compose) { |
657 | cleanup_compose_files(); | |
658 | } | |
659 | ||
660 | sub cleanup_compose_files() { | |
661 | unlink($compose_filename, $compose_filename . ".final"); | |
662 | ||
663 | } | |
664 | ||
4bc87a28 | 665 | $smtp->quit if $smtp; |
e205735d RA |
666 | |
667 | sub unique_email_list(@) { | |
668 | my %seen; | |
669 | my @emails; | |
670 | ||
671 | foreach my $entry (@_) { | |
db3106b2 EW |
672 | if (my $clean = extract_valid_address($entry)) { |
673 | $seen{$clean} ||= 0; | |
674 | next if $seen{$clean}++; | |
675 | push @emails, $entry; | |
676 | } else { | |
677 | print STDERR "W: unable to extract a valid address", | |
678 | " from: $entry\n"; | |
679 | } | |
e205735d RA |
680 | } |
681 | return @emails; | |
682 | } |