Skip to content

Commit 54fcb21

Browse files
committed
Merge branch 'tr/send-email-8bit' into maint
* tr/send-email-8bit: send-email: ask about and declare 8bit mails
2 parents d60ad81 + 3cae7e5 commit 54fcb21

File tree

3 files changed

+146
-0
lines changed

3 files changed

+146
-0
lines changed

Documentation/git-send-email.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,15 @@ See the CONFIGURATION section for 'sendemail.multiedit'.
101101
+
102102
The --to option must be repeated for each user you want on the to list.
103103

104+
--8bit-encoding=<encoding>::
105+
When encountering a non-ASCII message or subject that does not
106+
declare its encoding, add headers/quoting to indicate it is
107+
encoded in <encoding>. Default is the value of the
108+
'sendemail.assume8bitEncoding'; if that is unspecified, this
109+
will be prompted for if any non-ASCII files are encountered.
110+
+
111+
Note that no attempts whatsoever are made to validate the encoding.
112+
104113

105114
Sending
106115
~~~~~~~

git-send-email.perl

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ sub usage {
5454
--in-reply-to <str> * Email "In-Reply-To:"
5555
--annotate * Review each patch that will be sent in an editor.
5656
--compose * Open an editor for introduction.
57+
--8bit-encoding <str> * Encoding to assume 8bit mails if undeclared
5758
5859
Sending:
5960
--envelope-sender <str> * Email envelope sender.
@@ -191,6 +192,7 @@ sub do_edit {
191192
my ($identity, $aliasfiletype, @alias_files, @smtp_host_parts, $smtp_domain);
192193
my ($validate, $confirm);
193194
my (@suppress_cc);
195+
my ($auto_8bit_encoding);
194196

195197
my ($debug_net_smtp) = 0; # Net::SMTP, see send_message()
196198

@@ -222,6 +224,7 @@ sub do_edit {
222224
"multiedit" => \$multiedit,
223225
"confirm" => \$confirm,
224226
"from" => \$sender,
227+
"assume8bitencoding" => \$auto_8bit_encoding,
225228
);
226229

227230
# Help users prepare for 1.7.0
@@ -297,6 +300,7 @@ sub signal_handler {
297300
"thread!" => \$thread,
298301
"validate!" => \$validate,
299302
"format-patch!" => \$format_patch,
303+
"8bit-encoding=s" => \$auto_8bit_encoding,
300304
);
301305

302306
unless ($rc) {
@@ -669,6 +673,35 @@ sub ask {
669673
return undef;
670674
}
671675

676+
my %broken_encoding;
677+
678+
sub file_declares_8bit_cte($) {
679+
my $fn = shift;
680+
open (my $fh, '<', $fn);
681+
while (my $line = <$fh>) {
682+
last if ($line =~ /^$/);
683+
return 1 if ($line =~ /^Content-Transfer-Encoding: .*8bit.*$/);
684+
}
685+
close $fh;
686+
return 0;
687+
}
688+
689+
foreach my $f (@files) {
690+
next unless (body_or_subject_has_nonascii($f)
691+
&& !file_declares_8bit_cte($f));
692+
$broken_encoding{$f} = 1;
693+
}
694+
695+
if (!defined $auto_8bit_encoding && scalar %broken_encoding) {
696+
print "The following files are 8bit, but do not declare " .
697+
"a Content-Transfer-Encoding.\n";
698+
foreach my $f (sort keys %broken_encoding) {
699+
print " $f\n";
700+
}
701+
$auto_8bit_encoding = ask("Which 8bit encoding should I declare [UTF-8]? ",
702+
default => "UTF-8");
703+
}
704+
672705
my $prompting = 0;
673706
if (!defined $sender) {
674707
$sender = $repoauthor || $repocommitter || '';
@@ -1221,6 +1254,18 @@ sub send_message {
12211254
or die "(cc-cmd) failed to close pipe to '$cc_cmd'";
12221255
}
12231256

1257+
if ($broken_encoding{$t} && !$has_content_type) {
1258+
$has_content_type = 1;
1259+
push @xh, "MIME-Version: 1.0",
1260+
"Content-Type: text/plain; charset=$auto_8bit_encoding",
1261+
"Content-Transfer-Encoding: 8bit";
1262+
$body_encoding = $auto_8bit_encoding;
1263+
}
1264+
1265+
if ($broken_encoding{$t} && !is_rfc2047_quoted($subject)) {
1266+
$subject = quote_rfc2047($subject, $auto_8bit_encoding);
1267+
}
1268+
12241269
if (defined $author and $author ne $sender) {
12251270
$message = "From: $author\n\n$message";
12261271
if (defined $author_encoding) {
@@ -1233,6 +1278,7 @@ sub send_message {
12331278
}
12341279
}
12351280
else {
1281+
$has_content_type = 1;
12361282
push @xh,
12371283
'MIME-Version: 1.0',
12381284
"Content-Type: text/plain; charset=$author_encoding",
@@ -1310,3 +1356,17 @@ sub file_has_nonascii {
13101356
}
13111357
return 0;
13121358
}
1359+
1360+
sub body_or_subject_has_nonascii {
1361+
my $fn = shift;
1362+
open(my $fh, '<', $fn)
1363+
or die "unable to open $fn: $!\n";
1364+
while (my $line = <$fh>) {
1365+
last if $line =~ /^$/;
1366+
return 1 if $line =~ /^Subject.*[^[:ascii:]]/;
1367+
}
1368+
while (my $line = <$fh>) {
1369+
return 1 if $line =~ /[^[:ascii:]]/;
1370+
}
1371+
return 0;
1372+
}

t/t9001-send-email.sh

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -918,4 +918,81 @@ test_expect_success '--no-bcc overrides sendemail.bcc' '
918918
! grep "RCPT TO:<[email protected]>" stdout
919919
'
920920

921+
cat >email-using-8bit <<EOF
922+
From fe6ecc66ece37198fe5db91fa2fc41d9f4fe5cc4 Mon Sep 17 00:00:00 2001
923+
Message-Id: <[email protected]>
924+
925+
Date: Sat, 12 Jun 2010 15:53:58 +0200
926+
Subject: subject goes here
927+
928+
Dieser deutsche Text enthält einen Umlaut!
929+
EOF
930+
931+
cat >content-type-decl <<EOF
932+
MIME-Version: 1.0
933+
Content-Type: text/plain; charset=UTF-8
934+
Content-Transfer-Encoding: 8bit
935+
EOF
936+
937+
test_expect_success 'asks about and fixes 8bit encodings' '
938+
clean_fake_sendmail &&
939+
echo |
940+
941+
--smtp-server="$(pwd)/fake.sendmail" \
942+
email-using-8bit >stdout &&
943+
grep "do not declare a Content-Transfer-Encoding" stdout &&
944+
grep email-using-8bit stdout &&
945+
grep "Which 8bit encoding" stdout &&
946+
grep "Content\\|MIME" msgtxt1 >actual &&
947+
test_cmp actual content-type-decl
948+
'
949+
950+
test_expect_success 'sendemail.8bitEncoding works' '
951+
clean_fake_sendmail &&
952+
git config sendemail.assume8bitEncoding UTF-8 &&
953+
echo bogus |
954+
955+
--smtp-server="$(pwd)/fake.sendmail" \
956+
email-using-8bit >stdout &&
957+
grep "Content\\|MIME" msgtxt1 >actual &&
958+
test_cmp actual content-type-decl
959+
'
960+
961+
test_expect_success '--8bit-encoding overrides sendemail.8bitEncoding' '
962+
clean_fake_sendmail &&
963+
git config sendemail.assume8bitEncoding "bogus too" &&
964+
echo bogus |
965+
966+
--smtp-server="$(pwd)/fake.sendmail" \
967+
--8bit-encoding=UTF-8 \
968+
email-using-8bit >stdout &&
969+
grep "Content\\|MIME" msgtxt1 >actual &&
970+
test_cmp actual content-type-decl
971+
'
972+
973+
cat >email-using-8bit <<EOF
974+
From fe6ecc66ece37198fe5db91fa2fc41d9f4fe5cc4 Mon Sep 17 00:00:00 2001
975+
Message-Id: <[email protected]>
976+
977+
Date: Sat, 12 Jun 2010 15:53:58 +0200
978+
Subject: Dieser Betreff enthält auch einen Umlaut!
979+
980+
Nothing to see here.
981+
EOF
982+
983+
cat >expected <<EOF
984+
Subject: =?UTF-8?q?Dieser=20Betreff=20enth=C3=A4lt=20auch=20einen=20Umlaut!?=
985+
EOF
986+
987+
test_expect_success '--8bit-encoding also treats subject' '
988+
clean_fake_sendmail &&
989+
echo bogus |
990+
991+
--smtp-server="$(pwd)/fake.sendmail" \
992+
--8bit-encoding=UTF-8 \
993+
email-using-8bit >stdout &&
994+
grep "Subject" msgtxt1 >actual &&
995+
test_cmp expected actual
996+
'
997+
921998
test_done

0 commit comments

Comments
 (0)