2 # horrible hack of a script to send off a large number of email messages, one after
3 # each other, all chained together. This is useful for large numbers of patches.
5 # Use at your own risk!!!!
7 # greg kroah-hartman Jan 8, 2002
10 # GPL v2 (See COPYING)
12 # Ported to support git "mbox" format files by Ryan Anderson <ryan@michonline.com>
14 # Sends emails to the email listed on the command line.
16 # updated to give a valid subject and CC the owner of the patch - Jan 2005
17 # first line of the message is who to CC,
18 # and second line is the subject of the message.
24 use Mail::Sendmail qw(sendmail %mailcfg);
29 # Variables we fill in automatically, or via prompting:
30 my (@to,@cc,$initial_reply_to,$initial_subject,@files,$from);
33 #$initial_reply_to = ''; #<20050203173208.GA23964@foobar.com>';
35 my $term = new Term::ReadLine 'git-send-email';
37 # Begin by accumulating all the variables (defined above), that we will end up
38 # needing, first, from the command line:
40 my $rc = GetOptions("from=s" => \$from,
41 "in-reply-to=s" => \$initial_reply_to,
42 "subject=s" => \$initial_subject,
46 # Now, let's fill any that aren't set in with defaults:
48 open(GITVAR,"-|","git-var","-l")
49 or die "Failed to open pipe from git-var: $!";
51 my ($author,$committer);
54 my ($var,$data) = split /=/,$_,2;
55 my @fields = split /\s+/, $data;
57 my $ident = join(" ", @fields[0...(@fields-3)]);
59 if ($var eq 'GIT_AUTHOR_IDENT') {
61 } elsif ($var eq 'GIT_COMMITTER_IDENT') {
69 $from = $author || $committer;
70 1 while (!defined ($_ = $term->readline("Who should the emails appear to be from? ",
73 print "Emails will be sent from: ", $from, "\n";
77 1 while (!defined ($_ = $term->readline("Who should the emails be sent to? ",
80 push @to, split /,/, $to;
83 if (!defined $initial_subject) {
84 1 while (!defined ($_ =
85 $term->readline("What subject should the emails start with? ",
87 $initial_subject = $_;
90 if (!defined $initial_reply_to) {
91 1 while (!defined ($_ =
92 $term->readline("Message-ID to be used as In-Reply-To? ",
94 $initial_reply_to = $_;
97 # Now that all the defaults are set, process the rest of the command line
98 # arguments and collect up the files that need to be processed.
102 or die "Failed to opendir $f: $!";
104 push @files, map { +$f . "/" . $_ } grep !/^\.{1,2}$/,
110 print STDERR "Skipping $f - not found.\n";
115 print $_,"\n" for @files;
118 git-send-email-script [options] <file | directory> [... file | directory ]
120 --from Specify the "From:" line of the email to be sent.
121 --to Specify the primary "To:" line of the email.
122 --subject Specify the initial "Subject:" line.
123 --in-reply-to Specify the first "In-Reply-To:" header line.
125 Error: Please specify a file or a directory on the command line.
130 # Variables we set as part of the loop over files
131 our ($message_id, $cc, %mail, $subject, $reply_to, $message);
134 # Usually don't need to change anything below here.
136 # we make a "fake" message id by taking the current number
137 # of seconds since the beginning of Unix time and tacking on
138 # a random number to the end, in case we are called quicker than
139 # 1 second since the last time we were called.
142 my $date = `date "+\%s"`;
144 my $pseudo_rand = int (rand(4200));
145 $message_id = "<$date$pseudo_rand\@foobar.com>";
146 print "new message id = $message_id\n";
156 $to{lc(Email::Valid->address($_))}++ for (@to);
158 my $to = join(",", keys %to);
166 'In-Reply-To' => $reply_to,
167 'Message-ID' => $message_id,
168 'X-Mailer' => "git-send-email-script",
171 $mail{smtp} = 'localhost';
174 #print Data::Dumper->Dump([\%mail],[qw(*mail)]);
176 sendmail(%mail) or die $Mail::Sendmail::error;
178 print "OK. Log says:\n", $Mail::Sendmail::log;
183 $reply_to = $initial_reply_to;
185 $subject = $initial_subject;
187 foreach my $t (@files) {
189 open(F,"<",$t) or die "can't open file $t";
197 $found_mbox = 1, next if (/^From /);
201 if (/^Subject:\s+(.*)$/) {
204 } elsif (/^(Cc|From):\s+(.*)$/) {
205 printf("(mbox) Adding cc: %s from line '%s'\n",
212 # "send lots of email" format,
215 # So let's support that, too.
217 printf("(non-mbox) Adding cc: %s from line '%s'\n",
222 } elsif (!defined $subject) {
227 # A whitespace line will terminate the headers
233 if (/^Signed-off-by: (.*)$/i) {
237 printf("(sob) Adding cc: %s from line '%s'\n",
245 $clean_ccs{lc(Email::Valid->address($_))}++ for @cc;
247 $cc = join(",", keys %clean_ccs);
251 # set up for the next message
252 $reply_to = $message_id;
254 # $subject = "Re: ".$initial_subject;