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);
30 # Behavior modification variables
31 my ($chain_reply_to) = (1);
34 #$initial_reply_to = ''; #<20050203173208.GA23964@foobar.com>';
36 my $term = new Term::ReadLine 'git-send-email';
38 # Begin by accumulating all the variables (defined above), that we will end up
39 # needing, first, from the command line:
41 my $rc = GetOptions("from=s" => \$from,
42 "in-reply-to=s" => \$initial_reply_to,
43 "subject=s" => \$initial_subject,
45 "chain-reply-to!" => \$chain_reply_to,
48 # Now, let's fill any that aren't set in with defaults:
50 open(GITVAR,"-|","git-var","-l")
51 or die "Failed to open pipe from git-var: $!";
53 my ($author,$committer);
56 my ($var,$data) = split /=/,$_,2;
57 my @fields = split /\s+/, $data;
59 my $ident = join(" ", @fields[0...(@fields-3)]);
61 if ($var eq 'GIT_AUTHOR_IDENT') {
63 } elsif ($var eq 'GIT_COMMITTER_IDENT') {
71 $from = $author || $committer;
72 1 while (!defined ($_ = $term->readline("Who should the emails appear to be from? ",
75 print "Emails will be sent from: ", $from, "\n";
79 1 while (!defined ($_ = $term->readline("Who should the emails be sent to? ",
82 push @to, split /,/, $to;
85 if (!defined $initial_subject) {
86 1 while (!defined ($_ =
87 $term->readline("What subject should the emails start with? ",
89 $initial_subject = $_;
92 if (!defined $initial_reply_to) {
93 1 while (!defined ($_ =
94 $term->readline("Message-ID to be used as In-Reply-To? ",
96 $initial_reply_to = $_;
97 $initial_reply_to =~ s/(^\s+|\s+$)//g;
100 # Now that all the defaults are set, process the rest of the command line
101 # arguments and collect up the files that need to be processed.
105 or die "Failed to opendir $f: $!";
107 push @files, map { +$f . "/" . $_ } grep !/^\.{1,2}$/,
113 print STDERR "Skipping $f - not found.\n";
118 print $_,"\n" for @files;
121 git-send-email-script [options] <file | directory> [... file | directory ]
123 --from Specify the "From:" line of the email to be sent.
124 --to Specify the primary "To:" line of the email.
125 --subject Specify the initial "Subject:" line.
126 --in-reply-to Specify the first "In-Reply-To:" header line.
127 --chain-reply-to If set, the replies will all be to the first
128 email sent, rather than to the last email sent.
130 Error: Please specify a file or a directory on the command line.
135 # Variables we set as part of the loop over files
136 our ($message_id, $cc, %mail, $subject, $reply_to, $message);
139 # Usually don't need to change anything below here.
141 # we make a "fake" message id by taking the current number
142 # of seconds since the beginning of Unix time and tacking on
143 # a random number to the end, in case we are called quicker than
144 # 1 second since the last time we were called.
147 my $date = `date "+\%s"`;
149 my $pseudo_rand = int (rand(4200));
150 $message_id = "<$date$pseudo_rand\@foobar.com>";
151 print "new message id = $message_id\n";
161 $to{lc(Email::Valid->address($_))}++ for (@to);
163 my $to = join(",", keys %to);
171 'In-Reply-To' => $reply_to,
172 'Message-ID' => $message_id,
173 'X-Mailer' => "git-send-email-script",
176 $mail{smtp} = 'localhost';
179 #print Data::Dumper->Dump([\%mail],[qw(*mail)]);
181 sendmail(%mail) or die $Mail::Sendmail::error;
183 print "OK. Log says:\n", $Mail::Sendmail::log;
188 $reply_to = $initial_reply_to;
190 $subject = $initial_subject;
192 foreach my $t (@files) {
194 open(F,"<",$t) or die "can't open file $t";
202 $found_mbox = 1, next if (/^From /);
206 if (/^Subject:\s+(.*)$/) {
209 } elsif (/^(Cc|From):\s+(.*)$/) {
210 printf("(mbox) Adding cc: %s from line '%s'\n",
217 # "send lots of email" format,
220 # So let's support that, too.
222 printf("(non-mbox) Adding cc: %s from line '%s'\n",
227 } elsif (!defined $subject) {
232 # A whitespace line will terminate the headers
238 if (/^Signed-off-by: (.*)$/i) {
242 printf("(sob) Adding cc: %s from line '%s'\n",
250 $clean_ccs{lc(Email::Valid->address($_))}++ for @cc;
252 $cc = join(",", keys %clean_ccs);
256 # set up for the next message
257 if ($chain_reply_to || length($reply_to) == 0) {
258 $reply_to = $message_id;
261 # $subject = "Re: ".$initial_subject;