# [PATCH] git-send-email.perl: Fold long header lines to 78 chars

7 messages from 2009-10-05 to 2009-10-10. Participants: Joe Perches, Junio C Hamano.
Thread: https://gitlist.dev/t/21135

## Joe Perches, 2009-10-05 16:24

Subject: [PATCH] git-send-email.perl: Fold long header lines to 78 chars
Message-ID: <1254759898.1799.449.camel@Joe-Laptop.home>
URL: https://gitlist.dev/e/1254759898.1799.449.camel%40Joe-Laptop.home

```
Some MTAs reject or filter long header lines which can
be generated if the cc list is only a few entries.

Fold long header lines to 78 chars to be more rfc compliant.

Signed-off-by: Joe Perches <joe@perches.com>

diff --git a/git-send-email.perl b/git-send-email.perl
index dd821f7..cb8b48b 100755
--- a/git-send-email.perl
+++ b/git-send-email.perl
@@ -814,6 +814,41 @@ sub sanitize_address
 
 }
 
+# Fold header lines to 78 chars if possible for better RFC 2822 compliance
+# Does not terminate last line with newline
+sub fold_header
+{
+    my ($folded_line, $separator, @entries) = @_;
+    my $folded_header = "";
+    my $count = 0;
+    my $trim_sep = $separator;
+
+    $trim_sep =~ s/\s+$//;
+
+    foreach my $entry (@entries) {
+	if ($count == 0) {
+	    $folded_line = "$folded_line$entry";
+	} elsif ((length($folded_line) + length($entry)) > 78) {
+	    if ($folded_header ne "") {
+		$folded_header = "$folded_header$trim_sep\n";
+	    }
+	    $folded_header = "$folded_header$folded_line";
+	    $folded_line = " $entry";
+	} else {
+	    $folded_line = "$folded_line$separator$entry";
+	}
+	$count++;
+    }
+
+    if ($count == 0) {
+	$folded_header = "$folded_line";
+    } else {
+	$folded_header = "$folded_header$trim_sep\n$folded_line";
+    }
+
+    return "$folded_header";
+}
+
 # Returns 1 if the message was sent, and 0 otherwise.
 # In actuality, the whole program dies when there
 # is an error sending a message.
@@ -835,10 +870,10 @@ sub send_message
 	    $gitversion = Git::version();
 	}
 
-	my $cc = join(", ", unique_email_list(@cc));
+	@cc = unique_email_list(@cc);
 	my $ccline = "";
-	if ($cc ne '') {
-		$ccline = "\nCc: $cc";
+	if (@cc gt 0) {
+		$ccline = fold_header("\nCc: ", ", ", @cc);
 	}
 	my $sanitized_sender = sanitize_address($sender);
 	make_message_id() unless defined($message_id);
@@ -976,7 +1011,7 @@ X-Mailer: git-send-email $gitversion
 		if ($smtp_server !~ m#^/#) {
 			print "Server: $smtp_server\n";
 			print "MAIL FROM:<$raw_from>\n";
-			print "RCPT TO:".join(',',(map { "<$_>" } @recipients))."\n";
+			print fold_header("RCPT TO:", ",", map { "<$_>" } @recipients)."\n";
 		} else {
 			print "Sendmail: $smtp_server ".join(' ',@sendmail_parameters)."\n";
 		}

```

## Junio C Hamano, 2009-10-08 05:02

Subject: Re: [PATCH] git-send-email.perl: Fold long header lines to 78 chars
Message-ID: <7vd44yo4uz.fsf@alter.siamese.dyndns.org>
URL: https://gitlist.dev/e/7vd44yo4uz.fsf%40alter.siamese.dyndns.org
In-Reply-To: <1254759898.1799.449.camel@Joe-Laptop.home>

