Skip to content

Commit d54eaaa

Browse files
peffgitster
authored andcommitted
send-email: rfc2047-quote subject lines with non-ascii characters
We always use 'utf-8' as the encoding, since we currently have no way of getting the information from the user. This also refactors the quoting of recipient names, since both processes can share the rfc2047 quoting code. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 0706bd1 commit d54eaaa

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

git-send-email.perl

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,14 @@ sub expand_aliases {
534534
if (!$in_body && /^MIME-Version:/i) {
535535
$need_8bit_cte = 0;
536536
}
537+
if (!$in_body && /^Subject: ?(.*)/i) {
538+
my $subject = $1;
539+
$_ = "Subject: " .
540+
($subject =~ /[^[:ascii:]]/ ?
541+
quote_rfc2047($subject) :
542+
$subject) .
543+
"\n";
544+
}
537545
print C2 $_;
538546
}
539547
close(C);
@@ -624,6 +632,14 @@ sub unquote_rfc2047 {
624632
return wantarray ? ($_, $encoding) : $_;
625633
}
626634

635+
sub quote_rfc2047 {
636+
local $_ = shift;
637+
my $encoding = shift || 'utf-8';
638+
s/([^-a-zA-Z0-9!*+\/])/sprintf("=%02X", ord($1))/eg;
639+
s/(.*)/=\?$encoding\?q\?$1\?=/;
640+
return $_;
641+
}
642+
627643
# use the simplest quoting being able to handle the recipient
628644
sub sanitize_address
629645
{
@@ -641,8 +657,7 @@ sub sanitize_address
641657

642658
# rfc2047 is needed if a non-ascii char is included
643659
if ($recipient_name =~ /[^[:ascii:]]/) {
644-
$recipient_name =~ s/([^-a-zA-Z0-9!*+\/])/sprintf("=%02X", ord($1))/eg;
645-
$recipient_name =~ s/(.*)/=\?utf-8\?q\?$1\?=/;
660+
$recipient_name = quote_rfc2047($recipient_name);
646661
}
647662

648663
# double quotes are needed if specials or CTLs are included

t/t9001-send-email.sh

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,4 +210,19 @@ test_expect_success '--compose respects user mime type' '
210210
! grep "^Content-Type: text/plain; charset=utf-8" msgtxt1
211211
'
212212

213+
test_expect_success '--compose adds MIME for utf8 subject' '
214+
clean_fake_sendmail &&
215+
echo y | \
216+
GIT_EDITOR=$(pwd)/fake-editor \
217+
GIT_SEND_EMAIL_NOTTY=1 \
218+
git send-email \
219+
--compose --subject utf8-sübjëct \
220+
--from="Example <[email protected]>" \
221+
222+
--smtp-server="$(pwd)/fake.sendmail" \
223+
$patches &&
224+
grep "^fake edit" msgtxt1 &&
225+
grep "^Subject: =?utf-8?q?utf8-s=C3=BCbj=C3=ABct?=" msgtxt1
226+
'
227+
213228
test_done

0 commit comments

Comments
 (0)