@matheusbsbs / matheus-bagliano / commits / aca7ad7628
Fork 0

send-email: allow sendmail binary to be used instead of SMTP

This should make local mailing possible for machines without a connection to an SMTP server. It'll default to using /usr/sbin/sendmail or /usr/lib/sendmail if no SMTP server is specified (the default). If it can't find either of those paths, it'll fall back to connecting to an SMTP server on localhost. Signed-off-by: Eric Wong <normalperson@yhbt.net> Signed-off-by: Junio C Hamano <junkio@cox.net>

Eric Wong committed May 15, 2006 at 02:34 UTC aca7ad7628ce72afbd1d0d99778c9a5c9cac7a7a
1 file changed +40 -20
git-send-email.perl
+40 -20
@@ -40,7 +40,8 @@ my $compose_filename = ".msg.$$";
40 my (@to,@cc,@initial_cc,$initial_reply_to,$initial_subject,@files,$from,$compose,$time);
41
42 # Behavior modification variables
43 -my ($chain_reply_to, $smtp_server, $quiet, $suppress_from, $no_signed_off_cc) = (1, "localhost", 0, 0, 0);
43 +my ($chain_reply_to, $quiet, $suppress_from, $no_signed_off_cc) = (1, 0, 0, 0);
44 +my $smtp_server;
45
46 # Example reply to:
47 #$initial_reply_to = ''; #<20050203173208.GA23964@foobar.com>';
@@ -179,8 +180,14 @@ if (!defined $initial_reply_to && $prompting) {
180 $initial_reply_to =~ s/(^\s+|\s+$)//g;
181 }
182
182 -if (!defined $smtp_server) {
183 - $smtp_server = "localhost";
183 +if (!$smtp_server) {
184 + foreach (qw( /usr/sbin/sendmail /usr/lib/sendmail )) {
185 + if (-x $_) {
186 + $smtp_server = $_;
187 + last;
188 + }
189 + }
190 + $smtp_server ||= 'localhost'; # could be 127.0.0.1, too... *shrug*
191 }
192
193 if ($compose) {
@@ -358,26 +365,39 @@ X-Mailer: git-send-email $gitversion
365 ";
366 $header .= "In-Reply-To: $reply_to\n" if $reply_to;
367
361 - $smtp ||= Net::SMTP->new( $smtp_server );
362 - $smtp->mail( $from ) or die $smtp->message;
363 - $smtp->to( @recipients ) or die $smtp->message;
364 - $smtp->data or die $smtp->message;
365 - $smtp->datasend("$header\n$message") or die $smtp->message;
366 - $smtp->dataend() or die $smtp->message;
367 - $smtp->ok or die "Failed to send $subject\n".$smtp->message;
368 -
368 + if ($smtp_server =~ m#^/#) {
369 + my $pid = open my $sm, '|-';
370 + defined $pid or die $!;
371 + if (!$pid) {
372 + exec($smtp_server,'-i',@recipients) or die $!;
373 + }
374 + print $sm "$header\n$message";
375 + close $sm or die $?;
376 + } else {
377 + $smtp ||= Net::SMTP->new( $smtp_server );
378 + $smtp->mail( $from ) or die $smtp->message;
379 + $smtp->to( @recipients ) or die $smtp->message;
380 + $smtp->data or die $smtp->message;
381 + $smtp->datasend("$header\n$message") or die $smtp->message;
382 + $smtp->dataend() or die $smtp->message;
383 + $smtp->ok or die "Failed to send $subject\n".$smtp->message;
384 + }
385 if ($quiet) {
386 printf "Sent %s\n", $subject;
387 } else {
372 - print "OK. Log says:
373 -Date: $date
374 -Server: $smtp_server Port: 25
375 -From: $from
376 -Subject: $subject
377 -Cc: $cc
378 -To: $to
379 -
380 -Result: ", $smtp->code, ' ', ($smtp->message =~ /\n([^\n]+\n)$/s), "\n";
388 + print "OK. Log says:\nDate: $date\n";
389 + if ($smtp) {
390 + print "Server: $smtp_server\n";
391 + } else {
392 + print "Sendmail: $smtp_server\n";
393 + }
394 + print "From: $from\nSubject: $subject\nCc: $cc\nTo: $to\n\n";
395 + if ($smtp) {
396 + print "Result: ", $smtp->code, ' ',
397 + ($smtp->message =~ /\n([^\n]+\n)$/s), "\n";
398 + } else {
399 + print "Result: OK\n";
400 + }
401 }
402 }
403