Skip to contentSkip to conversation

send-email: dependency removal, cleanup, + small feature

PATCH In review Eric Wong 5 participants 24 messages Jun 15, 2016

lore
Share & tools
lore

Eric Wong

author
#0 lore raw

1 - Change from Mail::Sendmail to Net::SMTP 2 - use built-in time() instead of /bin/date '+%s' 3 - lazy-load Email::Valid and make it optional 4 - add support for mutt aliases files

Patches 1 and 3 make git-send-email easily runnable with any reasonable Perl installation. 2 is just a good idea. 4 makes my life a lot easier.

Signature · 1 line
Eric Wong
author
#1 lore raw

PATCH 1/4 send-email: Change from Mail::Sendmail to Net::SMTP

Net::SMTP is in the base Perl distribution, so users are more likely to have it. Net::SMTP also allows reusing the SMTP connection, so sending multiple emails is faster.

---

 git-send-email.perl |   66 ++++++++++++++++++++++++++++++++-------------------
 1 files changed, 41 insertions(+), 25 deletions(-)

7155ae6e5f94a8fdf55f50029af27279dd36fd0a

git-send-email.perl +41 -25
index b220d11..efaf457 100755
@@ -19,11 +19,17 @@
 use strict;
 use warnings;
 use Term::ReadLine;
-use Mail::Sendmail qw(sendmail %mailcfg);
 use Getopt::Long;
 use Data::Dumper;
+use Net::SMTP;
 use Email::Valid;
 
+# most mail servers generate the Date: header, but not all...
+$ENV{LC_ALL} = 'C';
+use POSIX qw/strftime/;
+
+my $smtp;
+
 sub unique_email_list(@);
 sub cleanup_compose_files();
 
@@ -271,35 +277,45 @@ $cc = "";
 
 sub send_message
 {
-	my $to = join (", ", unique_email_list(@to));
-
-	%mail = (	To	=>	$to,
-			From	=>	$from,
-			CC	=>	$cc,
-			Subject	=>	$subject,
-			Message	=>	$message,
-			'Reply-to'	=>	$from,
-			'In-Reply-To'	=>	$reply_to,
-			'Message-ID'	=>	$message_id,
-			'X-Mailer'	=>	"git-send-email",
-		);
-
-	$mail{smtp} = $smtp_server;
-	$mailcfg{mime} = 0;
-
-	#print Data::Dumper->Dump([\%mail],[qw(*mail)]);
-
-	sendmail(%mail) or die $Mail::Sendmail::error;
+	my @recipients = unique_email_list(@to);
+	my $to = join (",\n\t", @recipients);
+	@recipients = unique_email_list(@recipients,@cc);
+	my $date = strftime('%a, %d %b %Y %H:%M:%S %z', localtime(time));
+
+	my $header = "From: $from
+To: $to
+Cc: $cc
+Subject: $subject
+Reply-To: $from
+Date: $date
+Message-Id: $message_id
+X-Mailer: git-send-email
+";
+	$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 ($quiet) {
-		printf "Sent %s\n", $subject;
+		print "Sent $subject\n";
 	} else {
-		print "OK. Log says:\n", $Mail::Sendmail::log;
-		print "\n\n"
+		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";
 	}
 }
 
-
 $reply_to = $initial_reply_to;
 make_message_id();
 $subject = $initial_subject;
@@ -390,7 +406,7 @@ sub cleanup_compose_files() {
 
 }
 
