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 sub unique_email_list(@);
29 # Variables we fill in automatically, or via prompting:
30 my (@to,@cc,$initial_reply_to,$initial_subject,@files,$from);
32 # Behavior modification variables
33 my ($chain_reply_to, $smtp_server) = (1, "localhost");
36 #$initial_reply_to = ''; #<20050203173208.GA23964@foobar.com>';
38 my $term = new Term::ReadLine 'git-send-email';
40 # Begin by accumulating all the variables (defined above), that we will end up
41 # needing, first, from the command line:
43 my $rc = GetOptions("from=s" => \$from,
44 "in-reply-to=s" => \$initial_reply_to,
45 "subject=s" => \$initial_subject,
47 "chain-reply-to!" => \$chain_reply_to,
48 "smtp-server=s" => \$smtp_server,
51 # Now, let's fill any that aren't set in with defaults:
53 open(GITVAR,"-|","git-var","-l")
54 or die "Failed to open pipe from git-var: $!";
56 my ($author,$committer);
59 my ($var,$data) = split /=/,$_,2;
60 my @fields = split /\s+/, $data;
62 my $ident = join(" ", @fields[0...(@fields-3)]);
64 if ($var eq 'GIT_AUTHOR_IDENT') {
66 } elsif ($var eq 'GIT_COMMITTER_IDENT') {
74 $from = $author || $committer;
76 $_ = $term->readline("Who should the emails appear to be from? ",
78 } while (!defined $_);
81 print "Emails will be sent from: ", $from, "\n";
86 $_ = $term->readline("Who should the emails be sent to? ",
88 } while (!defined $_);
90 push @to, split /,/, $to;
93 if (!defined $initial_subject) {
95 $_ = $term->readline("What subject should the emails start with? ",
97 } while (!defined $_);
98 $initial_subject = $_;
101 if (!defined $initial_reply_to) {
103 $_= $term->readline("Message-ID to be used as In-Reply-To? ",
105 } while (!defined $_);
107 $initial_reply_to = $_;
108 $initial_reply_to =~ s/(^\s+|\s+$)//g;
111 if (!defined $smtp_server) {
112 $smtp_server = "localhost";
115 # Now that all the defaults are set, process the rest of the command line
116 # arguments and collect up the files that need to be processed.
120 or die "Failed to opendir $f: $!";
122 push @files, grep { -f $_ } map { +$f . "/" . $_ }
129 print STDERR "Skipping $f - not found.\n";
134 print $_,"\n" for @files;
137 git-send-email-script [options] <file | directory> [... file | directory ]
139 --from Specify the "From:" line of the email to be sent.
140 --to Specify the primary "To:" line of the email.
141 --subject Specify the initial "Subject:" line.
142 --in-reply-to Specify the first "In-Reply-To:" header line.
143 --chain-reply-to If set, the replies will all be to the previous
144 email sent, rather than to the first email sent.
146 --smtp-server If set, specifies the outgoing SMTP server to use.
147 Defaults to localhost.
149 Error: Please specify a file or a directory on the command line.
154 # Variables we set as part of the loop over files
155 our ($message_id, $cc, %mail, $subject, $reply_to, $message);
158 # Usually don't need to change anything below here.
160 # we make a "fake" message id by taking the current number
161 # of seconds since the beginning of Unix time and tacking on
162 # a random number to the end, in case we are called quicker than
163 # 1 second since the last time we were called.
165 # We'll setup a template for the message id, using the "from" address:
166 my $message_id_from = Email::Valid->address($from);
167 my $message_id_template = "<%s-git-send-email-$message_id_from>";
171 my $date = `date "+\%s"`;
173 my $pseudo_rand = int (rand(4200));
174 $message_id = sprintf $message_id_template, "$date$pseudo_rand";
175 #print "new message id = $message_id\n"; # Was useful for debugging
184 my $to = join (", ", unique_email_list(@to));
192 'In-Reply-To' => $reply_to,
193 'Message-ID' => $message_id,
194 'X-Mailer' => "git-send-email-script",
197 $mail{smtp} = $smtp_server;
200 #print Data::Dumper->Dump([\%mail],[qw(*mail)]);
202 sendmail(%mail) or die $Mail::Sendmail::error;
204 print "OK. Log says:\n", $Mail::Sendmail::log;
209 $reply_to = $initial_reply_to;
211 $subject = $initial_subject;
213 foreach my $t (@files) {
215 open(F,"<",$t) or die "can't open file $t";
223 $found_mbox = 1, next if (/^From /);
227 if (/^Subject:\s+(.*)$/) {
230 } elsif (/^(Cc|From):\s+(.*)$/) {
231 printf("(mbox) Adding cc: %s from line '%s'\n",
238 # "send lots of email" format,
241 # So let's support that, too.
243 printf("(non-mbox) Adding cc: %s from line '%s'\n",
248 } elsif (!defined $subject) {
253 # A whitespace line will terminate the headers
259 if (/^Signed-off-by: (.*)$/i) {
263 printf("(sob) Adding cc: %s from line '%s'\n",
270 $cc = join(", ", unique_email_list(@cc));
274 # set up for the next message
275 if ($chain_reply_to || length($reply_to) == 0) {
276 $reply_to = $message_id;
282 sub unique_email_list(@) {
286 foreach my $entry (@_) {
287 my $clean = Email::Valid->address($entry);
288 next if $seen{$clean}++;
289 push @emails, $entry;