提交 aca7ad76 编写于 作者: E Eric Wong 提交者: Junio C Hamano

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: NEric Wong <normalperson@yhbt.net>
Signed-off-by: NJunio C Hamano <junkio@cox.net>
上级 de1d4fa2
...@@ -40,7 +40,8 @@ ...@@ -40,7 +40,8 @@
my (@to,@cc,@initial_cc,$initial_reply_to,$initial_subject,@files,$from,$compose,$time); my (@to,@cc,@initial_cc,$initial_reply_to,$initial_subject,@files,$from,$compose,$time);
# Behavior modification variables # Behavior modification variables
my ($chain_reply_to, $smtp_server, $quiet, $suppress_from, $no_signed_off_cc) = (1, "localhost", 0, 0, 0); my ($chain_reply_to, $quiet, $suppress_from, $no_signed_off_cc) = (1, 0, 0, 0);
my $smtp_server;
# Example reply to: # Example reply to:
#$initial_reply_to = ''; #<20050203173208.GA23964@foobar.com>'; #$initial_reply_to = ''; #<20050203173208.GA23964@foobar.com>';
...@@ -179,8 +180,14 @@ sub expand_aliases { ...@@ -179,8 +180,14 @@ sub expand_aliases {
$initial_reply_to =~ s/(^\s+|\s+$)//g; $initial_reply_to =~ s/(^\s+|\s+$)//g;
} }
if (!defined $smtp_server) { if (!$smtp_server) {
$smtp_server = "localhost"; foreach (qw( /usr/sbin/sendmail /usr/lib/sendmail )) {
if (-x $_) {
$smtp_server = $_;
last;
}
}
$smtp_server ||= 'localhost'; # could be 127.0.0.1, too... *shrug*
} }
if ($compose) { if ($compose) {
...@@ -358,26 +365,39 @@ sub send_message ...@@ -358,26 +365,39 @@ sub send_message
"; ";
$header .= "In-Reply-To: $reply_to\n" if $reply_to; $header .= "In-Reply-To: $reply_to\n" if $reply_to;
$smtp ||= Net::SMTP->new( $smtp_server ); if ($smtp_server =~ m#^/#) {
$smtp->mail( $from ) or die $smtp->message; my $pid = open my $sm, '|-';
$smtp->to( @recipients ) or die $smtp->message; defined $pid or die $!;
$smtp->data or die $smtp->message; if (!$pid) {
$smtp->datasend("$header\n$message") or die $smtp->message; exec($smtp_server,'-i',@recipients) or die $!;
$smtp->dataend() or die $smtp->message; }
$smtp->ok or die "Failed to send $subject\n".$smtp->message; print $sm "$header\n$message";
close $sm or die $?;
} else {
$smtp ||= Net::SMTP->new( $smtp_server );
$smtp->mail( $from ) or die $smtp->message;
$smtp->to( @recipients ) or die $smtp->message;
$smtp->data or die $smtp->message;
$smtp->datasend("$header\n$message") or die $smtp->message;
$smtp->dataend() or die $smtp->message;
$smtp->ok or die "Failed to send $subject\n".$smtp->message;
}
if ($quiet) { if ($quiet) {
printf "Sent %s\n", $subject; printf "Sent %s\n", $subject;
} else { } else {
print "OK. Log says: print "OK. Log says:\nDate: $date\n";
Date: $date if ($smtp) {
Server: $smtp_server Port: 25 print "Server: $smtp_server\n";
From: $from } else {
Subject: $subject print "Sendmail: $smtp_server\n";
Cc: $cc }
To: $to print "From: $from\nSubject: $subject\nCc: $cc\nTo: $to\n\n";
if ($smtp) {
Result: ", $smtp->code, ' ', ($smtp->message =~ /\n([^\n]+\n)$/s), "\n"; print "Result: ", $smtp->code, ' ',
($smtp->message =~ /\n([^\n]+\n)$/s), "\n";
} else {
print "Result: OK\n";
}
} }
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册