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.
26 # most mail servers generate the Date: header, but not all...
28 use POSIX qw/strftime/;
30 my $have_email_valid = eval { require Email::Valid; 1 };
33 sub unique_email_list(@);
34 sub cleanup_compose_files();
36 # Constants (essentially)
37 my $compose_filename = ".msg.$$";
39 # Variables we fill in automatically, or via prompting:
40 my (@to,@cc,@initial_cc,@bcclist,
41 $initial_reply_to,$initial_subject,@files,$from,$compose,$time);
43 # Behavior modification variables
44 my ($chain_reply_to, $quiet, $suppress_from, $no_signed_off_cc) = (1, 0, 0, 0);
48 #$initial_reply_to = ''; #<20050203173208.GA23964@foobar.com>';
50 my $term = new Term::ReadLine 'git-send-email';
52 # Begin by accumulating all the variables (defined above), that we will end up
53 # needing, first, from the command line:
55 my $rc = GetOptions("from=s" => \$from,
56 "in-reply-to=s" => \$initial_reply_to,
57 "subject=s" => \$initial_subject,
59 "cc=s" => \@initial_cc,
61 "chain-reply-to!" => \$chain_reply_to,
62 "smtp-server=s" => \$smtp_server,
63 "compose" => \$compose,
65 "suppress-from" => \$suppress_from,
66 "no-signed-off-cc|no-signed-off-by-cc" => \$no_signed_off_cc,
69 # Now, let's fill any that aren't set in with defaults:
74 my $pid = open($fh, '-|');
75 die "$!" unless defined $pid;
77 exec('git-var', $var) or die "$!";
80 close $fh or die "$!";
87 my $val = gitvar($name);
88 my @field = split(/\s+/, $val);
89 return join(' ', @field[0...(@field-3)]);
92 my ($author) = gitvar_ident('GIT_AUTHOR_IDENT');
93 my ($committer) = gitvar_ident('GIT_COMMITTER_IDENT');
96 chomp(my @alias_files = `git-repo-config --get-all sendemail.aliasesfile`);
97 chomp(my $aliasfiletype = `git-repo-config sendemail.aliasfiletype`);
99 # multiline formats can be supported in the future
100 mutt => sub { my $fh = shift; while (<$fh>) {
101 if (/^alias\s+(\S+)\s+(.*)$/) {
102 my ($alias, $addr) = ($1, $2);
103 $addr =~ s/#.*$//; # mutt allows # comments
104 # commas delimit multiple addresses
105 $aliases{$alias} = [ split(/\s*,\s*/, $addr) ];
107 mailrc => sub { my $fh = shift; while (<$fh>) {
108 if (/^alias\s+(\S+)\s+(.*)$/) {
109 # spaces delimit multiple addresses
110 $aliases{$1} = [ split(/\s+/, $2) ];
112 pine => sub { my $fh = shift; while (<$fh>) {
113 if (/^(\S+)\s+(.*)$/) {
114 $aliases{$1} = [ split(/\s*,\s*/, $2) ];
116 gnus => sub { my $fh = shift; while (<$fh>) {
117 if (/\(define-mail-alias\s+"(\S+?)"\s+"(\S+?)"\)/) {
118 $aliases{$1} = [ $2 ];
122 if (@alias_files && defined $parse_alias{$aliasfiletype}) {
123 foreach my $file (@alias_files) {
124 open my $fh, '<', $file or die "opening $file: $!\n";
125 $parse_alias{$aliasfiletype}->($fh);
131 if (!defined $from) {
132 $from = $author || $committer;
134 $_ = $term->readline("Who should the emails appear to be from? ",
136 } while (!defined $_);
139 print "Emails will be sent from: ", $from, "\n";
145 $_ = $term->readline("Who should the emails be sent to? ",
147 } while (!defined $_);
149 push @to, split /,/, $to;
158 @cur = map { $aliases{$_} ? @{$aliases{$_}} : $_ } @last;
159 } while (join(',',@cur) ne join(',',@last));
163 @to = expand_aliases(@to);
164 @initial_cc = expand_aliases(@initial_cc);
165 @bcclist = expand_aliases(@bcclist);
167 if (!defined $initial_subject && $compose) {
169 $_ = $term->readline("What subject should the emails start with? ",
171 } while (!defined $_);
172 $initial_subject = $_;
176 if (!defined $initial_reply_to && $prompting) {
178 $_= $term->readline("Message-ID to be used as In-Reply-To for the first email? ",
180 } while (!defined $_);
182 $initial_reply_to = $_;
183 $initial_reply_to =~ s/(^\s+|\s+$)//g;
187 foreach (qw( /usr/sbin/sendmail /usr/lib/sendmail )) {
193 $smtp_server ||= 'localhost'; # could be 127.0.0.1, too... *shrug*
197 # Note that this does not need to be secure, but we will make a small
198 # effort to have it be unique
199 open(C,">",$compose_filename)
200 or die "Failed to open for writing $compose_filename: $!";
201 print C "From $from # This line is ignored.\n";
202 printf C "Subject: %s\n\n", $initial_subject;
204 GIT: Please enter your email below.
205 GIT: Lines beginning in "GIT: " will be removed.
206 GIT: Consider including an overall diffstat or table of contents
207 GIT: for the patch you are writing.
212 my $editor = $ENV{EDITOR};
213 $editor = 'vi' unless defined $editor;
214 system($editor, $compose_filename);
216 open(C2,">",$compose_filename . ".final")
217 or die "Failed to open $compose_filename.final : " . $!;
219 open(C,"<",$compose_filename)
220 or die "Failed to open $compose_filename : " . $!;
230 $_ = $term->readline("Send this email? (y|n) ");
231 } while (!defined $_);
233 if (uc substr($_,0,1) ne 'Y') {
234 cleanup_compose_files();
238 @files = ($compose_filename . ".final");
242 # Now that all the defaults are set, process the rest of the command line
243 # arguments and collect up the files that need to be processed.
247 or die "Failed to opendir $f: $!";
249 push @files, grep { -f $_ } map { +$f . "/" . $_ }
256 print STDERR "Skipping $f - not found.\n";
262 print $_,"\n" for (@files);
266 git-send-email [options] <file | directory> [... file | directory ]
268 --from Specify the "From:" line of the email to be sent.
270 --to Specify the primary "To:" line of the email.
272 --cc Specify an initial "Cc:" list for the entire series
275 --bcc Specify a list of email addresses that should be Bcc:
278 --compose Use \$EDITOR to edit an introductory message for the
281 --subject Specify the initial "Subject:" line.
282 Only necessary if --compose is also set. If --compose
283 is not set, this will be prompted for.
285 --in-reply-to Specify the first "In-Reply-To:" header line.
286 Only used if --compose is also set. If --compose is not
287 set, this will be prompted for.
289 --chain-reply-to If set, the replies will all be to the previous
290 email sent, rather than to the first email sent.
293 --no-signed-off-cc Suppress the automatic addition of email addresses
294 that appear in a Signed-off-by: line, to the cc: list.
295 Note: Using this option is not recommended.
297 --smtp-server If set, specifies the outgoing SMTP server to use.
298 Defaults to localhost.
300 --suppress-from Supress sending emails to yourself if your address
301 appears in a From: line.
303 --quiet Make git-send-email less verbose. One line per email should be
306 Error: Please specify a file or a directory on the command line.
311 # Variables we set as part of the loop over files
312 our ($message_id, $cc, %mail, $subject, $reply_to, $references, $message);
314 sub extract_valid_address {
317 # check for a local address:
318 return $address if ($address =~ /^([\w\-]+)$/);
320 if ($have_email_valid) {
321 return Email::Valid->address($address);
323 # less robust/correct than the monster regexp in Email::Valid,
324 # but still does a 99% job, and one less dependency
326 if ($address =~ /([^\"<>\s]+@[^<>\s]+)/) {
327 $cleaned_address = $1;
329 return $cleaned_address;
333 # Usually don't need to change anything below here.
335 # we make a "fake" message id by taking the current number
336 # of seconds since the beginning of Unix time and tacking on
337 # a random number to the end, in case we are called quicker than
338 # 1 second since the last time we were called.
340 # We'll setup a template for the message id, using the "from" address:
341 my $message_id_from = extract_valid_address($from);
342 my $message_id_template = "<%s-git-send-email-$message_id_from>";
347 my $pseudo_rand = int (rand(4200));
348 $message_id = sprintf $message_id_template, "$date$pseudo_rand";
349 #print "new message id = $message_id\n"; # Was useful for debugging
355 $time = time - scalar $#files;
359 my @recipients = unique_email_list(@to);
360 my $to = join (",\n\t", @recipients);
361 @recipients = unique_email_list(@recipients,@cc,@bcclist);
362 my $date = strftime('%a, %d %b %Y %H:%M:%S %z', localtime($time++));
363 my $gitversion = '@@GIT_VERSION@@';
364 if ($gitversion =~ m/..GIT_VERSION../) {
365 $gitversion = `git --version`;
367 # keep only what's after the last space
368 $gitversion =~ s/^.* //;
371 my $header = "From: $from
377 Message-Id: $message_id
378 X-Mailer: git-send-email $gitversion
382 $header .= "In-Reply-To: $reply_to\n";
383 $header .= "References: $references\n";
386 if ($smtp_server =~ m#^/#) {
387 my $pid = open my $sm, '|-';
388 defined $pid or die $!;
390 exec($smtp_server,'-i',
391 map { scalar extract_valid_address($_) }
392 @recipients) or die $!;
394 print $sm "$header\n$message";
397 $smtp ||= Net::SMTP->new( $smtp_server );
398 $smtp->mail( $from ) or die $smtp->message;
399 $smtp->to( @recipients ) or die $smtp->message;
400 $smtp->data or die $smtp->message;
401 $smtp->datasend("$header\n$message") or die $smtp->message;
402 $smtp->dataend() or die $smtp->message;
403 $smtp->ok or die "Failed to send $subject\n".$smtp->message;
406 printf "Sent %s\n", $subject;
408 print "OK. Log says:\nDate: $date\n";
410 print "Server: $smtp_server\n";
412 print "Sendmail: $smtp_server\n";
414 print "From: $from\nSubject: $subject\nCc: $cc\nTo: $to\n\n";
416 print "Result: ", $smtp->code, ' ',
417 ($smtp->message =~ /\n([^\n]+\n)$/s), "\n";
419 print "Result: OK\n";
424 $reply_to = $initial_reply_to;
425 $references = $initial_reply_to || '';
427 $subject = $initial_subject;
429 foreach my $t (@files) {
430 open(F,"<",$t) or die "can't open file $t";
432 my $author_not_sender = undef;
439 $found_mbox = 1, next if (/^From /);
443 if (/^Subject:\s+(.*)$/) {
446 } elsif (/^(Cc|From):\s+(.*)$/) {
448 next if ($suppress_from);
451 $author_not_sender = $2;
453 printf("(mbox) Adding cc: %s from line '%s'\n",
454 $2, $_) unless $quiet;
460 # "send lots of email" format,
463 # So let's support that, too.
465 printf("(non-mbox) Adding cc: %s from line '%s'\n",
466 $_, $_) unless $quiet;
470 } elsif (!defined $subject) {
475 # A whitespace line will terminate the headers
481 if (/^Signed-off-by: (.*)$/i && !$no_signed_off_cc) {
485 printf("(sob) Adding cc: %s from line '%s'\n",
486 $c, $_) unless $quiet;
491 if (defined $author_not_sender) {
492 $message = "From: $author_not_sender\n\n$message";
495 $cc = join(", ", unique_email_list(@cc));
499 # set up for the next message
500 if ($chain_reply_to || length($reply_to) == 0) {
501 $reply_to = $message_id;
502 if (length $references > 0) {
503 $references .= " $message_id";
505 $references = "$message_id";
512 cleanup_compose_files();
515 sub cleanup_compose_files() {
516 unlink($compose_filename, $compose_filename . ".final");
520 $smtp->quit if $smtp;
522 sub unique_email_list(@) {
526 foreach my $entry (@_) {
527 if (my $clean = extract_valid_address($entry)) {
529 next if $seen{$clean}++;
530 push @emails, $entry;
532 print STDERR "W: unable to extract a valid address",