```
Joe Perches <joe@perches.com> writes:

> Some MTAs reject or filter long header lines which can
> be generated if the cc list is only a few entries.
>
> Fold long header lines to 78 chars to be more rfc compliant.
>
> Signed-off-by: Joe Perches <joe@perches.com>
>
> diff --git a/git-send-email.perl b/git-send-email.perl
> index dd821f7..cb8b48b 100755
> --- a/git-send-email.perl
> +++ b/git-send-email.perl
> @@ -835,10 +870,10 @@ sub send_message
>  	    $gitversion = Git::version();
>  	}
>  
> -	my $cc = join(", ", unique_email_list(@cc));
> +	@cc = unique_email_list(@cc);
>  	my $ccline = "";
> -	if ($cc ne '') {
> -		$ccline = "\nCc: $cc";
> +	if (@cc gt 0) {

"gt"?  I think you meant (@cc > 0) but you can also say "if (@cc) {" which
would most clearly convey what you want to say..

> +		$ccline = fold_header("\nCc: ", ", ", @cc);
>  	}
>  	my $sanitized_sender = sanitize_address($sender);
>  	make_message_id() unless defined($message_id);
> @@ -976,7 +1011,7 @@ X-Mailer: git-send-email $gitversion
>  		if ($smtp_server !~ m#^/#) {
>  			print "Server: $smtp_server\n";
>  			print "MAIL FROM:<$raw_from>\n";
> -			print "RCPT TO:".join(',',(map { "<$_>" } @recipients))."\n";
> +			print fold_header("RCPT TO:", ",", map { "<$_>" } @recipients)."\n";
I do not think this hunk is correct.

Shouldn't we be rather repeating "RCPT TO: " for each recipient, as
RFC2821 4.1.1.3 says (this is an issue with the original code)?  I do not
think SMTP's "RCPT TO" command has the notion of continuation line used
for the payload (i.e. RFC 2822 Internet Message Format), and folding the
line is a new bug this patch introduces.

>  		} else {
>  			print "Sendmail: $smtp_server ".join(' ',@sendmail_parameters)."\n";
>  		}

```

## Joe Perches, 2009-10-08 05:28

Subject: Re: [PATCH] git-send-email.perl: Fold long header lines to 78 chars
Message-ID: <1254979690.2056.103.camel@Joe-Laptop.home>
URL: https://gitlist.dev/e/1254979690.2056.103.camel%40Joe-Laptop.home
In-Reply-To: <7vd44yo4uz.fsf@alter.siamese.dyndns.org>

```
On Wed, 2009-10-07 at 22:02 -0700, Junio C Hamano wrote:
> Joe Perches <joe@perches.com> writes:
> > Some MTAs reject or filter long header lines which can
> > be generated if the cc list is only a few entries.
> > Fold long header lines to 78 chars to be more rfc compliant.
> >
> > -	my $cc = join(", ", unique_email_list(@cc));

It's probably better/simpler to not use fold_header and
just do the same join as "my $to"

	my $cc = join(",\n\t", unique_email_list(@cc));

> >  		if ($smtp_server !~ m#^/#) {
> >  			print "Server: $smtp_server\n";
> >  			print "MAIL FROM:<$raw_from>\n";
> > -			print "RCPT TO:".join(',',(map { "<$_>" } @recipients))."\n";
> > +			print fold_header("RCPT TO:", ",", map { "<$_>" } @recipients)."\n";
> I do not think this hunk is correct.
> Shouldn't we be rather repeating "RCPT TO: " for each recipient, as
> RFC2821 4.1.1.3 says (this is an issue with the original code)?

Looks like you're right.

Want a new patch or will you fix both issues?

I suggest using the same join as "To:" for "Cc:" and
multiple single line "RCPT TO:"s.

```

## Junio C Hamano, 2009-10-08 16:27

Subject: Re: [PATCH] git-send-email.perl: Fold long header lines to 78 chars
Message-ID: <7vy6nlhmw7.fsf@alter.siamese.dyndns.org>
URL: https://gitlist.dev/e/7vy6nlhmw7.fsf%40alter.siamese.dyndns.org
In-Reply-To: <1254979690.2056.103.camel@Joe-Laptop.home>

