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, $quiet, $suppress_from, $no_signed_off_cc) = (1, "localhost", 0, 0, 0);
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,
55 "suppress-from" => \$suppress_from,
56 "no-signed-off-cc" => \$no_signed_off_cc,
59 # Now, let's fill any that aren't set in with defaults:
61 open(GITVAR,"-|","git-var","-l")
62 or die "Failed to open pipe from git-var: $!";
64 my ($author,$committer);
67 my ($var,$data) = split /=/,$_,2;
68 my @fields = split /\s+/, $data;
70 my $ident = join(" ", @fields[0...(@fields-3)]);
72 if ($var eq 'GIT_AUTHOR_IDENT') {
74 } elsif ($var eq 'GIT_COMMITTER_IDENT') {
82 $from = $author || $committer;
84 $_ = $term->readline("Who should the emails appear to be from? ",
86 } while (!defined $_);
89 print "Emails will be sent from: ", $from, "\n";
95 $_ = $term->readline("Who should the emails be sent to? ",
97 } while (!defined $_);
99 push @to, split /,/, $to;
103 if (!defined $initial_subject && $compose) {
105 $_ = $term->readline("What subject should the emails start with? ",
107 } while (!defined $_);
108 $initial_subject = $_;
112 if (!defined $initial_reply_to && $prompting) {
114 $_= $term->readline("Message-ID to be used as In-Reply-To for the first email? ",
116 } while (!defined $_);
118 $initial_reply_to = $_;
119 $initial_reply_to =~ s/(^\s+|\s+$)//g;
122 if (!defined $smtp_server) {
123 $smtp_server = "localhost";
127 # Note that this does not need to be secure, but we will make a small
128 # effort to have it be unique
129 open(C,">",$compose_filename)
130 or die "Failed to open for writing $compose_filename: $!";
131 print C "From $from # This line is ignored.\n";
132 printf C "Subject: %s\n\n", $initial_subject;
134 GIT: Please enter your email below.
135 GIT: Lines beginning in "GIT: " will be removed.
136 GIT: Consider including an overall diffstat or table of contents
137 GIT: for the patch you are writing.
142 my $editor = $ENV{EDITOR};
143 $editor = 'vi' unless defined $editor;
144 system($editor, $compose_filename);
146 open(C2,">",$compose_filename . ".final")
147 or die "Failed to open $compose_filename.final : " . $!;
149 open(C,"<",$compose_filename)
150 or die "Failed to open $compose_filename : " . $!;
160 $_ = $term->readline("Send this email? (y|n) ");
161 } while (!defined $_);
163 if (uc substr($_,0,1) ne 'Y') {
164 cleanup_compose_files();
168 @files = ($compose_filename . ".final");
172 # Now that all the defaults are set, process the rest of the command line
173 # arguments and collect up the files that need to be processed.
177 or die "Failed to opendir $f: $!";
179 push @files, grep { -f $_ } map { +$f . "/" . $_ }
186 print STDERR "Skipping $f - not found.\n";
192 print $_,"\n" for (@files);
196 git-send-email [options] <file | directory> [... file | directory ]
198 --from Specify the "From:" line of the email to be sent.
200 --to Specify the primary "To:" line of the email.
202 --compose Use \$EDITOR to edit an introductory message for the
205 --subject Specify the initial "Subject:" line.
206 Only necessary if --compose is also set. If --compose
207 is not set, this will be prompted for.
209 --in-reply-to Specify the first "In-Reply-To:" header line.
210 Only used if --compose is also set. If --compose is not
211 set, this will be prompted for.
213 --chain-reply-to If set, the replies will all be to the previous
214 email sent, rather than to the first email sent.
217 --no-signed-off-cc Suppress the automatic addition of email addresses
218 that appear in a Signed-off-by: line, to the cc: list.
219 Note: Using this option is not recommended.
221 --smtp-server If set, specifies the outgoing SMTP server to use.
222 Defaults to localhost.
224 --suppress-from Supress sending emails to yourself if your address
225 appears in a From: line.
227 --quiet Make git-send-email less verbose. One line per email should be
230 Error: Please specify a file or a directory on the command line.
235 # Variables we set as part of the loop over files
236 our ($message_id, $cc, %mail, $subject, $reply_to, $message);
239 # Usually don't need to change anything below here.
241 # we make a "fake" message id by taking the current number
242 # of seconds since the beginning of Unix time and tacking on
243 # a random number to the end, in case we are called quicker than
244 # 1 second since the last time we were called.
246 # We'll setup a template for the message id, using the "from" address:
247 my $message_id_from = Email::Valid->address($from);
248 my $message_id_template = "<%s-git-send-email-$message_id_from>";
252 my $date = `date "+\%s"`;
254 my $pseudo_rand = int (rand(4200));
255 $message_id = sprintf $message_id_template, "$date$pseudo_rand";
256 #print "new message id = $message_id\n"; # Was useful for debugging
265 my $to = join (", ", unique_email_list(@to));
273 'In-Reply-To' => $reply_to,
274 'Message-ID' => $message_id,
275 'X-Mailer' => "git-send-email",
278 $mail{smtp} = $smtp_server;
281 #print Data::Dumper->Dump([\%mail],[qw(*mail)]);
283 sendmail(%mail) or die $Mail::Sendmail::error;
286 printf "Sent %s\n", $subject;
288 print "OK. Log says:\n", $Mail::Sendmail::log;
294 $reply_to = $initial_reply_to;
296 $subject = $initial_subject;
298 foreach my $t (@files) {
299 open(F,"<",$t) or die "can't open file $t";
307 $found_mbox = 1, next if (/^From /);
311 if (/^Subject:\s+(.*)$/) {
314 } elsif (/^(Cc|From):\s+(.*)$/) {
315 next if ($2 eq $from && $suppress_from);
316 printf("(mbox) Adding cc: %s from line '%s'\n",
317 $2, $_) unless $quiet;
323 # "send lots of email" format,
326 # So let's support that, too.
328 printf("(non-mbox) Adding cc: %s from line '%s'\n",
329 $_, $_) unless $quiet;
333 } elsif (!defined $subject) {
338 # A whitespace line will terminate the headers
344 if (/^Signed-off-by: (.*)$/i && !$no_signed_off_cc) {
348 printf("(sob) Adding cc: %s from line '%s'\n",
349 $c, $_) unless $quiet;
355 $cc = join(", ", unique_email_list(@cc));
359 # set up for the next message
360 if ($chain_reply_to || length($reply_to) == 0) {
361 $reply_to = $message_id;
367 cleanup_compose_files();
370 sub cleanup_compose_files() {
371 unlink($compose_filename, $compose_filename . ".final");
377 sub unique_email_list(@) {
381 foreach my $entry (@_) {
382 my $clean = Email::Valid->address($entry);
383 next if $seen{$clean}++;
384 push @emails, $entry;