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.
28 my ($class, $reason) = @_;
29 return bless \
$reason, shift;
33 die "Cannot use readline on FakeTerm: $$self";
37 # most mail servers generate the Date: header, but not all...
38 sub format_2822_time
{
40 my @localtm = localtime($time);
41 my @gmttm = gmtime($time);
42 my $localmin = $localtm[1] + $localtm[2] * 60;
43 my $gmtmin = $gmttm[1] + $gmttm[2] * 60;
44 if ($localtm[0] != $gmttm[0]) {
45 die "local zone differs from GMT by a non-minute interval\n";
47 if ((($gmttm[6] + 1) % 7) == $localtm[6]) {
49 } elsif ((($gmttm[6] - 1) % 7) == $localtm[6]) {
51 } elsif ($gmttm[6] != $localtm[6]) {
52 die "local time offset greater than or equal to 24 hours\n";
54 my $offset = $localmin - $gmtmin;
55 my $offhour = $offset / 60;
56 my $offmin = abs($offset % 60);
57 if (abs($offhour) >= 24) {
58 die ("local time offset greater than or equal to 24 hours\n");
61 return sprintf("%s, %2d %s %d %02d:%02d:%02d %s%02d%02d",
62 qw(Sun Mon Tue Wed Thu Fri Sat)[$localtm[6]],
64 qw(Jan Feb Mar Apr May Jun
65 Jul Aug Sep Oct Nov Dec)[$localtm[4]],
70 ($offset >= 0) ?
'+' : '-',
76 my $have_email_valid = eval { require Email
::Valid
; 1 };
79 sub unique_email_list
(@
);
80 sub cleanup_compose_files
();
82 # Constants (essentially)
83 my $compose_filename = ".msg.$$";
85 # Variables we fill in automatically, or via prompting:
86 my (@to,@cc,@initial_cc,@bcclist,
87 $initial_reply_to,$initial_subject,@files,$from,$compose,$time);
89 # Behavior modification variables
90 my ($chain_reply_to, $quiet, $suppress_from, $no_signed_off_cc) = (1, 0, 0, 0);
94 #$initial_reply_to = ''; #<20050203173208.GA23964@foobar.com>';
96 my $repo = Git
->repository();
98 new Term
::ReadLine
'git-send-email';
101 $term = new FakeTerm
"$@: going non-interactive";
104 # Begin by accumulating all the variables (defined above), that we will end up
105 # needing, first, from the command line:
107 my $rc = GetOptions
("from=s" => \
$from,
108 "in-reply-to=s" => \
$initial_reply_to,
109 "subject=s" => \
$initial_subject,
111 "cc=s" => \
@initial_cc,
112 "bcc=s" => \
@bcclist,
113 "chain-reply-to!" => \
$chain_reply_to,
114 "smtp-server=s" => \
$smtp_server,
115 "compose" => \
$compose,
117 "suppress-from" => \
$suppress_from,
118 "no-signed-off-cc|no-signed-off-by-cc" => \
$no_signed_off_cc,
121 # Verify the user input
123 foreach my $entry (@to) {
124 die "Comma in --to entry: $entry'\n" unless $entry !~ m/,/;
127 foreach my $entry (@initial_cc) {
128 die "Comma in --cc entry: $entry'\n" unless $entry !~ m/,/;
131 foreach my $entry (@bcclist) {
132 die "Comma in --bcclist entry: $entry'\n" unless $entry !~ m/,/;
135 # Now, let's fill any that aren't set in with defaults:
137 my ($author) = $repo->ident_person('author');
138 my ($committer) = $repo->ident_person('committer');
141 my @alias_files = $repo->config('sendemail.aliasesfile');
142 my $aliasfiletype = $repo->config('sendemail.aliasfiletype');
144 # multiline formats can be supported in the future
145 mutt
=> sub { my $fh = shift; while (<$fh>) {
146 if (/^alias\s+(\S+)\s+(.*)$/) {
147 my ($alias, $addr) = ($1, $2);
148 $addr =~ s/#.*$//; # mutt allows # comments
149 # commas delimit multiple addresses
150 $aliases{$alias} = [ split(/\s*,\s*/, $addr) ];
152 mailrc
=> sub { my $fh = shift; while (<$fh>) {
153 if (/^alias\s+(\S+)\s+(.*)$/) {
154 # spaces delimit multiple addresses
155 $aliases{$1} = [ split(/\s+/, $2) ];
157 pine
=> sub { my $fh = shift; while (<$fh>) {
158 if (/^(\S+)\s+(.*)$/) {
159 $aliases{$1} = [ split(/\s*,\s*/, $2) ];
161 gnus
=> sub { my $fh = shift; while (<$fh>) {
162 if (/\(define-mail-alias\s+"(\S+?)"\s+"(\S+?)"\)/) {
163 $aliases{$1} = [ $2 ];
167 if (@alias_files and $aliasfiletype and defined $parse_alias{$aliasfiletype}) {
168 foreach my $file (@alias_files) {
169 open my $fh, '<', $file or die "opening $file: $!\n";
170 $parse_alias{$aliasfiletype}->($fh);
176 if (!defined $from) {
177 $from = $author || $committer;
179 $_ = $term->readline("Who should the emails appear to be from? ",
181 } while (!defined $_);
184 print "Emails will be sent from: ", $from, "\n";
190 $_ = $term->readline("Who should the emails be sent to? ",
192 } while (!defined $_);
194 push @to, split /,/, $to;
203 @cur = map { $aliases{$_} ? @
{$aliases{$_}} : $_ } @last;
204 } while (join(',',@cur) ne join(',',@last));
208 @to = expand_aliases
(@to);
209 @initial_cc = expand_aliases
(@initial_cc);
210 @bcclist = expand_aliases
(@bcclist);
212 if (!defined $initial_subject && $compose) {
214 $_ = $term->readline("What subject should the emails start with? ",
216 } while (!defined $_);
217 $initial_subject = $_;
221 if (!defined $initial_reply_to && $prompting) {
223 $_= $term->readline("Message-ID to be used as In-Reply-To for the first email? ",
225 } while (!defined $_);
227 $initial_reply_to = $_;
228 $initial_reply_to =~ s/(^\s+|\s+$)//g;
232 foreach (qw( /usr/sbin/sendmail /usr/lib/sendmail )) {
238 $smtp_server ||= 'localhost'; # could be 127.0.0.1, too... *shrug*
242 # Note that this does not need to be secure, but we will make a small
243 # effort to have it be unique
244 open(C
,">",$compose_filename)
245 or die "Failed to open for writing $compose_filename: $!";
246 print C
"From $from # This line is ignored.\n";
247 printf C
"Subject: %s\n\n", $initial_subject;
249 GIT: Please enter your email below.
250 GIT: Lines beginning in "GIT: " will be removed.
251 GIT: Consider including an overall diffstat or table of contents
252 GIT: for the patch you are writing.
257 my $editor = $ENV{EDITOR
};
258 $editor = 'vi' unless defined $editor;
259 system($editor, $compose_filename);
261 open(C2
,">",$compose_filename . ".final")
262 or die "Failed to open $compose_filename.final : " . $!;
264 open(C
,"<",$compose_filename)
265 or die "Failed to open $compose_filename : " . $!;
275 $_ = $term->readline("Send this email? (y|n) ");
276 } while (!defined $_);
278 if (uc substr($_,0,1) ne 'Y') {
279 cleanup_compose_files
();
283 @files = ($compose_filename . ".final");
287 # Now that all the defaults are set, process the rest of the command line
288 # arguments and collect up the files that need to be processed.
292 or die "Failed to opendir $f: $!";
294 push @files, grep { -f
$_ } map { +$f . "/" . $_ }
301 print STDERR
"Skipping $f - not found.\n";
307 print $_,"\n" for (@files);
311 git-send-email [options] <file | directory> [... file | directory ]
313 --from Specify the "From:" line of the email to be sent.
315 --to Specify the primary "To:" line of the email.
317 --cc Specify an initial "Cc:" list for the entire series
320 --bcc Specify a list of email addresses that should be Bcc:
323 --compose Use \$EDITOR to edit an introductory message for the
326 --subject Specify the initial "Subject:" line.
327 Only necessary if --compose is also set. If --compose
328 is not set, this will be prompted for.
330 --in-reply-to Specify the first "In-Reply-To:" header line.
331 Only used if --compose is also set. If --compose is not
332 set, this will be prompted for.
334 --chain-reply-to If set, the replies will all be to the previous
335 email sent, rather than to the first email sent.
338 --no-signed-off-cc Suppress the automatic addition of email addresses
339 that appear in a Signed-off-by: line, to the cc: list.
340 Note: Using this option is not recommended.
342 --smtp-server If set, specifies the outgoing SMTP server to use.
343 Defaults to localhost.
345 --suppress-from Suppress sending emails to yourself if your address
346 appears in a From: line.
348 --quiet Make git-send-email less verbose. One line per email should be
351 Error: Please specify a file or a directory on the command line.
356 # Variables we set as part of the loop over files
357 our ($message_id, $cc, %mail, $subject, $reply_to, $references, $message);
359 sub extract_valid_address
{
361 my $local_part_regexp = '[^<>"\s@]+';
362 my $domain_regexp = '[^.<>"\s@]+(?:\.[^.<>"\s@]+)+';
364 # check for a local address:
365 return $address if ($address =~ /^($local_part_regexp)$/);
367 if ($have_email_valid) {
368 return scalar Email
::Valid
->address($address);
370 # less robust/correct than the monster regexp in Email::Valid,
371 # but still does a 99% job, and one less dependency
372 $address =~ /($local_part_regexp\@$domain_regexp)/;
377 # Usually don't need to change anything below here.
379 # we make a "fake" message id by taking the current number
380 # of seconds since the beginning of Unix time and tacking on
381 # a random number to the end, in case we are called quicker than
382 # 1 second since the last time we were called.
384 # We'll setup a template for the message id, using the "from" address:
385 my $message_id_from = extract_valid_address
($from);
386 my $message_id_template = "<%s-git-send-email-$message_id_from>";
391 my $pseudo_rand = int (rand(4200));
392 $message_id = sprintf $message_id_template, "$date$pseudo_rand";
393 #print "new message id = $message_id\n"; # Was useful for debugging
399 $time = time - scalar $#files;
403 my @recipients = unique_email_list
(@to);
404 my $to = join (",\n\t", @recipients);
405 @recipients = unique_email_list
(@recipients,@cc,@bcclist);
406 my $date = format_2822_time
($time++);
407 my $gitversion = '@@GIT_VERSION@@';
408 if ($gitversion =~ m/..GIT_VERSION../) {
409 $gitversion = Git
::version
();
412 my $header = "From: $from
417 Message-Id: $message_id
418 X-Mailer: git-send-email $gitversion
422 $header .= "In-Reply-To: $reply_to\n";
423 $header .= "References: $references\n";
426 if ($smtp_server =~ m
#^/#) {
427 my $pid = open my $sm, '|-';
428 defined $pid or die $!;
430 exec($smtp_server,'-i',
431 map { extract_valid_address
($_) }
432 @recipients) or die $!;
434 print $sm "$header\n$message";
438 $smtp ||= Net
::SMTP
->new( $smtp_server );
439 $smtp->mail( $from ) or die $smtp->message;
440 $smtp->to( @recipients ) or die $smtp->message;
441 $smtp->data or die $smtp->message;
442 $smtp->datasend("$header\n$message") or die $smtp->message;
443 $smtp->dataend() or die $smtp->message;
444 $smtp->ok or die "Failed to send $subject\n".$smtp->message;
447 printf "Sent %s\n", $subject;
449 print "OK. Log says:\nDate: $date\n";
451 print "Server: $smtp_server\n";
453 print "Sendmail: $smtp_server\n";
455 print "From: $from\nSubject: $subject\nCc: $cc\nTo: $to\n\n";
457 print "Result: ", $smtp->code, ' ',
458 ($smtp->message =~ /\n([^\n]+\n)$/s), "\n";
460 print "Result: OK\n";
465 $reply_to = $initial_reply_to;
466 $references = $initial_reply_to || '';
468 $subject = $initial_subject;
470 foreach my $t (@files) {
471 open(F
,"<",$t) or die "can't open file $t";
473 my $author_not_sender = undef;
480 $found_mbox = 1, next if (/^From /);
484 if (/^Subject:\s+(.*)$/) {
487 } elsif (/^(Cc|From):\s+(.*)$/) {
489 next if ($suppress_from);
491 elsif ($1 eq 'From') {
492 $author_not_sender = $2;
494 printf("(mbox) Adding cc: %s from line '%s'\n",
495 $2, $_) unless $quiet;
501 # "send lots of email" format,
504 # So let's support that, too.
506 printf("(non-mbox) Adding cc: %s from line '%s'\n",
507 $_, $_) unless $quiet;
511 } elsif (!defined $subject) {
516 # A whitespace line will terminate the headers
522 if (/^Signed-off-by: (.*)$/i && !$no_signed_off_cc) {
526 printf("(sob) Adding cc: %s from line '%s'\n",
527 $c, $_) unless $quiet;
532 if (defined $author_not_sender) {
533 $message = "From: $author_not_sender\n\n$message";
536 $cc = join(", ", unique_email_list
(@cc));
540 # set up for the next message
541 if ($chain_reply_to || !defined $reply_to || length($reply_to) == 0) {
542 $reply_to = $message_id;
543 if (length $references > 0) {
544 $references .= " $message_id";
546 $references = "$message_id";
553 cleanup_compose_files
();
556 sub cleanup_compose_files
() {
557 unlink($compose_filename, $compose_filename . ".final");
561 $smtp->quit if $smtp;
563 sub unique_email_list
(@
) {
567 foreach my $entry (@_) {
568 if (my $clean = extract_valid_address
($entry)) {
570 next if $seen{$clean}++;
571 push @emails, $entry;
573 print STDERR
"W: unable to extract a valid address",