```
Joe Perches <joe@perches.com> writes:

>> I do not think this hunk is correct.
>> Shouldn't we be rather repeating "RCPT TO: " for each recipient, as
>> RFC2821 4.1.1.3 says (this is an issue with the original code)?
>
> Looks like you're right.
>
> Want a new patch or will you fix both issues?
>
> I suggest using the same join as "To:" for "Cc:" and
> multiple single line "RCPT TO:"s.

Sure.  Please make it so.

```

## Joe Perches, 2009-10-08 17:03

Subject: [PATCH v2] git-send-email.perl: fold multiple entry "Cc:" and multiple single line "RCPT TO:"s
Message-ID: <1255021406.2056.122.camel@Joe-Laptop.home>
URL: https://gitlist.dev/e/1255021406.2056.122.camel%40Joe-Laptop.home
In-Reply-To: <7vy6nlhmw7.fsf@alter.siamese.dyndns.org>

```
Some MTAs reject Cc: lines longer than 78 chars.
Avoid this by using the same join as "To:" ",\n\t"
so each subsequent Cc entry is on a new line.

RCPT TO: should have a single entry per line.
see: http://www.ietf.org/rfc/rfc2821.txt

Signed-off-by: Joe Perches <joe@perches.com>

diff --git a/git-send-email.perl b/git-send-email.perl
index dd821f7..ce81425 100755
--- a/git-send-email.perl
+++ b/git-send-email.perl
@@ -835,7 +835,7 @@ sub send_message
 	    $gitversion = Git::version();
 	}
 
-	my $cc = join(", ", unique_email_list(@cc));
+	my $cc = join(",\n\t", unique_email_list(@cc));
 	my $ccline = "";
 	if ($cc ne '') {
 		$ccline = "\nCc: $cc";
@@ -976,7 +976,9 @@ X-Mailer: git-send-email $gitversion
 		if ($smtp_server !~ m#^/#) {
 			print "Server: $smtp_server\n";
 			print "MAIL FROM:<$raw_from>\n";
-			print "RCPT TO:".join(',',(map { "<$_>" } @recipients))."\n";
+			foreach my $entry (@recipients) {
+			    print "RCPT TO:<$entry>\n";
+			}
 		} else {
 			print "Sendmail: $smtp_server ".join(' ',@sendmail_parameters)."\n";
 		}

```

## Junio C Hamano, 2009-10-09 06:50

Subject: Re: [PATCH v2] git-send-email.perl: fold multiple entry "Cc:" and multiple single line "RCPT TO:"s
Message-ID: <7vtyy95ae8.fsf@alter.siamese.dyndns.org>
URL: https://gitlist.dev/e/7vtyy95ae8.fsf%40alter.siamese.dyndns.org
In-Reply-To: <1255021406.2056.122.camel@Joe-Laptop.home>

```
Joe Perches <joe@perches.com> writes:

> Some MTAs reject Cc: lines longer than 78 chars.
> Avoid this by using the same join as "To:" ",\n\t"
> so each subsequent Cc entry is on a new line.
>
> RCPT TO: should have a single entry per line.
> see: http://www.ietf.org/rfc/rfc2821.txt
>
> Signed-off-by: Joe Perches <joe@perches.com>

Thanks.

```

## Junio C Hamano, 2009-10-10 00:57

Subject: Re: [PATCH v2] git-send-email.perl: fold multiple entry "Cc:" and multiple single line "RCPT TO:"s
Message-ID: <7vtyy8dq15.fsf@alter.siamese.dyndns.org>
URL: https://gitlist.dev/e/7vtyy8dq15.fsf%40alter.siamese.dyndns.org
In-Reply-To: <1255021406.2056.122.camel@Joe-Laptop.home>

```
This breaks t9001 since it expects an old (and probably incorrect) RCPT TO:
lines.  I fixed them up.

```
