3 # Copyright 2002,2005 Greg Kroah-Hartman <greg@kroah.com>
4 # Copyright 2005 Ryan Anderson <ryan@michonline.com>
8 # Ported to support git "mbox" format files by Ryan Anderson <ryan@michonline.com>
10 # Sends a collection of emails to the given email addresses, disturbingly fast.
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:
15 # first line of the message is who to CC,
16 # and second line is the subject of the message.
27 my ($class, $reason) = @_;
28 return bless \
$reason, shift;
32 die "Cannot use readline on FakeTerm: $$self";
36 # most mail servers generate the Date: header, but not all...
38 use POSIX qw
/strftime/;
40 my $have_email_valid = eval { require Email
::Valid
; 1 };
43 sub unique_email_list
(@
);
44 sub cleanup_compose_files
();
46 # Constants (essentially)
47 my $compose_filename = ".msg.$$";
49 # Variables we fill in automatically, or via prompting:
50 my (@to,@cc,@initial_cc,@bcclist,
51 $initial_reply_to,$initial_subject,@files,$from,$compose,$time);
53 # Behavior modification variables
54 my ($chain_reply_to, $quiet, $suppress_from, $no_signed_off_cc) = (1, 0, 0, 0);
58 #$initial_reply_to = ''; #<20050203173208.GA23964@foobar.com>';
61 new Term
::ReadLine
'git-send-email';
64 $term = new FakeTerm
"$@: going non-interactive";
67 # Begin by accumulating all the variables (defined above), that we will end up
68 # needing, first, from the command line:
70 my $rc = GetOptions
("from=s" => \
$from,
71 "in-reply-to=s" => \
$initial_reply_to,
72 "subject=s" => \
$initial_subject,
74 "cc=s" => \
@initial_cc,
76 "chain-reply-to!" => \
$chain_reply_to,
77 "smtp-server=s" => \
$smtp_server,
78 "compose" => \
$compose,
80 "suppress-from" => \
$suppress_from,
81 "no-signed-off-cc|no-signed-off-by-cc" => \
$no_signed_off_cc,
84 # Verify the user input
86 foreach my $entry (@to) {
87 die "Comma in --to entry: $entry'\n" unless $entry !~ m/,/;
90 foreach my $entry (@initial_cc) {
91 die "Comma in --cc entry: $entry'\n" unless $entry !~ m/,/;
94 foreach my $entry (@bcclist) {
95 die "Comma in --bcclist entry: $entry'\n" unless $entry !~ m/,/;
98 # Now, let's fill any that aren't set in with defaults:
103 my $pid = open($fh, '-|');
104 die "$!" unless defined $pid;
106 exec('git-var', $var) or die "$!";
109 close $fh or die "$!";
116 my $val = gitvar
($name);
117 my @field = split(/\s+/, $val);
118 return join(' ', @field[0...(@field-3)]);
121 my ($author) = gitvar_ident
('GIT_AUTHOR_IDENT');
122 my ($committer) = gitvar_ident
('GIT_COMMITTER_IDENT');
125 chomp(my @alias_files = `git-repo-config --get-all sendemail.aliasesfile`);
126 chomp(my $aliasfiletype = `git-repo-config sendemail.aliasfiletype`);
128 # multiline formats can be supported in the future
129 mutt
=> sub { my $fh = shift; while (<$fh>) {
130 if (/^alias\s+(\S+)\s+(.*)$/) {
131 my ($alias, $addr) = ($1, $2);
132 $addr =~ s/#.*$//; # mutt allows # comments
133 # commas delimit multiple addresses
134 $aliases{$alias} = [ split(/\s*,\s*/, $addr) ];
136 mailrc
=> sub { my $fh = shift; while (<$fh>) {
137 if (/^alias\s+(\S+)\s+(.*)$/) {
138 # spaces delimit multiple addresses
139 $aliases{$1} = [ split(/\s+/, $2) ];
141 pine
=> sub { my $fh = shift; while (<$fh>) {
142 if (/^(\S+)\s+(.*)$/) {
143 $aliases{$1} = [ split(/\s*,\s*/, $2) ];
145 gnus
=> sub { my $fh = shift; while (<$fh>) {
146 if (/\(define-mail-alias\s+"(\S+?)"\s+"(\S+?)"\)/) {
147 $aliases{$1} = [ $2 ];
151 if (@alias_files && defined $parse_alias{$aliasfiletype}) {
152 foreach my $file (@alias_files) {
153 open my $fh, '<', $file or die "opening $file: $!\n";
154 $parse_alias{$aliasfiletype}->($fh);
160 if (!defined $from) {
161 $from = $author || $committer;
163 $_ = $term->readline("Who should the emails appear to be from? ",
165 } while (!defined $_);
168 print "Emails will be sent from: ", $from, "\n";
174 $_ = $term->readline("Who should the emails be sent to? ",
176 } while (!defined $_);
178 push @to, split /,/, $to;
187 @cur = map { $aliases{$_} ? @
{$aliases{$_}} : $_ } @last;
188 } while (join(',',@cur) ne join(',',@last));
192 @to = expand_aliases
(@to);
193 @initial_cc = expand_aliases
(@initial_cc);
194 @bcclist = expand_aliases
(@bcclist);
196 if (!defined $initial_subject && $compose) {
198 $_ = $term->readline("What subject should the emails start with? ",
200 } while (!defined $_);
201 $initial_subject = $_;
205 if (!defined $initial_reply_to && $prompting) {
207 $_= $term->readline("Message-ID to be used as In-Reply-To for the first email? ",
209 } while (!defined $_);
211 $initial_reply_to = $_;
212 $initial_reply_to =~ s/(^\s+|\s+$)//g;
216 foreach (qw( /usr/sbin/sendmail /usr/lib/sendmail )) {
222 $smtp_server ||= 'localhost'; # could be 127.0.0.1, too... *shrug*
226 # Note that this does not need to be secure, but we will make a small
227 # effort to have it be unique
228 open(C
,">",$compose_filename)
229 or die "Failed to open for writing $compose_filename: $!";
230 print C
"From $from # This line is ignored.\n";
231 printf C
"Subject: %s\n\n", $initial_subject;
233 GIT: Please enter your email below.
234 GIT: Lines beginning in "GIT: " will be removed.
235 GIT: Consider including an overall diffstat or table of contents
236 GIT: for the patch you are writing.
241 my $editor = $ENV{EDITOR
};
242 $editor = 'vi' unless defined $editor;
243 system($editor, $compose_filename);
245 open(C2
,">",$compose_filename . ".final")
246 or die "Failed to open $compose_filename.final : " . $!;
248 open(C
,"<",$compose_filename)
249 or die "Failed to open $compose_filename : " . $!;
259 $_ = $term->readline("Send this email? (y|n) ");
260 } while (!defined $_);
262 if (uc substr($_,0,1) ne 'Y') {
263 cleanup_compose_files
();
267 @files = ($compose_filename . ".final");
271 # Now that all the defaults are set, process the rest of the command line
272 # arguments and collect up the files that need to be processed.
276 or die "Failed to opendir $f: $!";
278 push @files, grep { -f
$_ } map { +$f . "/" . $_ }
285 print STDERR
"Skipping $f - not found.\n";
291 print $_,"\n" for (@files);
295 git-send-email [options] <file | directory> [... file | directory ]
297 --from Specify the "From:" line of the email to be sent.
299 --to Specify the primary "To:" line of the email.
301 --cc Specify an initial "Cc:" list for the entire series
304 --bcc Specify a list of email addresses that should be Bcc:
307 --compose Use \$EDITOR to edit an introductory message for the
310 --subject Specify the initial "Subject:" line.
311 Only necessary if --compose is also set. If --compose
312 is not set, this will be prompted for.
314 --in-reply-to Specify the first "In-Reply-To:" header line.
315 Only used if --compose is also set. If --compose is not
316 set, this will be prompted for.
318 --chain-reply-to If set, the replies will all be to the previous
319 email sent, rather than to the first email sent.
322 --no-signed-off-cc Suppress the automatic addition of email addresses
323 that appear in a Signed-off-by: line, to the cc: list.
324 Note: Using this option is not recommended.
326 --smtp-server If set, specifies the outgoing SMTP server to use.
327 Defaults to localhost.
329 --suppress-from Supress sending emails to yourself if your address
330 appears in a From: line.
332 --quiet Make git-send-email less verbose. One line per email should be
335 Error: Please specify a file or a directory on the command line.
340 # Variables we set as part of the loop over files
341 our ($message_id, $cc, %mail, $subject, $reply_to, $references, $message);
343 sub extract_valid_address
{
345 my $local_part_regexp = '[^<>"\s@]+';
346 my $domain_regexp = '[^.<>"\s@]+(?:\.[^.<>"\s@]+)+';
348 # check for a local address:
349 return $address if ($address =~ /^($local_part_regexp)$/);
351 if ($have_email_valid) {
352 return scalar Email
::Valid
->address($address);
354 # less robust/correct than the monster regexp in Email::Valid,
355 # but still does a 99% job, and one less dependency
356 $address =~ /($local_part_regexp\@$domain_regexp)/;
361 # Usually don't need to change anything below here.
363 # we make a "fake" message id by taking the current number
364 # of seconds since the beginning of Unix time and tacking on
365 # a random number to the end, in case we are called quicker than
366 # 1 second since the last time we were called.
368 # We'll setup a template for the message id, using the "from" address:
369 my $message_id_from = extract_valid_address
($from);
370 my $message_id_template = "<%s-git-send-email-$message_id_from>";
375 my $pseudo_rand = int (rand(4200));
376 $message_id = sprintf $message_id_template, "$date$pseudo_rand";
377 #print "new message id = $message_id\n"; # Was useful for debugging
383 $time = time - scalar $#files;
387 my @recipients = unique_email_list
(@to);
388 my $to = join (",\n\t", @recipients);
389 @recipients = unique_email_list
(@recipients,@cc,@bcclist);
390 my $date = strftime
('%a, %d %b %Y %H:%M:%S %z', localtime($time++));
391 my $gitversion = '@@GIT_VERSION@@';
392 if ($gitversion =~ m/..GIT_VERSION../) {
393 $gitversion = `git --version`;
395 # keep only what's after the last space
396 $gitversion =~ s/^.* //;
399 my $header = "From: $from
405 Message-Id: $message_id
406 X-Mailer: git-send-email $gitversion
410 $header .= "In-Reply-To: $reply_to\n";
411 $header .= "References: $references\n";
414 if ($smtp_server =~ m
#^/#) {
415 my $pid = open my $sm, '|-';
416 defined $pid or die $!;
418 exec($smtp_server,'-i',
419 map { extract_valid_address
($_) }
420 @recipients) or die $!;
422 print $sm "$header\n$message";
426 $smtp ||= Net
::SMTP
->new( $smtp_server );
427 $smtp->mail( $from ) or die $smtp->message;
428 $smtp->to( @recipients ) or die $smtp->message;
429 $smtp->data or die $smtp->message;
430 $smtp->datasend("$header\n$message") or die $smtp->message;
431 $smtp->dataend() or die $smtp->message;
432 $smtp->ok or die "Failed to send $subject\n".$smtp->message;
435 printf "Sent %s\n", $subject;
437 print "OK. Log says:\nDate: $date\n";
439 print "Server: $smtp_server\n";
441 print "Sendmail: $smtp_server\n";
443 print "From: $from\nSubject: $subject\nCc: $cc\nTo: $to\n\n";
445 print "Result: ", $smtp->code, ' ',
446 ($smtp->message =~ /\n([^\n]+\n)$/s), "\n";
448 print "Result: OK\n";
453 $reply_to = $initial_reply_to;
454 $references = $initial_reply_to || '';
456 $subject = $initial_subject;
458 foreach my $t (@files) {
459 open(F
,"<",$t) or die "can't open file $t";
461 my $author_not_sender = undef;
468 $found_mbox = 1, next if (/^From /);
472 if (/^Subject:\s+(.*)$/) {
475 } elsif (/^(Cc|From):\s+(.*)$/) {
477 next if ($suppress_from);
480 $author_not_sender = $2;
482 printf("(mbox) Adding cc: %s from line '%s'\n",
483 $2, $_) unless $quiet;
489 # "send lots of email" format,
492 # So let's support that, too.
494 printf("(non-mbox) Adding cc: %s from line '%s'\n",
495 $_, $_) unless $quiet;
499 } elsif (!defined $subject) {
504 # A whitespace line will terminate the headers
510 if (/^Signed-off-by: (.*)$/i && !$no_signed_off_cc) {
514 printf("(sob) Adding cc: %s from line '%s'\n",
515 $c, $_) unless $quiet;
520 if (defined $author_not_sender) {
521 $message = "From: $author_not_sender\n\n$message";
524 $cc = join(", ", unique_email_list
(@cc));
528 # set up for the next message
529 if ($chain_reply_to || length($reply_to) == 0) {
530 $reply_to = $message_id;
531 if (length $references > 0) {
532 $references .= " $message_id";
534 $references = "$message_id";
541 cleanup_compose_files
();
544 sub cleanup_compose_files
() {
545 unlink($compose_filename, $compose_filename . ".final");
549 $smtp->quit if $smtp;
551 sub unique_email_list
(@
) {
555 foreach my $entry (@_) {
556 if (my $clean = extract_valid_address
($entry)) {
558 next if $seen{$clean}++;
559 push @emails, $entry;
561 print STDERR
"W: unable to extract a valid address",