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.
22 use Mail::Sendmail qw(sendmail %mailcfg);
27 # Variables we fill in automatically, or via prompting:
28 my (@to,@cc,$initial_reply_to,$initial_subject,@files,$from);
31 #$initial_reply_to = ''; #<20050203173208.GA23964@foobar.com>';
33 my $term = new Term::ReadLine 'git-send-email';
35 # Begin by accumulating all the variables (defined above), that we will end up
36 # needing, first, from the command line:
38 my $rc = GetOptions("from=s" => \$from,
39 "in-reply-to=s" => \$initial_reply_to,
40 "subject=s" => \$initial_subject,
44 # Now, let's fill any that aren't set in with defaults:
46 open(GITVAR,"-|","git-var","-l")
47 or die "Failed to open pipe from git-var: $!";
49 my ($author,$committer);
52 my ($var,$data) = split /=/,$_,2;
53 my @fields = split /\s+/, $data;
55 my $ident = join(" ", @fields[0...(@fields-3)]);
57 if ($var eq 'GIT_AUTHOR_IDENT') {
59 } elsif ($var eq 'GIT_COMMITTER_IDENT') {
67 $from = $author || $committer;
68 1 while (!defined ($_ = $term->readline("Who should the emails appear to be from? ",
71 print "Emails will be sent from: ", $from, "\n";
75 1 while (!defined ($_ = $term->readline("Who should the emails be sent to? ",
78 push @to, split /,/, $to;
81 if (!defined $initial_subject) {
82 1 while (!defined ($_ =
83 $term->readline("What subject should the emails start with? ",
85 $initial_subject = $_;
88 if (!defined $initial_reply_to) {
89 1 while (!defined ($_ =
90 $term->readline("Message-ID to be used as In-Reply-To? ",
92 $initial_reply_to = $_;
95 # Now that all the defaults are set, process the rest of the command line
96 # arguments and collect up the files that need to be processed.
100 or die "Failed to opendir $f: $!";
102 push @files, map { +$f . "/" . $_ } grep !/^\.{1,2}$/,
108 print STDERR "Skipping $f - not found.\n";
113 print $_,"\n" for @files;
116 git-send-email-script [options] <file | directory> [... file | directory ]
118 --from Specify the "From:" line of the email to be sent.
119 --to Specify the primary "To:" line of the email.
120 --subject Specify the initial "Subject:" line.
121 --in-reply-to Specify the first "In-Reply-To:" header line.
123 Error: Please specify a file or a directory on the command line.
128 # Variables we set as part of the loop over files
129 our ($message_id, $cc, %mail, $subject, $reply_to, $message);
132 # Usually don't need to change anything below here.
134 # we make a "fake" message id by taking the current number
135 # of seconds since the beginning of Unix time and tacking on
136 # a random number to the end, in case we are called quicker than
137 # 1 second since the last time we were called.
140 my $date = `date "+\%s"`;
142 my $pseudo_rand = int (rand(4200));
143 $message_id = "<$date$pseudo_rand\@foobar.com>";
144 print "new message id = $message_id\n";
154 $to{lc(Email::Valid->address($_))}++ for (@to);
156 my $to = join(",", keys %to);
164 'In-Reply-To' => $reply_to,
165 'Message-ID' => $message_id,
166 'X-Mailer' => "git-send-email-script",
169 $mail{smtp} = 'localhost';
172 #print Data::Dumper->Dump([\%mail],[qw(*mail)]);
174 sendmail(%mail) or die $Mail::Sendmail::error;
176 print "OK. Log says:\n", $Mail::Sendmail::log;
181 $reply_to = $initial_reply_to;
183 $subject = $initial_subject;
185 foreach my $t (@files) {
187 open(F,"<",$t) or die "can't open file $t";
195 $found_mbox = 1, next if (/^From /);
199 if (/^Subject:\s+(.*)$/) {
202 } elsif (/^(Cc|From):\s+(.*)$/) {
203 printf("(mbox) Adding cc: %s from line '%s'\n",
210 # "send lots of email" format,
213 # So let's support that, too.
215 printf("(non-mbox) Adding cc: %s from line '%s'\n",
220 } elsif (!defined $subject) {
225 # A whitespace line will terminate the headers
231 if (/^Signed-off-by: (.*)$/i) {
235 printf("(sob) Adding cc: %s from line '%s'\n",
243 $clean_ccs{lc(Email::Valid->address($_))}++ for @cc;
245 $cc = join(",", keys %clean_ccs);
249 # set up for the next message
250 $reply_to = $message_id;
252 # $subject = "Re: ".$initial_subject;