send-email: add an auto option for transfer encoding

For most patches, using a transfer encoding of 8bit provides good compatibility with most servers and makes it as easy as possible to view patches. However, there are some patches for which 8bit is not a valid encoding: RFC 5322 specifies that a message must not have lines exceeding 998 octets. Add a transfer encoding value, auto, which indicates that a patch should use 8bit where allowed and quoted-printable otherwise. Choose quoted-printable instead of base64, since base64-encoded plain text is treated as suspicious by some spam filters. Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

brian m. carlson committed Jul 8, 2018 at 22:17 UTC 7a36987ffffa59052723ed7299c1de25bc18048a
3 files changed +37 -9
Documentation/git-send-email.txt
+7 -4
@@ -137,15 +137,18 @@ Note that no attempts whatsoever are made to validate the encoding.
137 Specify encoding of compose message. Default is the value of the
138 'sendemail.composeencoding'; if that is unspecified, UTF-8 is assumed.
139
140 ---transfer-encoding=(7bit|8bit|quoted-printable|base64)::
140 +--transfer-encoding=(7bit|8bit|quoted-printable|base64|auto)::
141 Specify the transfer encoding to be used to send the message over SMTP.
142 7bit will fail upon encountering a non-ASCII message. quoted-printable
143 can be useful when the repository contains files that contain carriage
144 returns, but makes the raw patch email file (as saved from a MUA) much
145 harder to inspect manually. base64 is even more fool proof, but also
146 - even more opaque. Default is the value of the `sendemail.transferEncoding`
147 - configuration value; if that is unspecified, git will use 8bit and not
148 - add a Content-Transfer-Encoding header.
146 + even more opaque. auto will use 8bit when possible, and quoted-printable
147 + otherwise.
148 ++
149 +Default is the value of the `sendemail.transferEncoding` configuration
150 +value; if that is unspecified, git will use 8bit and not add a
151 +Content-Transfer-Encoding header.
152
153 --xmailer::
154 --no-xmailer::
git-send-email.perl
+7 -5
@@ -1739,9 +1739,8 @@ sub process_file {
1739 }
1740 if (defined $target_xfer_encoding) {
1741 $xfer_encoding = '8bit' if not defined $xfer_encoding;
1742 - $message = apply_transfer_encoding(
1742 + ($message, $xfer_encoding) = apply_transfer_encoding(
1743 $message, $xfer_encoding, $target_xfer_encoding);
1744 - $xfer_encoding = $target_xfer_encoding;
1744 }
1745 if (defined $xfer_encoding) {
1746 push @xh, "Content-Transfer-Encoding: $xfer_encoding";
@@ -1852,13 +1851,16 @@ sub apply_transfer_encoding {
1851 $message = MIME::Base64::decode($message)
1852 if ($from eq 'base64');
1853
1854 + $to = ($message =~ /.{999,}/) ? 'quoted-printable' : '8bit'
1855 + if $to eq 'auto';
1856 +
1857 die __("cannot send message as 7bit")
1858 if ($to eq '7bit' and $message =~ /[^[:ascii:]]/);
1857 - return $message
1859 + return ($message, $to)
1860 if ($to eq '7bit' or $to eq '8bit');
1859 - return MIME::QuotedPrint::encode($message, "\n", 0)
1861 + return (MIME::QuotedPrint::encode($message, "\n", 0), $to)
1862 if ($to eq 'quoted-printable');
1861 - return MIME::Base64::encode($message, "\n")
1863 + return (MIME::Base64::encode($message, "\n"), $to)
1864 if ($to eq 'base64');
1865 die __("invalid transfer encoding");
1866 }
t/t9001-send-email.sh
+23
@@ -456,6 +456,29 @@ test_expect_success $PREREQ 'allow long lines with --no-validate' '
456 2>errors
457 '
458
459 +test_expect_success $PREREQ 'short lines with auto encoding are 8bit' '
460 + clean_fake_sendmail &&
461 + git send-email \
462 + --from="A <author@example.com>" \
463 + --to=nobody@example.com \
464 + --smtp-server="$(pwd)/fake.sendmail" \
465 + --transfer-encoding=auto \
466 + $patches &&
467 + grep "Content-Transfer-Encoding: 8bit" msgtxt1
468 +'
469 +
470 +test_expect_success $PREREQ 'long lines with auto encoding are quoted-printable' '
471 + clean_fake_sendmail &&
472 + git send-email \
473 + --from="Example <nobody@example.com>" \
474 + --to=nobody@example.com \
475 + --smtp-server="$(pwd)/fake.sendmail" \
476 + --transfer-encoding=auto \
477 + --no-validate \
478 + longline.patch &&
479 + grep "Content-Transfer-Encoding: quoted-printable" msgtxt1
480 +'
481 +
482 test_expect_success $PREREQ 'Invalid In-Reply-To' '
483 clean_fake_sendmail &&
484 git send-email \