-
+$smtp->quit if $smtp;
 
 sub unique_email_list(@) {
 	my %seen;
Signature · 1 line
1.2.4.gb622a
#2 lore raw

Re: [PATCH 1/4] send-email: Change from Mail::Sendmail to Net::SMTP

Quoted 3 lines · Eric Wong

Net::SMTP is in the base Perl distribution, so users are more likely to have it. Net::SMTP also allows reusing the SMTP connection, so sending multiple emails is faster.

Overall, I like this set of cleanups, just one thing struck me as, "why?"

git-send-email.perl $cc = ""; · line 305

Quoted 3 lines
git-send-email.perl +1 -1
 	if ($quiet) {
-		printf "Sent %s\n", $subject;
+		print "Sent $subject\n";

This seems to be a pointless change, and actually, might be long-term counterproductive.

Assumption: Eventually, we're going to want to internationalize git.

If that is true, we'll eventually do something like this to lines like that:

	printf( gettext("Send %s\n"), $subject);

The alternative:

	print gettext("Send $subject\n");

does not work.

(The line that xgettext will see is 'Send $subject\n', but when the program actually runs, gettext will see the interpolated version, which fails.)

Internationalization may still be a ways off, but I think we're reaching the point where it might be something we care to think about.

Signature · 2 lines
Ryan Anderson
  sometimes Pug Majere
author
#3 lore raw

PATCH 1/1 send-email: Change from Mail::Sendmail to Net::SMTP

Net::SMTP is in the base Perl distribution, so users are more likely to have it. Net::SMTP also allows reusing the SMTP connection, so sending multiple emails is faster.

---

Notes: Reverted printf => print change from earlier.

 git-send-email.perl |   64 ++++++++++++++++++++++++++++++++-------------------
 1 files changed, 40 insertions(+), 24 deletions(-)

8d65a0a4121ade9f48f186d0dcf9f41adc62b22c

git-send-email.perl +40 -24
index b220d11..25daf16 100755
@@ -19,11 +19,17 @@
 use strict;
 use warnings;
 use Term::ReadLine;
-use Mail::Sendmail qw(sendmail %mailcfg);
 use Getopt::Long;
 use Data::Dumper;
+use Net::SMTP;
 use Email::Valid;
 
+# most mail servers generate the Date: header, but not all...
+$ENV{LC_ALL} = 'C';
+use POSIX qw/strftime/;
+
+my $smtp;
+
 sub unique_email_list(@);
 sub cleanup_compose_files();
 
@@ -271,35 +277,45 @@ $cc = "";
 
 sub send_message
 {
-	my $to = join (", ", unique_email_list(@to));
-
-	%mail = (	To	=>	$to,
-			From	=>	$from,
-			CC	=>	$cc,
-			Subject	=>	$subject,
-			Message	=>	$message,
-			'Reply-to'	=>	$from,
-			'In-Reply-To'	=>	$reply_to,
-			'Message-ID'	=>	$message_id,
-			'X-Mailer'	=>	"git-send-email",
-		);
-
-	$mail{smtp} = $smtp_server;
-	$mailcfg{mime} = 0;
-
-	#print Data::Dumper->Dump([\%mail],[qw(*mail)]);
-
-	sendmail(%mail) or die $Mail::Sendmail::error;
+	my @recipients = unique_email_list(@to);
+	my $to = join (",\n\t", @recipients);
+	@recipients = unique_email_list(@recipients,@cc);
+	my $date = strftime('%a, %d %b %Y %H:%M:%S %z', localtime(time));
+
+	my $header = "From: $from
+To: $to
+Cc: $cc
+Subject: $subject
+Reply-To: $from
+Date: $date
+Message-Id: $message_id
+X-Mailer: git-send-email
+";
+	$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 ($quiet) {
 		printf "Sent %s\n", $subject;
 	} else {
-		print "OK. Log says:\n", $Mail::Sendmail::log;
-		print "\n\n"
+		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";
 	}
 }
 
-
 $reply_to = $initial_reply_to;
 make_message_id();
 $subject = $initial_subject;
@@ -390,7 +406,7 @@ sub cleanup_compose_files() {
 
 }
 
-
+$smtp->quit if $smtp;
 
 sub unique_email_list(@) {
 	my %seen;
Signature · 1 line
1.2.4.gb622a
author
#9 lore raw

Re: [PATCH 1/4] send-email: Change from Mail::Sendmail to Net::SMTP

In reply to Ryan Anderson
Quoted 8 lines · Ryan Anderson
Quoted 3 lines · Eric Wong

Net::SMTP is in the base Perl distribution, so users are more likely to have it. Net::SMTP also allows reusing the SMTP connection, so sending multiple emails is faster.

Overall, I like this set of cleanups, just one thing struck me as, "why?"

Quoted 6 lines

git-send-email.perl $cc = ""; · line 305

Quoted 3 lines
git-send-email.perl +1 -1
 	if ($quiet) {
-		printf "Sent %s\n", $subject;
+		print "Sent $subject\n";

This seems to be a pointless change, and actually, might be long-term counterproductive.

Force of habit, I think. I originally rewrote that portion but thought I reverted it back to the way it was. Besides, it's even slightly faster this way :) It could still be faster(!) if I just printed a list (like below).

Quoted 9 lines

Assumption: Eventually, we're going to want to internationalize git.

If that is true, we'll eventually do something like this to lines like that:

	printf( gettext("Send %s\n"), $subject);

The alternative:

	print gettext("Send $subject\n");

does not work.

print gettext('Send '),$subject,"\n";

Quoted 6 lines

(The line that xgettext will see is 'Send $subject\n', but when the program actually runs, gettext will see the interpolated version, which fails.)

Internationalization may still be a ways off, but I think we're reaching the point where it might be something we care to think about.

Signature · 1 line
Eric Wong
#10 lore raw

Re: [PATCH 1/4] send-email: Change from Mail::Sendmail to Net::SMTP

Quoted 1 line · Eric Wong

print gettext('Send '),$subject,"\n";

Nak; who said all languages have verb before what the verb acts upon?

author
#11 lore raw

PATCH 2/4 send-email: use built-in time() instead of /bin/date '+%s'

In reply to Eric Wong

---

 git-send-email.perl |    3 +--
 1 files changed, 1 insertions(+), 2 deletions(-)

079ce058710240643369589448660620cd925f5c

git-send-email.perl +1 -2
index efaf457..5e08817 100755
@@ -264,8 +264,7 @@ my $message_id_template = "<%s-git-send-
 
 sub make_message_id
 {
-	my $date = `date "+\%s"`;
-	chomp($date);
+	my $date = time;
 	my $pseudo_rand = int (rand(4200));
 	$message_id = sprintf $message_id_template, "$date$pseudo_rand";
 	#print "new message id = $message_id\n"; # Was useful for debugging
Signature · 1 line
1.2.4.gb622a
author
#12 lore raw

PATCH 3/4 send-email: lazy-load Email::Valid and make it optional

In reply to Eric Wong

It's not installed on enough machines, and is overkill most of the time. We'll fallback to a very basic regexp (that is a looser variant of what Email::Valid allows) just in case, but nothing like the monster regexp Email::Valid has to offer :)

---

 git-send-email.perl |   16 +++++++++++++---
 1 files changed, 13 insertions(+), 3 deletions(-)

140eaf9b9d438ea489e6c72e2148feb3e355aea8

git-send-email.perl +13 -3
index 5e08817..73bba19 100755
@@ -22,12 +22,12 @@ use Term::ReadLine;
 use Getopt::Long;
 use Data::Dumper;
 use Net::SMTP;
-use Email::Valid;
 
 # most mail servers generate the Date: header, but not all...
 $ENV{LC_ALL} = 'C';
 use POSIX qw/strftime/;
 
+my $have_email_valid = eval { require Email::Valid or undef };
 my $smtp;
 
 sub unique_email_list(@);
@@ -250,6 +250,16 @@ EOT
 # Variables we set as part of the loop over files
 our ($message_id, $cc, %mail, $subject, $reply_to, $message);
 
+sub extract_valid_address {
+	my $address = shift;
+	if ($have_email_valid) {
+		return Email::Valid->address($address);
+	} else {
+		# less robust/correct than the monster regexp in Email::Valid,
+		# but still does a 99% job, and one less dependency
+		return ($address =~ /([^\"<>\s]+@[^<>\s]+)/);
+	}
+}
 
 # Usually don't need to change anything below here.
 
@@ -259,7 +269,7 @@ our ($message_id, $cc, %mail, $subject, 
 # 1 second since the last time we were called.
 
 # We'll setup a template for the message id, using the "from" address:
-my $message_id_from = Email::Valid->address($from);
+my $message_id_from = extract_valid_address($from);
 my $message_id_template = "<%s-git-send-email-$message_id_from>";
 
 sub make_message_id
@@ -412,7 +422,7 @@ sub unique_email_list(@) {
 	my @emails;
 
 	foreach my $entry (@_) {
-		my $clean = Email::Valid->address($entry);
+		my $clean = extract_valid_address($entry);
 		next if $seen{$clean}++;
 		push @emails, $entry;
 	}
Signature · 1 line
1.2.4.gb622a
#13 lore raw

Re: [PATCH 3/4] send-email: lazy-load Email::Valid and make it optional

Quoted 1 line · Eric Wong

+my $have_email_valid = eval { require Email::Valid or undef };

Merlyn already commented on this and I'd appreciate this part to be redone.

author
#14 lore raw

PATCH 1/1 send-email: lazy-load Email::Valid and make it optional

It's not installed on enough machines, and is overkill most of the time. We'll fallback to a very basic regexp just in case, but nothing like the monster regexp Email::Valid has to offer :)

Small cleanup from Merlyn.

---

 git-send-email.perl |   16 +++++++++++++---
 1 files changed, 13 insertions(+), 3 deletions(-)

3f09a822e3871eeae521da80c748602862fc52ce

git-send-email.perl +13 -3
index 5e08817..7cbf11d 100755
@@ -22,12 +22,12 @@ use Term::ReadLine;
 use Getopt::Long;
 use Data::Dumper;
 use Net::SMTP;
-use Email::Valid;
 
 # most mail servers generate the Date: header, but not all...
 $ENV{LC_ALL} = 'C';
 use POSIX qw/strftime/;
 
+my $have_email_valid = eval { require Email::Valid; 1 };
 my $smtp;
 
 sub unique_email_list(@);
@@ -250,6 +250,16 @@ EOT
 # Variables we set as part of the loop over files
 our ($message_id, $cc, %mail, $subject, $reply_to, $message);
 
+sub extract_valid_address {
+	my $address = shift;
+	if ($have_email_valid) {
+		return Email::Valid->address($address);
+	} else {
+		# less robust/correct than the monster regexp in Email::Valid,
+		# but still does a 99% job, and one less dependency
+		return ($address =~ /([^\"<>\s]+@[^<>\s]+)/);
+	}
+}
 
 # Usually don't need to change anything below here.
 
@@ -259,7 +269,7 @@ our ($message_id, $cc, %mail, $subject, 
 # 1 second since the last time we were called.
 
 # We'll setup a template for the message id, using the "from" address:
-my $message_id_from = Email::Valid->address($from);
+my $message_id_from = extract_valid_address($from);
 my $message_id_template = "<%s-git-send-email-$message_id_from>";
 
 sub make_message_id
@@ -412,7 +422,7 @@ sub unique_email_list(@) {
 	my @emails;
 
 	foreach my $entry (@_) {
-		my $clean = Email::Valid->address($entry);
+		my $clean = extract_valid_address($entry);
 		next if $seen{$clean}++;
 		push @emails, $entry;
 	}
Signature · 1 line
1.2.4.gb622a
#15 lore raw

Re: [PATCH 3/4] send-email: lazy-load Email::Valid and make it optional

In reply to Eric Wong
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

"Eric" == Eric Wong <[email protected]> writes:

Eric> +my $have_email_valid = eval { require Email::Valid or undef };

This is not necessary... if eval fails, it returns undef by definition. Your code presumes that the non-zero last-expression-evaluated of a require is also returned from the require, which I believe is only accidentally true, although the behavior may be recently documented and therefore promised. (On perl 5.8, it looks a bit fishy to me at a quick glance.)

My favorite idiom for a possibly failing eval-step is:

        my $can_I_do_this = eval { riskything; 1 };

If riskything fails, eval returns undef. If it succeeds, it evaluates the 1, and returns that.

Signature · 4 lines
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<[email protected]> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
author
#16 lore raw

PATCH 4/4 send-email: add support for mutt aliases files

In reply to Eric Wong

I got rid of the Email::Valid dependency since writing RFC-correct email addresses is probably not a problem for most users.

In my experience, misspelled usernames are a much bigger problem. I am prone to doing things like leaving out the 'k' in Junio's email address and other things that Email::Valid can't catch.

Since I use mutt and the aliases file is pretty simple, I've added basic support for mutt alias files.

To setup git-send-email to use a mutt aliases file for a repo, do this:

git-repo-config sendemail.muttaliases <mutt_alias_file_path>

More email clients/address book formats can easily be supported in the future.

---

 git-send-email.perl |   12 ++++++++++++
 1 files changed, 12 insertions(+), 0 deletions(-)

8bc65d178dd755ecd1f3c038b975b9bbe58c1015

git-send-email.perl +12 -0
index 73bba19..207b1fb 100755
@@ -89,6 +89,15 @@ sub gitvar_ident {
 my ($author) = gitvar_ident('GIT_AUTHOR_IDENT');
 my ($committer) = gitvar_ident('GIT_COMMITTER_IDENT');
 
+my %aliases;
+if (my $mutt_aliases = `git-repo-config sendemail.muttaliases`) {
+    chomp $mutt_aliases;
+    open my $ma, '<', $mutt_aliases or die "opening $mutt_aliases: $!\n";
+    while (<$ma>) { if (/^alias\s+(\S+)\s+(.*)/) { $aliases{$1} = $2 } }
+    close $ma;
+}
+# aliases for more mail clients can be supported here:
+
 my $prompting = 0;
 if (!defined $from) {
 	$from = $author || $committer;
@@ -112,6 +121,9 @@ if (!@to) {
 	$prompting++;
 }
 
+@to = map { $aliases{$_} || $_ } @to;
+@initial_cc = map { $aliases{$_} || $_ } @initial_cc;
+
 if (!defined $initial_subject && $compose) {
 	do {
 		$_ = $term->readline("What subject should the emails start with? ",
Signature · 1 line
1.2.4.gb622a
#17 lore raw

Re: [PATCH 4/4] send-email: add support for mutt aliases files

git-send-email.perl sub gitvar_ident { · line 100

Quoted 11 lines · Eric Wong

More email clients/address book formats can easily be supported in the future.

git-send-email.perl +8 -0
+if (my $mutt_aliases = `git-repo-config sendemail.muttaliases`) {
+    chomp $mutt_aliases;
+    open my $ma, '<', $mutt_aliases or die "opening $mutt_aliases: $!\n";
+    while (<$ma>) { if (/^alias\s+(\S+)\s+(.*)/) { $aliases{$1} = $2 } }
+    close $ma;
+}
+# aliases for more mail clients can be supported here:
+

I'd rather avoid proliferation of sendemail.{foo,bar,mutt,pine,...}aliases variables. Can we autodetect the alias file format and parse the given file accordingly?

#18 lore raw

Re: [PATCH 4/4] send-email: add support for mutt aliases files

Quoted 5 lines · Junio C Hamano
Quoted 2 lines · Eric Wong

More email clients/address book formats can easily be supported in the future.

Quoted 12 lines

git-send-email.perl sub gitvar_ident { · line 100

Quoted 8 lines
git-send-email.perl +8 -0
+if (my $mutt_aliases = `git-repo-config sendemail.muttaliases`) {
+    chomp $mutt_aliases;
+    open my $ma, '<', $mutt_aliases or die "opening $mutt_aliases: $!\n";
+    while (<$ma>) { if (/^alias\s+(\S+)\s+(.*)/) { $aliases{$1} = $2 } }
+    close $ma;
+}
+# aliases for more mail clients can be supported here:
+

I'd rather avoid proliferation of sendemail.{foo,bar,mutt,pine,...}aliases variables. Can we autodetect the alias file format and parse the given file accordingly?

Don't bother - instead of lots of variables, just have two:
sendemail.aliasesfile sendemail.aliasfiletype

Signature · 2 lines
Ryan Anderson
  sometimes Pug Majere
author
#19 lore raw

PATCH 1/1 send-email: address expansion for common mailers

mutt, gnus, pine, mailrc formats should be supported.

Testing and feedback for correctness and completeness of all formats and support for additional formats would be good.

Nested expansions are also supported.

Two git repo-config keys are required for this
(as suggested by Ryan Anderson):

sendemail.aliasesfile = <filename of aliases file> sendemail.aliasfiletype = (mutt|gnus|pine|mailrc)

I was initially working on auto-detection, but mailrc and mutt formats tend to throw each other off (they're alike, but handle multiple addresses per-alias differently).

---

 git-send-email.perl |   46 ++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 46 insertions(+), 0 deletions(-)

aea3aaf9571ab639c67608f62771e73104842294

git-send-email.perl +46 -0
index 7cbf11d..208c119 100755
@@ -89,6 +89,39 @@ sub gitvar_ident {
 my ($author) = gitvar_ident('GIT_AUTHOR_IDENT');
 my ($committer) = gitvar_ident('GIT_COMMITTER_IDENT');
 
+my %aliases;
+chomp(my $aliases_file = `git-repo-config sendemail.aliasesfile`);
+chomp(my $aliasfiletype = `git-repo-config sendemail.aliasfiletype`);
+my %parse_alias = (
+	# multiline formats can be supported in the future
+	mutt => sub { my $fh = shift; while (<$fh>) {
+		if (/^alias\s+(\S+)\s+(.*)$/) {
+			my ($alias, $addr) = ($1, $2);
+			$addr =~ s/#.*$//; # mutt allows # comments
+			 # commas delimit multiple addresses
+			$aliases{$alias} = [ split(/\s*,\s*/, $addr) ];
+		}}},
+	mailrc => sub { my $fh = shift; while (<$fh>) {
+		if (/^alias\s+(\S+)\s+(.*)$/) {
+			# spaces delimit multiple addresses
+			$aliases{$1} = [ split(/\s+/, $2) ];
+		}}},
+	pine => sub { my $fh = shift; while (<$fh>) {
+		if (/^(\S+)\s+(.*)$/) {
+			$aliases{$1} = [ split(/\s*,\s*/, $2) ];
+		}}},
+	gnus => sub { my $fh = shift; while (<$fh>) {
+		if (/\(define-mail-alias\s+"(\S+?)"\s+"(\S+?)"\)/) {
+			$aliases{$1} = [ $2 ];
+		}}}
+);
+
+if ($aliases_file && defined $parse_alias{$aliasfiletype}) {
+	open my $fh, '<', $aliases_file or die "opening $aliases_file: $!\n";
+	$parse_alias{$aliasfiletype}->($fh);
+	close $fh;
+}
+
 my $prompting = 0;
 if (!defined $from) {
 	$from = $author || $committer;
@@ -112,6 +145,19 @@ if (!@to) {
 	$prompting++;
 }
 
+sub expand_aliases {
+	my @cur = @_;
+	my @last;
+	do {
+		@last = @cur;
+		@cur = map { $aliases{$_} ? @{$aliases{$_}} : $_ } @last;
+	} while (join(',',@cur) ne join(',',@last));
+	return @cur;
+}
+
+@to = expand_aliases(@to);
+@initial_cc = expand_aliases(@initial_cc);
+
 if (!defined $initial_subject && $compose) {
 	do {
 		$_ = $term->readline("What subject should the emails start with? ",
Signature · 1 line
1.2.4.gb622a
#20 lore raw

Re: [PATCH] send-email: address expansion for common mailers

Quoted 9 lines · Eric Wong

Two git repo-config keys are required for this
(as suggested by Ryan Anderson):

sendemail.aliasesfile = <filename of aliases file> sendemail.aliasfiletype = (mutt|gnus|pine|mailrc)

I was initially working on auto-detection, but mailrc and mutt formats tend to throw each other off (they're alike, but handle multiple addresses per-alias differently).

I think specifying the type explicitly is probably not too much hassle for the end user, so that is fine. Now, do we want to support more than one aliases file?

author
#21 lore raw

Re: [PATCH] send-email: address expansion for common mailers

Quoted 8 lines · Junio C Hamano
Quoted 6 lines · Eric Wong

Two git repo-config keys are required for this
(as suggested by Ryan Anderson):

sendemail.aliasesfile = <filename of aliases file> sendemail.aliasfiletype = (mutt|gnus|pine|mailrc)

Quoted 7 lines
Quoted 3 lines

I was initially working on auto-detection, but mailrc and mutt formats tend to throw each other off (they're alike, but handle multiple addresses per-alias differently).

I think specifying the type explicitly is probably not too much hassle for the end user, so that is fine. Now, do we want to support more than one aliases file?

If they're different types, probably not. But if it's the same type, it's pretty easy and I don't see why not. This patch applies on top of the previous one.

Subject: [PATCH] send-email: allow more than one alias file to be used

The aliasfiletype must be the same for all aliasesfiles, though.

---

 git-send-email.perl |   12 +++++++-----
 1 files changed, 7 insertions(+), 5 deletions(-)

16306f761b34505672a04bff333d6342724756c8

git-send-email.perl +7 -5
index d3e1768..5d1e95c 100755
@@ -90,7 +90,7 @@ my ($author) = gitvar_ident('GIT_AUTHOR_
 my ($committer) = gitvar_ident('GIT_COMMITTER_IDENT');
 
 my %aliases;
-chomp(my $aliases_file = `git-repo-config sendemail.aliasesfile`);
+chomp(my @alias_files = `git-repo-config --get-all sendemail.aliasesfile`);
 chomp(my $aliasfiletype = `git-repo-config sendemail.aliasfiletype`);
 my %parse_alias = (
 	# multiline formats can be supported in the future
@@ -116,10 +116,12 @@ my %parse_alias = (
 		}}}
 );
 
-if ($aliases_file && defined $parse_alias{$aliasfiletype}) {
-	open my $fh, '<', $aliases_file or die "opening $aliases_file: $!\n";
-	$parse_alias{$aliasfiletype}->($fh);
-	close $fh;
+if (@alias_files && defined $parse_alias{$aliasfiletype}) {
+	foreach my $file (@alias_files) {
+		open my $fh, '<', $file or die "opening $file: $!\n";
+		$parse_alias{$aliasfiletype}->($fh);
+		close $fh;
+	}
 }
 
 my $prompting = 0;
Signature · 1 line
1.2.4.gb622a
#4 lore raw

Re: [PATCH] send-email: Change from Mail::Sendmail to Net::SMTP

In reply to Eric Wong
Quoted 3 lines · Eric Wong

Net::SMTP is in the base Perl distribution, so users are more likely to have it. Net::SMTP also allows reusing the SMTP connection, so sending multiple emails is faster.

This is causing problems for me on my Debian sarge dev box.

* If I have to believe strace(), Net::SMTP is trying to look up
"localhost" via DNS. Sketchy workaround: use 127.0.0.1.

* This box has nothing listening on port 25. It doesn't get email
from the net, being a LAN machine, so I've told the debian config system that we don't need an smtp daemon. Net::SMTP doesn't know how to use /usr/bin/sendmail

* That nasty @@VERSION@@ thing isn't valid perl, so working on this
code is a pain. Something like this (warning! broken diff ahead!) fixes it for me.

@@ -292,6 +292,11 @@ sub send_message

        @recipients = unique_email_list(@recipients,@cc);
        my $date = strftime('%a, %d %b %Y %H:%M:%S %z', localtime($time++));

+ my $gitversion = '@@GIT_VERSION@@';
+ if ($gitversion eq '@@'.'GIT_VERSION@@') {
+ $gitversion = git --version;
+ }
+
my $header = "From: $from
To: $to
Cc: $cc
@@ -299,11 +304,11 @@ Subject: $subject
Reply-To: $from
Date: $date
Message-Id: $message_id
-X-Mailer: git-send-email @@GIT_VERSION@@
+X-Mailer: git-send-email $gitversion

 ";
        $header .= "In-Reply-To: $reply_to\n" if $reply_to;

cheers,

martin

#5 lore raw

Re: [PATCH] send-email: Change from Mail::Sendmail to Net::SMTP

Quoted 4 lines · Martin Langhoff

* This box has nothing listening on port 25. It doesn't get email
from the net, being a LAN machine, so I've told the debian config system that we don't need an smtp daemon. Net::SMTP doesn't know how to use /usr/bin/sendmail

Wouldn't --smtp-server=that.smtp.server work for you? Ah, that would not work if your use is to send a local mail. Hmph...

#6 lore raw

Re: [PATCH] send-email: Change from Mail::Sendmail to Net::SMTP

Quoted 6 lines · Junio C Hamano
Quoted 2 lines

system that we don't need an smtp daemon. Net::SMTP doesn't know how to use /usr/bin/sendmail

Wouldn't --smtp-server=that.smtp.server work for you? Ah, that would not work if your use is to send a local mail. Hmph...

Well, the machine knows that the smtp server is (I mean, files in /etc have the right values in them), but I don't think often about it. Only when I am installing OSs or MTAs...

I know... I'm a whiner! ;-) I'll probably do something that does an eval and tries Mail::Sendmail and post it.

martin

author
#7 lore raw

Re: [PATCH] send-email: Change from Mail::Sendmail to Net::SMTP

Quoted 8 lines · Martin Langhoff
Quoted 6 lines · Junio C Hamano
Quoted 2 lines

system that we don't need an smtp daemon. Net::SMTP doesn't know how to use /usr/bin/sendmail

Wouldn't --smtp-server=that.smtp.server work for you? Ah, that would not work if your use is to send a local mail. Hmph...

Quoted 6 lines

Well, the machine knows that the smtp server is (I mean, files in /etc have the right values in them), but I don't think often about it. Only when I am installing OSs or MTAs...

I know... I'm a whiner! ;-) I'll probably do something that does an eval and tries Mail::Sendmail and post it.

You should be able to just open a pipe to:
/usr/sbin/sendmail @recipients
and just write headers\nbody to that pipe.

Perhaps allow and detect --smtp-server=/path/to/sendmail ?

Signature · 1 line
Eric Wong
#8 lore raw

Re: [PATCH] send-email: Change from Mail::Sendmail to Net::SMTP

Quoted 3 lines · Eric Wong

You should be able to just open a pipe to:
/usr/sbin/sendmail @recipients
and just write headers\nbody to that pipe.

Sounds reasonable. I just looked at what Mail::Sendmail does and it isn't specially interesting. (There used to be a different Perl module that did smart things, depending on what MTA it found, but I can't find it now).

Quoted 1 line

Perhaps allow and detect --smtp-server=/path/to/sendmail ?

Oh, it should just work with sendmail if it's there and we don't provide --smtp-server ;-)

m

author
#22 lore raw

PATCH 1/1 send-email: address expansion for common mailers

In reply to Eric Wong

mutt, gnus, pine, mailrc formats should be supported.

Testing and feedback for correctness and completeness of all formats and support for additional formats would be good.

Nested expansions are also supported.

More than one alias file to be used.

All alias file formats must still of be the same type, though.

Two git repo-config keys are required for this
(as suggested by Ryan Anderson):

sendemail.aliasesfile = <filename of aliases file> sendemail.aliasfiletype = (mutt|gnus|pine|mailrc)

---

Looks like this patch got forgotten a while ago, and I never noticed because I forgot to set WITH_SEND_EMAIL when doing make install. Of course, WITH_SEND_EMAIL should no longer be needed...

 git-send-email.perl |   48 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 48 insertions(+), 0 deletions(-)

ff6593287dc500853c1cf05bdb0f32f970f10c9d

git-send-email.perl +48 -0
index 703dd1f..d8c4b1f 100755
@@ -89,6 +89,41 @@ sub gitvar_ident {
 my ($author) = gitvar_ident('GIT_AUTHOR_IDENT');
 my ($committer) = gitvar_ident('GIT_COMMITTER_IDENT');
 
+my %aliases;
+chomp(my @alias_files = `git-repo-config --get-all sendemail.aliasesfile`);
+chomp(my $aliasfiletype = `git-repo-config sendemail.aliasfiletype`);
+my %parse_alias = (
+	# multiline formats can be supported in the future
+	mutt => sub { my $fh = shift; while (<$fh>) {
+		if (/^alias\s+(\S+)\s+(.*)$/) {
+			my ($alias, $addr) = ($1, $2);
+			$addr =~ s/#.*$//; # mutt allows # comments
+			 # commas delimit multiple addresses
+			$aliases{$alias} = [ split(/\s*,\s*/, $addr) ];
+		}}},
+	mailrc => sub { my $fh = shift; while (<$fh>) {
+		if (/^alias\s+(\S+)\s+(.*)$/) {
+			# spaces delimit multiple addresses
+			$aliases{$1} = [ split(/\s+/, $2) ];
+		}}},
+	pine => sub { my $fh = shift; while (<$fh>) {
+		if (/^(\S+)\s+(.*)$/) {
+			$aliases{$1} = [ split(/\s*,\s*/, $2) ];
+		}}},
+	gnus => sub { my $fh = shift; while (<$fh>) {
+		if (/\(define-mail-alias\s+"(\S+?)"\s+"(\S+?)"\)/) {
+			$aliases{$1} = [ $2 ];
+		}}}
+);
+
+if (@alias_files && defined $parse_alias{$aliasfiletype}) {
+	foreach my $file (@alias_files) {
+		open my $fh, '<', $file or die "opening $file: $!\n";
+		$parse_alias{$aliasfiletype}->($fh);
+		close $fh;
+	}
+}
+
 my $prompting = 0;
 if (!defined $from) {
 	$from = $author || $committer;
@@ -112,6 +147,19 @@ if (!@to) {
 	$prompting++;
 }
 
+sub expand_aliases {
+	my @cur = @_;
+	my @last;
+	do {
+		@last = @cur;
+		@cur = map { $aliases{$_} ? @{$aliases{$_}} : $_ } @last;
+	} while (join(',',@cur) ne join(',',@last));
+	return @cur;
+}
+
+@to = expand_aliases(@to);
+@initial_cc = expand_aliases(@initial_cc);
+
 if (!defined $initial_subject && $compose) {
 	do {
 		$_ = $term->readline("What subject should the emails start with? ",
Signature · 1 line
1.3.2.g1c9b
Acked-by on v1 1/1
#23 lore raw

Re: [PATCH (resend)] send-email: address expansion for common mailers

Quoted 11 lines · Eric Wong

mutt, gnus, pine, mailrc formats should be supported.

Testing and feedback for correctness and completeness of all formats and support for additional formats would be good.

Nested expansions are also supported.

More than one alias file to be used.

All alias file formats must still of be the same type, though.

Quoted 7 lines

Two git repo-config keys are required for this
(as suggested by Ryan Anderson):

sendemail.aliasesfile = <filename of aliases file> sendemail.aliasfiletype = (mutt|gnus|pine|mailrc)

Quoted 80 lines

---

Looks like this patch got forgotten a while ago, and I never noticed because I forgot to set WITH_SEND_EMAIL when doing make install. Of course, WITH_SEND_EMAIL should no longer be needed...

 git-send-email.perl |   48 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 48 insertions(+), 0 deletions(-)

ff6593287dc500853c1cf05bdb0f32f970f10c9d

git-send-email.perl +48 -0
index 703dd1f..d8c4b1f 100755
@@ -89,6 +89,41 @@ sub gitvar_ident {
 my ($author) = gitvar_ident('GIT_AUTHOR_IDENT');
 my ($committer) = gitvar_ident('GIT_COMMITTER_IDENT');
 
+my %aliases;
+chomp(my @alias_files = `git-repo-config --get-all sendemail.aliasesfile`);
+chomp(my $aliasfiletype = `git-repo-config sendemail.aliasfiletype`);
+my %parse_alias = (
+	# multiline formats can be supported in the future
+	mutt => sub { my $fh = shift; while (<$fh>) {
+		if (/^alias\s+(\S+)\s+(.*)$/) {
+			my ($alias, $addr) = ($1, $2);
+			$addr =~ s/#.*$//; # mutt allows # comments
+			 # commas delimit multiple addresses
+			$aliases{$alias} = [ split(/\s*,\s*/, $addr) ];
+		}}},
+	mailrc => sub { my $fh = shift; while (<$fh>) {
+		if (/^alias\s+(\S+)\s+(.*)$/) {
+			# spaces delimit multiple addresses
+			$aliases{$1} = [ split(/\s+/, $2) ];
+		}}},
+	pine => sub { my $fh = shift; while (<$fh>) {
+		if (/^(\S+)\s+(.*)$/) {
+			$aliases{$1} = [ split(/\s*,\s*/, $2) ];
+		}}},
+	gnus => sub { my $fh = shift; while (<$fh>) {
+		if (/\(define-mail-alias\s+"(\S+?)"\s+"(\S+?)"\)/) {
+			$aliases{$1} = [ $2 ];
+		}}}
+);
+
+if (@alias_files && defined $parse_alias{$aliasfiletype}) {
+	foreach my $file (@alias_files) {
+		open my $fh, '<', $file or die "opening $file: $!\n";
+		$parse_alias{$aliasfiletype}->($fh);
+		close $fh;
+	}
+}
+
 my $prompting = 0;
 if (!defined $from) {
 	$from = $author || $committer;
@@ -112,6 +147,19 @@ if (!@to) {
 	$prompting++;
 }
 
+sub expand_aliases {
+	my @cur = @_;
+	my @last;
+	do {
+		@last = @cur;
+		@cur = map { $aliases{$_} ? @{$aliases{$_}} : $_ } @last;
+	} while (join(',',@cur) ne join(',',@last));
+	return @cur;
+}
+
+@to = expand_aliases(@to);
+@initial_cc = expand_aliases(@initial_cc);
+
 if (!defined $initial_subject && $compose) {
 	do {
 		$_ = $term->readline("What subject should the emails start with? ",
Signature · 1 line
1.3.2.g1c9b