git/list[1] front-page[2] threads[3] people[4] search[5] about
 

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

From
Joe Perches <joe@perches.com>
Date
Oct 5, 2009, 16:24 UTC
Message-ID
<1254759898.1799.449.camel@Joe-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";
 		}
Next: Junio C Hamano
Message 1 of 7 in “git-send-email.perl: Fold long header lines to 78 chars”
  1. git-send-email.perl: Fold long header lines to 78 charsJoe Perches, Oct 5, 2009
  2. Junio C HamanoOct 8, 2009
  3. Joe PerchesOct 8, 2009
  4. Junio C HamanoOct 8, 2009
  5. git-send-email.perl: fold multiple entry "Cc:" and multiple single line "RCPT TO:"sJoe Perches, Oct 8, 2009
  6. Junio C HamanoOct 9, 2009
  7. Junio C HamanoOct 10, 2009

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.