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.
25 # most mail servers generate the Date: header, but not all...
27 use POSIX qw
/strftime/;
29 my $have_email_valid = eval { require Email
::Valid
; 1 };
32 sub unique_email_list
(@
);
33 sub cleanup_compose_files
();
35 # Constants (essentially)
36 my $compose_filename = ".msg.$$";
38 # Variables we fill in automatically, or via prompting:
39 my (@to,@cc,@initial_cc,@bcclist,
40 $initial_reply_to,$initial_subject,@files,$from,$compose,$time);
42 # Behavior modification variables
43 my ($chain_reply_to, $quiet, $suppress_from, $no_signed_off_cc) = (1, 0, 0, 0);
47 #$initial_reply_to = ''; #<20050203173208.GA23964@foobar.com>';
49 my $term = new Term
::ReadLine
'git-send-email';
51 # Begin by accumulating all the variables (defined above), that we will end up
52 # needing, first, from the command line:
54 my $rc = GetOptions
("from=s" => \
$from,
55 "in-reply-to=s" => \
$initial_reply_to,
56 "subject=s" => \
$initial_subject,
58 "cc=s" => \
@initial_cc,
60 "chain-reply-to!" => \
$chain_reply_to,
61 "smtp-server=s" => \
$smtp_server,
62 "compose" => \
$compose,
64 "suppress-from" => \
$suppress_from,
65 "no-signed-off-cc|no-signed-off-by-cc" => \
$no_signed_off_cc,
68 # Now, let's fill any that aren't set in with defaults:
73 my $pid = open($fh, '-|');
74 die "$!" unless defined $pid;
76 exec('git-var', $var) or die "$!";
79 close $fh or die "$!";
86 my $val = gitvar
($name);
87 my @field = split(/\s+/, $val);
88 return join(' ', @field[0...(@field-3)]);
91 my ($author) = gitvar_ident
('GIT_AUTHOR_IDENT');
92 my ($committer) = gitvar_ident
('GIT_COMMITTER_IDENT');
95 chomp(my @alias_files = `git-repo-config --get-all sendemail.aliasesfile`);
96 chomp(my $aliasfiletype = `git-repo-config sendemail.aliasfiletype`);
98 # multiline formats can be supported in the future
99 mutt
=> sub { my $fh = shift; while (<$fh>) {
100 if (/^alias\s+(\S+)\s+(.*)$/) {
101 my ($alias, $addr) = ($1, $2);
102 $addr =~ s/#.*$//; # mutt allows # comments
103 # commas delimit multiple addresses
104 $aliases{$alias} = [ split(/\s*,\s*/, $addr) ];
106 mailrc
=> sub { my $fh = shift; while (<$fh>) {
107 if (/^alias\s+(\S+)\s+(.*)$/) {
108 # spaces delimit multiple addresses
109 $aliases{$1} = [ split(/\s+/, $2) ];
111 pine
=> sub { my $fh = shift; while (<$fh>) {
112 if (/^(\S+)\s+(.*)$/) {
113 $aliases{$1} = [ split(/\s*,\s*/, $2) ];
115 gnus
=> sub { my $fh = shift; while (<$fh>) {
116 if (/\(define-mail-alias\s+"(\S+?)"\s+"(\S+?)"\)/) {
117 $aliases{$1} = [ $2 ];
121 if (@alias_files && defined $parse_alias{$aliasfiletype}) {
122 foreach my $file (@alias_files) {
123 open my $fh, '<', $file or die "opening $file: $!\n";
124 $parse_alias{$aliasfiletype}->($fh);
130 if (!defined $from) {
131 $from = $author || $committer;
133 $_ = $term->readline("Who should the emails appear to be from? ",
135 } while (!defined $_);
138 print "Emails will be sent from: ", $from, "\n";
144 $_ = $term->readline("Who should the emails be sent to? ",
146 } while (!defined $_);
148 push @to, split /,/, $to;
157 @cur = map { $aliases{$_} ? @
{$aliases{$_}} : $_ } @last;
158 } while (join(',',@cur) ne join(',',@last));
162 @to = expand_aliases
(@to);
163 @initial_cc = expand_aliases
(@initial_cc);
164 @bcclist = expand_aliases
(@bcclist);
166 if (!defined $initial_subject && $compose) {
168 $_ = $term->readline("What subject should the emails start with? ",
170 } while (!defined $_);
171 $initial_subject = $_;
175 if (!defined $initial_reply_to && $prompting) {
177 $_= $term->readline("Message-ID to be used as In-Reply-To for the first email? ",
179 } while (!defined $_);
181 $initial_reply_to = $_;
182 $initial_reply_to =~ s/(^\s+|\s+$)//g;
186 foreach (qw( /usr/sbin/sendmail /usr/lib/sendmail )) {
192 $smtp_server ||= 'localhost'; # could be 127.0.0.1, too... *shrug*
196 # Note that this does not need to be secure, but we will make a small
197 # effort to have it be unique
198 open(C
,">",$compose_filename)
199 or die "Failed to open for writing $compose_filename: $!";
200 print C
"From $from # This line is ignored.\n";
201 printf C
"Subject: %s\n\n", $initial_subject;
203 GIT: Please enter your email below.
204 GIT: Lines beginning in "GIT: " will be removed.
205 GIT: Consider including an overall diffstat or table of contents
206 GIT: for the patch you are writing.
211 my $editor = $ENV{EDITOR
};
212 $editor = 'vi' unless defined $editor;
213 system($editor, $compose_filename);
215 open(C2
,">",$compose_filename . ".final")
216 or die "Failed to open $compose_filename.final : " . $!;
218 open(C
,"<",$compose_filename)
219 or die "Failed to open $compose_filename : " . $!;
229 $_ = $term->readline("Send this email? (y|n) ");
230 } while (!defined $_);
232 if (uc substr($_,0,1) ne 'Y') {
233 cleanup_compose_files
();
237 @files = ($compose_filename . ".final");
241 # Now that all the defaults are set, process the rest of the command line
242 # arguments and collect up the files that need to be processed.
246 or die "Failed to opendir $f: $!";
248 push @files, grep { -f
$_ } map { +$f . "/" . $_ }
255 print STDERR
"Skipping $f - not found.\n";
261 print $_,"\n" for (@files);
265 git-send-email [options] <file | directory> [... file | directory ]
267 --from Specify the "From:" line of the email to be sent.
269 --to Specify the primary "To:" line of the email.
271 --cc Specify an initial "Cc:" list for the entire series
274 --bcc Specify a list of email addresses that should be Bcc:
277 --compose Use \$EDITOR to edit an introductory message for the
280 --subject Specify the initial "Subject:" line.
281 Only necessary if --compose is also set. If --compose
282 is not set, this will be prompted for.
284 --in-reply-to Specify the first "In-Reply-To:" header line.
285 Only used if --compose is also set. If --compose is not
286 set, this will be prompted for.
288 --chain-reply-to If set, the replies will all be to the previous
289 email sent, rather than to the first email sent.
292 --no-signed-off-cc Suppress the automatic addition of email addresses
293 that appear in a Signed-off-by: line, to the cc: list.
294 Note: Using this option is not recommended.
296 --smtp-server If set, specifies the outgoing SMTP server to use.
297 Defaults to localhost.
299 --suppress-from Supress sending emails to yourself if your address
300 appears in a From: line.
302 --quiet Make git-send-email less verbose. One line per email should be
305 Error: Please specify a file or a directory on the command line.
310 # Variables we set as part of the loop over files
311 our ($message_id, $cc, %mail, $subject, $reply_to, $references, $message);
313 sub extract_valid_address
{
315 my $local_part_regexp = '[^<>"\s@]+';
316 my $domain_regexp = '[^.<>"\s@]+(?:\.[^.<>"\s@]+)+';
318 # check for a local address:
319 return $address if ($address =~ /^($local_part_regexp)$/);
321 if ($have_email_valid) {
322 return scalar Email
::Valid
->address($address);
324 # less robust/correct than the monster regexp in Email::Valid,
325 # but still does a 99% job, and one less dependency
326 $address =~ /($local_part_regexp\@$domain_regexp)/;
331 # Usually don't need to change anything below here.
333 # we make a "fake" message id by taking the current number
334 # of seconds since the beginning of Unix time and tacking on
335 # a random number to the end, in case we are called quicker than
336 # 1 second since the last time we were called.
338 # We'll setup a template for the message id, using the "from" address:
339 my $message_id_from = extract_valid_address
($from);
340 my $message_id_template = "<%s-git-send-email-$message_id_from>";
345 my $pseudo_rand = int (rand(4200));
346 $message_id = sprintf $message_id_template, "$date$pseudo_rand";
347 #print "new message id = $message_id\n"; # Was useful for debugging
353 $time = time - scalar $#files;
357 my @recipients = unique_email_list
(@to);
358 my $to = join (",\n\t", @recipients);
359 @recipients = unique_email_list
(@recipients,@cc,@bcclist);
360 my $date = strftime
('%a, %d %b %Y %H:%M:%S %z', localtime($time++));
361 my $gitversion = '@@GIT_VERSION@@';
362 if ($gitversion =~ m/..GIT_VERSION../) {
363 $gitversion = `git --version`;
365 # keep only what's after the last space
366 $gitversion =~ s/^.* //;
369 my $header = "From: $from
375 Message-Id: $message_id
376 X-Mailer: git-send-email $gitversion
380 $header .= "In-Reply-To: $reply_to\n";
381 $header .= "References: $references\n";
384 if ($smtp_server =~ m
#^/#) {
385 my $pid = open my $sm, '|-';
386 defined $pid or die $!;
388 exec($smtp_server,'-i',
389 map { extract_valid_address
($_) }
390 @recipients) or die $!;
392 print $sm "$header\n$message";
396 $smtp ||= Net
::SMTP
->new( $smtp_server );
397 $smtp->mail( $from ) or die $smtp->message;
398 $smtp->to( @recipients ) or die $smtp->message;
399 $smtp->data or die $smtp->message;
400 $smtp->datasend("$header\n$message") or die $smtp->message;
401 $smtp->dataend() or die $smtp->message;
402 $smtp->ok or die "Failed to send $subject\n".$smtp->message;
405 printf "Sent %s\n", $subject;
407 print "OK. Log says:\nDate: $date\n";
409 print "Server: $smtp_server\n";
411 print "Sendmail: $smtp_server\n";
413 print "From: $from\nSubject: $subject\nCc: $cc\nTo: $to\n\n";
415 print "Result: ", $smtp->code, ' ',
416 ($smtp->message =~ /\n([^\n]+\n)$/s), "\n";
418 print "Result: OK\n";
423 $reply_to = $initial_reply_to;
424 $references = $initial_reply_to || '';
426 $subject = $initial_subject;
428 foreach my $t (@files) {
429 open(F
,"<",$t) or die "can't open file $t";
431 my $author_not_sender = undef;
438 $found_mbox = 1, next if (/^From /);
442 if (/^Subject:\s+(.*)$/) {
445 } elsif (/^(Cc|From):\s+(.*)$/) {
447 next if ($suppress_from);
450 $author_not_sender = $2;
452 printf("(mbox) Adding cc: %s from line '%s'\n",
453 $2, $_) unless $quiet;
459 # "send lots of email" format,
462 # So let's support that, too.
464 printf("(non-mbox) Adding cc: %s from line '%s'\n",
465 $_, $_) unless $quiet;
469 } elsif (!defined $subject) {
474 # A whitespace line will terminate the headers
480 if (/^Signed-off-by: (.*)$/i && !$no_signed_off_cc) {
484 printf("(sob) Adding cc: %s from line '%s'\n",
485 $c, $_) unless $quiet;
490 if (defined $author_not_sender) {
491 $message = "From: $author_not_sender\n\n$message";
494 $cc = join(", ", unique_email_list
(@cc));
498 # set up for the next message
499 if ($chain_reply_to || length($reply_to) == 0) {
500 $reply_to = $message_id;
501 if (length $references > 0) {
502 $references .= " $message_id";
504 $references = "$message_id";
511 cleanup_compose_files
();
514 sub cleanup_compose_files
() {
515 unlink($compose_filename, $compose_filename . ".final");
519 $smtp->quit if $smtp;
521 sub unique_email_list
(@
) {
525 foreach my $entry (@_) {
526 if (my $clean = extract_valid_address
($entry)) {
528 next if $seen{$clean}++;
529 push @emails, $entry;
531 print STDERR
"W: unable to extract a valid address",