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(@);
28 sub cleanup_compose_files();
30 # Constants (essentially)
31 my $compose_filename = ".msg.$$";
33 # Variables we fill in automatically, or via prompting:
34 my (@to,@cc,$initial_reply_to,$initial_subject,@files,$from,$compose);
36 # Behavior modification variables
37 my ($chain_reply_to, $smtp_server) = (1, "localhost");
40 #$initial_reply_to = ''; #<20050203173208.GA23964@foobar.com>';
42 my $term = new Term::ReadLine 'git-send-email';
44 # Begin by accumulating all the variables (defined above), that we will end up
45 # needing, first, from the command line:
47 my $rc = GetOptions("from=s" => \$from,
48 "in-reply-to=s" => \$initial_reply_to,
49 "subject=s" => \$initial_subject,
51 "chain-reply-to!" => \$chain_reply_to,
52 "smtp-server=s" => \$smtp_server,
53 "compose" => \$compose,
56 # Now, let's fill any that aren't set in with defaults:
58 open(GITVAR,"-|","git-var","-l")
59 or die "Failed to open pipe from git-var: $!";
61 my ($author,$committer);
64 my ($var,$data) = split /=/,$_,2;
65 my @fields = split /\s+/, $data;
67 my $ident = join(" ", @fields[0...(@fields-3)]);
69 if ($var eq 'GIT_AUTHOR_IDENT') {
71 } elsif ($var eq 'GIT_COMMITTER_IDENT') {
79 $from = $author || $committer;
81 $_ = $term->readline("Who should the emails appear to be from? ",
83 } while (!defined $_);
86 print "Emails will be sent from: ", $from, "\n";
92 $_ = $term->readline("Who should the emails be sent to? ",
94 } while (!defined $_);
96 push @to, split /,/, $to;
100 if (!defined $initial_subject && $compose) {
102 $_ = $term->readline("What subject should the emails start with? ",
104 } while (!defined $_);
105 $initial_subject = $_;
109 if (!defined $initial_reply_to && $prompting) {
111 $_= $term->readline("Message-ID to be used as In-Reply-To for the first email? ",
113 } while (!defined $_);
115 $initial_reply_to = $_;
116 $initial_reply_to =~ s/(^\s+|\s+$)//g;
119 if (!defined $smtp_server) {
120 $smtp_server = "localhost";
124 # Note that this does not need to be secure, but we will make a small
125 # effort to have it be unique
126 open(C,">",$compose_filename)
127 or die "Failed to open for writing $compose_filename: $!";
129 printf C "Subject: %s\n\n", $initial_subject;
131 GIT: Please enter your email below.
132 GIT: Lines beginning in "GIT: " will be removed.
133 GIT: Consider including an overall diffstat or table of contents
134 GIT: for the patch you are writing.
139 my $editor = $ENV{EDITOR};
140 $editor = 'vi' unless defined $editor;
141 system($editor, $compose_filename);
143 open(C2,">",$compose_filename . ".final")
144 or die "Failed to open $compose_filename.final : " . $!;
146 open(C,"<",$compose_filename)
147 or die "Failed to open $compose_filename : " . $!;
157 $_ = $term->readline("Send this email? (y|n) ");
158 } while (!defined $_);
160 if (uc substr($_,0,1) ne 'Y') {
161 cleanup_compose_files();
165 @files = ($compose_filename . ".final");
169 # Now that all the defaults are set, process the rest of the command line
170 # arguments and collect up the files that need to be processed.
174 or die "Failed to opendir $f: $!";
176 push @files, grep { -f $_ } map { +$f . "/" . $_ }
183 print STDERR "Skipping $f - not found.\n";
188 print $_,"\n" for @files;
191 git-send-email [options] <file | directory> [... file | directory ]
193 --from Specify the "From:" line of the email to be sent.
195 --to Specify the primary "To:" line of the email.
197 --compose Use \$EDITOR to edit an introductory message for the
200 --subject Specify the initial "Subject:" line.
201 Only necessary if --compose is also set. If --compose
202 is not set, this will be prompted for.
204 --in-reply-to Specify the first "In-Reply-To:" header line.
205 Only used if --compose is also set. If --compose is not
206 set, this will be prompted for.
208 --chain-reply-to If set, the replies will all be to the previous
209 email sent, rather than to the first email sent.
212 --smtp-server If set, specifies the outgoing SMTP server to use.
213 Defaults to localhost.
215 Error: Please specify a file or a directory on the command line.
220 # Variables we set as part of the loop over files
221 our ($message_id, $cc, %mail, $subject, $reply_to, $message);
224 # Usually don't need to change anything below here.
226 # we make a "fake" message id by taking the current number
227 # of seconds since the beginning of Unix time and tacking on
228 # a random number to the end, in case we are called quicker than
229 # 1 second since the last time we were called.
231 # We'll setup a template for the message id, using the "from" address:
232 my $message_id_from = Email::Valid->address($from);
233 my $message_id_template = "<%s-git-send-email-$message_id_from>";
237 my $date = `date "+\%s"`;
239 my $pseudo_rand = int (rand(4200));
240 $message_id = sprintf $message_id_template, "$date$pseudo_rand";
241 #print "new message id = $message_id\n"; # Was useful for debugging
250 my $to = join (", ", unique_email_list(@to));
258 'In-Reply-To' => $reply_to,
259 'Message-ID' => $message_id,
260 'X-Mailer' => "git-send-email",
263 $mail{smtp} = $smtp_server;
266 #print Data::Dumper->Dump([\%mail],[qw(*mail)]);
268 sendmail(%mail) or die $Mail::Sendmail::error;
270 print "OK. Log says:\n", $Mail::Sendmail::log;
275 $reply_to = $initial_reply_to;
277 $subject = $initial_subject;
279 foreach my $t (@files) {
281 open(F,"<",$t) or die "can't open file $t";
289 $found_mbox = 1, next if (/^From /);
293 if (/^Subject:\s+(.*)$/) {
296 } elsif (/^(Cc|From):\s+(.*)$/) {
297 printf("(mbox) Adding cc: %s from line '%s'\n",
304 # "send lots of email" format,
307 # So let's support that, too.
309 printf("(non-mbox) Adding cc: %s from line '%s'\n",
314 } elsif (!defined $subject) {
319 # A whitespace line will terminate the headers
325 if (/^Signed-off-by: (.*)$/i) {
329 printf("(sob) Adding cc: %s from line '%s'\n",
336 $cc = join(", ", unique_email_list(@cc));
340 # set up for the next message
341 if ($chain_reply_to || length($reply_to) == 0) {
342 $reply_to = $message_id;
348 cleanup_compose_files();
351 sub cleanup_compose_files() {
352 unlink($compose_filename, $compose_filename . ".final");
358 sub unique_email_list(@) {
362 foreach my $entry (@_) {
363 my $clean = Email::Valid->address($entry);
364 next if $seen{$clean}++;
365 push @emails, $entry;