send-email: allow sendmail binary to be used instead of SMTP
PATCH 1/1 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.
---
git-send-email.perl | 55 +++++++++++++++++++++++++++++++++++++++------------ 1 files changed, 42 insertions(+), 13 deletions(-)
1c9bacc5a2bfe382f68046aeba62302d28e4c976
git-send-email.perl +42 -13
@@ -40,7 +40,8 @@ # Variables we fill in automatically, or my (@to,@cc,@initial_cc,$initial_reply_to,$initial_subject,@files,$from,$compose,$time); # 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: #$initial_reply_to = ''; #<[email protected]>';
@@ -179,8 +180,14 @@ if (!defined $initial_reply_to && $promp $initial_reply_to =~ s/(^\s+|\s+$)//g; } -if (!defined $smtp_server) { - $smtp_server = "localhost"; +if (!$smtp_server) { + 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) {
@@ -358,18 +365,39 @@ X-Mailer: git-send-email $gitversion "; $header .= "In-Reply-To: $reply_to\n" if $reply_to; - $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 ($smtp_server =~ m#^/#) { + my $pid = open my $sm, '|-'; + defined $pid or die $!; + if (!$pid) { + exec($smtp_server,'-i',@recipients) or die $!; + } + print $sm "$header\n$message"; + close $sm or die $?; + if ($quiet) { + printf "Sent %s\n", $subject; + } else { + print "OK. Log says: +Date: $date +Sendmail: $smtp_server +From: $from +Subject: $subject +Cc: $cc +To: $to - if ($quiet) { - printf "Sent %s\n", $subject; +Result: OK\n"; + } } else { - print "OK. Log says: + $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) { + printf "Sent %s\n", $subject; + } else { + print "OK. Log says: Date: $date Server: $smtp_server Port: 25 From: $from
@@ -378,6 +406,7 @@ Cc: $cc To: $to Result: ", $smtp->code, ' ', ($smtp->message =~ /\n([^\n]+\n)$/s), "\n"; + } } }
Signature · 1 line
1.3.2.g1c9b
PATCH 1/1 send-email: quiet some warnings
---
git-send-email.perl | 12 ++++++++++-- 1 files changed, 10 insertions(+), 2 deletions(-)
0e5a33f56f62e527b74b2afbd93a1cca812f97af
git-send-email.perl +10 -2
@@ -507,8 +507,16 @@ sub unique_email_list(@) { my @emails; foreach my $entry (@_) { - my $clean = extract_valid_address($entry); - next if $seen{$clean}++; + if (my $clean = extract_valid_address($entry)) { + $seen{$clean} ||= 0; + next if $seen{$clean}++; + } else { + # it could still be a local email address without '@', + # which neither Email::Valid or our own small regex says + # is valid... + $seen{$entry} ||= 0; + next if $seen{$entry}++; + } push @emails, $entry; } return @emails;
Signature · 1 line
1.3.2.g0e5a
Re: [PATCH] send-email: quiet some warnings
Quoted 6 lines · Eric Wong
(quoted diff) +0
-2@@ -507,8 +507,16 @@ sub unique_email_list(@) { my @emails; foreach my $entry (@_) { - my $clean = extract_valid_address($entry); - next if $seen{$clean}++;
Review sub unique_email_list(@) { · line 519
Quoted 10 lines
(quoted diff) +10
-0+ if (my $clean = extract_valid_address($entry)) { + $seen{$clean} ||= 0; + next if $seen{$clean}++; + } else { + # it could still be a local email address without '@', + # which neither Email::Valid or our own small regex says + # is valid... + $seen{$entry} ||= 0; + next if $seen{$entry}++; + }
Wouldn't you want three kinds of return values from sub extract_valid_address() if you want to do this? That is,
(1) address is valid and this is the cleaned up value;
(2) address is known to be bogus; do not use it.
(3) address might be local.
And (1) and (3) are probably the same thing in practice. In a localsite setting, it is often convenient to be able to use addr-spec that is local-part only, so something like the attached change, with your error squelching for 'undef' return in the second case, might be more appropriate.
-- >8 --
git-send-email.perl +3 -0
@@ -300,6 +300,9 @@ our ($message_id, $cc, %mail, $subject, sub extract_valid_address { my $address = shift; + + return $address if ($address =~ /^(\w+)$/); + if ($have_email_valid) { return Email::Valid->address($address); } else {
PATCH 1/1 send-email: quiet some warnings, reject invalid addresses
I'm not sure why we never actually rejected invalid addresses in the first place. We just seemed to be using our email validity checkers to kill duplicates.
Now we just drop invalid email addresses completely and warn the user about it.
Since we support local sendmail, we'll also accept username-only addresses.
---
git-send-email.perl | 15 ++++++++++++--- 1 files changed, 12 insertions(+), 3 deletions(-)
f069e9a9cee726d82dfeee1d477152a0fe0651c1
git-send-email.perl +12 -3
@@ -307,6 +307,10 @@ our ($message_id, $cc, %mail, $subject, sub extract_valid_address { my $address = shift; + + # check for a local address: + return $address if ($address =~ /^([\w\-]+)$/); + if ($have_email_valid) { return Email::Valid->address($address); } else {
@@ -498,9 +502,14 @@ sub unique_email_list(@) { my @emails; foreach my $entry (@_) { - my $clean = extract_valid_address($entry); - next if $seen{$clean}++; - push @emails, $entry; + if (my $clean = extract_valid_address($entry)) { + $seen{$clean} ||= 0; + next if $seen{$clean}++; + push @emails, $entry; + } else { + print STDERR "W: unable to extract a valid address", + " from: $entry\n"; + } } return @emails; }
Signature · 1 line
1.3.2.g7d11
Quoted 9 lines · Eric Wong
This should make local mailing possible for machines without a connection to an SMTP server.
It'll default to using
/usr/sbin/sendmailor/usr/lib/sendmailif 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 <[email protected]>
Also, Thanks to Martin Langhoff for bringing this up.
Signature · 1 line
Eric Wong
Quoted 2 lines · Eric Wong
This should make local mailing possible for machines without a connection to an SMTP server.
Which is a good thing, but
Quoted 4 lines
It'll default to using
/usr/sbin/sendmailor/usr/lib/sendmailif 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.
I do not know if it is OK to change the default to first prefer local MDA executable and then "localhost". That is, ...
Quoted 7 lines
git-send-email.perl +1
-2@@ -179,8 +180,14 @@ if (!defined $initial_reply_to && $promp $initial_reply_to =~ s/(^\s+|\s+$)//g; } -if (!defined $smtp_server) { - $smtp_server = "localhost"; +if (!$smtp_server) {
git-send-email.perl if (!defined $initial_reply_to && $promp · line 193
Quoted 10 lines
git-send-email.perl +7
-0+ 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) {
Without this hunk, people who did not specify --smtp-server=host could get away with having anything that listens to 25/tcp on the localhost that is not either of the above two paths; now they have to explicitly say --smtp-server=localhost to defeat what this hunk does. I do not know if it is a big deal, though.
git-send-email.perl X-Mailer: git-send-email $gitversion · line 368
Quoted 1 line
git-send-email.perl +1
-0+ if ($smtp_server =~ m#^/#) {
I like this if(){}else{} here, but have a feeling that the logging part should be placed outside it to be shared.
While we are at it, we might want to enhance $smtp_server parsing to take host:port notation so that people can use message submission port 587/tcp (RFC 4409) instead.
Quoted 31 lines · Junio C Hamano
Quoted 2 lines · Eric Wong
This should make local mailing possible for machines without a connection to an SMTP server.
Which is a good thing, but
Quoted 4 lines
It'll default to using
/usr/sbin/sendmailor/usr/lib/sendmailif 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.I do not know if it is OK to change the default to first prefer local MDA executable and then "localhost". That is, ...
Quoted 16 lines
git-send-email.perl +8
-2@@ -179,8 +180,14 @@ if (!defined $initial_reply_to && $promp $initial_reply_to =~ s/(^\s+|\s+$)//g; } -if (!defined $smtp_server) { - $smtp_server = "localhost"; +if (!$smtp_server) { + foreach (qw( /usr/sbin/sendmail /usr/lib/sendmail )) { + if (-x $_) { + $smtp_server = $_; + last; + } + } + $smtp_server ||= 'localhost'; # could be 127.0.0.1, too... *shrug* }
Quoted 7 lines
git-send-email.perl if (!defined $initial_reply_to && $promp · line 193
Quoted 1 line
git-send-email.perl +0
-0if ($compose) {Without this hunk, people who did not specify --smtp-server=host could get away with having anything that listens to 25/tcp on the localhost that is not either of the above two paths; now they have to explicitly say --smtp-server=localhost to defeat what this hunk does. I do not know if it is a big deal, though.
I believe this is what Martin wanted. I think it's a good idea since sendmail binaries tend to be more flexible, but I'm ok with it either way.
Of course, Greg and Ryan were the original authors of this, so I'd like their take on it, too.
Quoted 4 lines
git-send-email.perl X-Mailer: git-send-email $gitversion · line 368
Quoted 1 line
git-send-email.perl +1
-0+ if ($smtp_server =~ m#^/#) {I like this if(){}else{} here, but have a feeling that the logging part should be placed outside it to be shared.
Cleaned that up a bit, patch coming. Also removed the Port: printout completely, as it's rather redundant (see below).
Quoted 3 lines
While we are at it, we might want to enhance $smtp_server parsing to take host:port notation so that people can use message submission port 587/tcp (RFC 4409) instead.
This already works, IO::Socket::INET (behind Net::SMTP) takes care of it :)
Signature · 1 line
Eric Wong
PATCH 1/1 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.
---
git-send-email.perl | 60 ++++++++++++++++++++++++++++++++++----------------- 1 files changed, 40 insertions(+), 20 deletions(-)
d4248a8ab7c883ab0f4dc080374bf60dc582f0f4
git-send-email.perl +40 -20
@@ -40,7 +40,8 @@ # Variables we fill in automatically, or my (@to,@cc,@initial_cc,$initial_reply_to,$initial_subject,@files,$from,$compose,$time); # 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: #$initial_reply_to = ''; #<[email protected]>';
@@ -179,8 +180,14 @@ if (!defined $initial_reply_to && $promp $initial_reply_to =~ s/(^\s+|\s+$)//g; } -if (!defined $smtp_server) { - $smtp_server = "localhost"; +if (!$smtp_server) { + 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) {
@@ -358,26 +365,39 @@ X-Mailer: git-send-email $gitversion "; $header .= "In-Reply-To: $reply_to\n" if $reply_to; - $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 ($smtp_server =~ m#^/#) { + my $pid = open my $sm, '|-'; + defined $pid or die $!; + if (!$pid) { + exec($smtp_server,'-i',@recipients) or die $!; + } + 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) { printf "Sent %s\n", $subject; } else { - print "OK. Log says: -Date: $date -Server: $smtp_server Port: 25 -From: $from -Subject: $subject -Cc: $cc -To: $to - -Result: ", $smtp->code, ' ', ($smtp->message =~ /\n([^\n]+\n)$/s), "\n"; + print "OK. Log says:\nDate: $date\n"; + if ($smtp) { + print "Server: $smtp_server\n"; + } else { + print "Sendmail: $smtp_server\n"; + } + print "From: $from\nSubject: $subject\nCc: $cc\nTo: $to\n\n"; + if ($smtp) { + print "Result: ", $smtp->code, ' ', + ($smtp->message =~ /\n([^\n]+\n)$/s), "\n"; + } else { + print "Result: OK\n"; + } } }
Signature · 1 line
1.3.2.g7d11
Quoted 3 lines · Eric Wong
I believe this is what Martin wanted. I think it's a good idea since sendmail binaries tend to be more flexible, but I'm ok with it either way.
I am not opposed to have an option to run a local submission agent binary (I said I like that if(){}else{} there, didn't I?). The ability to do so is a good thing. I am not however sure about changing the default when no option is specified on the command line.
Quoted 8 lines
Quoted 4 lines
git-send-email.perl X-Mailer: git-send-email $gitversion · line 368
Quoted 1 line
git-send-email.perl +1
-0+ if ($smtp_server =~ m#^/#) {I like this if(){}else{} here, but have a feeling that the logging part should be placed outside it to be shared.
Cleaned that up a bit, patch coming. Also removed the Port: printout completely, as it's rather redundant (see below).
Quoted 6 lines
Quoted 3 lines
While we are at it, we might want to enhance $smtp_server parsing to take host:port notation so that people can use message submission port 587/tcp (RFC 4409) instead.
This already works,
IO::Socket::INET(behindNet::SMTP) takes care of it :)
Thanks. Maybe the next option would be delivery to a file (or even SMTP batch)? ;-)
Quoted 11 lines · Junio C Hamano
Quoted 3 lines · Eric Wong
I believe this is what Martin wanted. I think it's a good idea since sendmail binaries tend to be more flexible, but I'm ok with it either way.
I am not opposed to have an option to run a local submission agent binary (I said I like that if(){}else{} there, didn't I?). The ability to do so is a good thing. I am not however sure about changing the default when no option is specified on the command line.
By "I believe this is what Martin wanted", I meant changing the default to
sendmail: <[email protected]>
> Oh, it should just work with sendmail if it's there and we don't > provide --smtp-server ;-)
Quoted 12 lines
Quoted 12 lines
Quoted 4 lines
git-send-email.perl X-Mailer: git-send-email $gitversion · line 368
Quoted 1 line
git-send-email.perl +1
-0+ if ($smtp_server =~ m#^/#) {I like this if(){}else{} here, but have a feeling that the logging part should be placed outside it to be shared.
Cleaned that up a bit, patch coming. Also removed the Port: printout completely, as it's rather redundant (see below).
Quoted 3 lines
While we are at it, we might want to enhance $smtp_server parsing to take host:port notation so that people can use message submission port 587/tcp (RFC 4409) instead.
Quoted 5 lines
Quoted 2 lines
This already works,
IO::Socket::INET(behindNet::SMTP) takes care of it :)Thanks. Maybe the next option would be delivery to a file (or even SMTP batch)? ;-)
Sure :) Authentication and SSL may be worth looking into, though. I'm pretty content these days with my autossh tunnel to my mail server, though.
Signature · 1 line
Eric Wong
Quoted 11 lines · Eric Wong
Quoted 5 lines · Junio C Hamano
I am not opposed to have an option to run a local submission agent binary (I said I like that if(){}else{} there, didn't I?). The ability to do so is a good thing. I am not however sure about changing the default when no option is specified on the command line.
By "I believe this is what Martin wanted", I meant changing the default to
sendmail: <[email protected]>> Oh, it should just work with sendmail if it's there and we don't
Thanks Eric! git-send-email used to default to using local binaries. It was only with the switch to Net::SMTP that the default changed to localhost:25.
IMHO the developer's machine is more likely to have a working /usr/sbin/sendmail than an SMTP server (specially looking at current linux desktop configurations).
OTOH, as long as I can override it to use sendmail, it's all good.
martin
Quoted 3 lines · Martin Langhoff
Thanks Eric! git-send-email used to default to using local binaries. It was only with the switch to
Net::SMTPthat the default changed to localhost:25.
You're welcome.
That's odd, though, looking at Mail::Sendmail, I don't think it actually uses the sendmail binary, either. The FAQ
<http://alma.ch/perl/Mail-Sendmail-FAQ.html> confirms this, too.
Of course documentation may be lying and the code I'm looking at may be *very* well obfuscated :D
Signature · 1 line
Eric Wong
Quoted 8 lines · Martin Langhoff
Quoted 7 lines · Eric Wong
Quoted 5 lines · Junio C Hamano
I am not opposed to have an option to run a local submission agent binary (I said I like that if(){}else{} there, didn't I?). The ability to do so is a good thing. I am not however sure about changing the default when no option is specified on the command line.
Quoted 8 lines
Quoted 4 lines
By "I believe this is what Martin wanted", I meant changing the default to
sendmail: <[email protected]>> Oh, it should just work with sendmail if it's there and we don't
Thanks Eric! git-send-email used to default to using local binaries. It was only with the switch to
Net::SMTPthat the default changed to localhost:25.
Thanks, and I think it makes sense.
Quoted 11 lines · Junio C Hamano
Quoted 3 lines · Eric Wong
I believe this is what Martin wanted. I think it's a good idea since sendmail binaries tend to be more flexible, but I'm ok with it either way.
I am not opposed to have an option to run a local submission agent binary (I said I like that if(){}else{} there, didn't I?). The ability to do so is a good thing. I am not however sure about changing the default when no option is specified on the command line.
I think, in practice, that /usr/lib/sendmail will exist anywhere you hve something running on port 25, at least on unixy machines. In my searches at an old job, that appeared to be the canonical place to call sendmail from, and every MTA appears to provide an appropriate binary there.
So, I'm not overly worried about it.
Quoted 7 lines · Ryan Anderson
I think, in practice, that
/usr/lib/sendmailwill exist anywhere you hve something running on port 25, at least on unixy machines. In my searches at an old job, that appeared to be the canonical place to call sendmail from, and every MTA appears to provide an appropriate binary there.So, I'm not overly worried about it.
exim, postfix and friends?
I used to know somebody who port-forwarded 25/tcp to central smtp server from smaller machines in her intranet installation, but I would say that is rare. I am not worried about it either; I just wanted to make sure _somebody_ thought the potential issues through and agreed with the change the patch makes.
Quoted 11 lines · Junio C Hamano
Quoted 7 lines · Ryan Anderson
I think, in practice, that
/usr/lib/sendmailwill exist anywhere you hve something running on port 25, at least on unixy machines. In my searches at an old job, that appeared to be the canonical place to call sendmail from, and every MTA appears to provide an appropriate binary there.So, I'm not overly worried about it.
exim, postfix and friends?
At least as packaged by Debian and Ubuntu, exim and postfix both fit that rule.
It's just that there is no other canonical name for the binary that provides the functionality, so everyone steals the name.
Quoted 5 lines
I used to know somebody who port-forwarded 25/tcp to central smtp server from smaller machines in her intranet installation, but I would say that is rare. I am not worried about it either; I just wanted to make sure _somebody_ thought the potential issues through and agreed with the change the patch makes.
Fair enough.
Quoted 4 lines · Junio C Hamano
Quoted 2 lines · Ryan Anderson
I think, in practice, that
/usr/lib/sendmailwill exist anywhere you hve something running on port 25, at least on unixy machines.
...
Quoted 1 line
exim, postfix and friends?
/usr/sbin/sendmail is there for any current MTA, so if qmail, exim, postfix, sendmail and all MTAs I can think of install a working sendmail binary.
Newish desktop-targetted distros (and MacOSX) are leaning towards not running an MTA on port 25 any more unless you ask them for it. They'll do crontab-driven queue flushes, so the messages you feed to /usr/sbin/sendmail will be sent a few minutes later.
That is why IMHO /usr/sbin/sendmail is a better default than localhost:25, but I can live with either ;-)
Quoted 2 lines
I used to know somebody who port-forwarded 25/tcp to central smtp server from smaller machines in her intranet installation,
I can understand that, but I think the changes in unixy distros mentioned above make it unnecessary today.
cheers,
martin