Skip to content

Commit a2f5be5

Browse files
committed
Merge branch 'jk/maint-send-email-compose' into maint
* jk/maint-send-email-compose: send-email: rfc2047-quote subject lines with non-ascii characters send-email: specify content-type of --compose body
2 parents 93c7b9c + d54eaaa commit a2f5be5

File tree

2 files changed

+100
-2
lines changed

2 files changed

+100
-2
lines changed

git-send-email.perl

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -518,8 +518,30 @@ sub expand_aliases {
518518
open(C,"<",$compose_filename)
519519
or die "Failed to open $compose_filename : " . $!;
520520

521+
my $need_8bit_cte = file_has_nonascii($compose_filename);
522+
my $in_body = 0;
521523
while(<C>) {
522524
next if m/^GIT: /;
525+
if (!$in_body && /^\n$/) {
526+
$in_body = 1;
527+
if ($need_8bit_cte) {
528+
print C2 "MIME-Version: 1.0\n",
529+
"Content-Type: text/plain; ",
530+
"charset=utf-8\n",
531+
"Content-Transfer-Encoding: 8bit\n";
532+
}
533+
}
534+
if (!$in_body && /^MIME-Version:/i) {
535+
$need_8bit_cte = 0;
536+
}
537+
if (!$in_body && /^Subject: ?(.*)/i) {
538+
my $subject = $1;
539+
$_ = "Subject: " .
540+
($subject =~ /[^[:ascii:]]/ ?
541+
quote_rfc2047($subject) :
542+
$subject) .
543+
"\n";
544+
}
523545
print C2 $_;
524546
}
525547
close(C);
@@ -610,6 +632,14 @@ sub unquote_rfc2047 {
610632
return wantarray ? ($_, $encoding) : $_;
611633
}
612634

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+
613643
# use the simplest quoting being able to handle the recipient
614644
sub sanitize_address
615645
{
@@ -627,8 +657,7 @@ sub sanitize_address
627657

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

634663
# double quotes are needed if specials or CTLs are included
@@ -956,3 +985,13 @@ sub validate_patch {
956985
}
957986
return undef;
958987
}
988+
989+
sub file_has_nonascii {
990+
my $fn = shift;
991+
open(my $fh, '<', $fn)
992+
or die "unable to open $fn: $!\n";
993+
while (my $line = <$fh>) {
994+
return 1 if $line =~ /[^[:ascii:]]/;
995+
}
996+
return 0;
997+
}

t/t9001-send-email.sh

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,4 +166,63 @@ test_expect_success 'second message is patch' '
166166
grep "Subject:.*Second" msgtxt2
167167
'
168168

169+
test_expect_success '--compose adds MIME for utf8 body' '
170+
clean_fake_sendmail &&
171+
(echo "#!/bin/sh" &&
172+
echo "echo utf8 body: àéìöú >>\$1"
173+
) >fake-editor-utf8 &&
174+
chmod +x fake-editor-utf8 &&
175+
echo y | \
176+
GIT_EDITOR=$(pwd)/fake-editor-utf8 \
177+
GIT_SEND_EMAIL_NOTTY=1 \
178+
git send-email \
179+
--compose --subject foo \
180+
--from="Example <[email protected]>" \
181+
182+
--smtp-server="$(pwd)/fake.sendmail" \
183+
$patches &&
184+
grep "^utf8 body" msgtxt1 &&
185+
grep "^Content-Type: text/plain; charset=utf-8" msgtxt1
186+
'
187+
188+
test_expect_success '--compose respects user mime type' '
189+
clean_fake_sendmail &&
190+
(echo "#!/bin/sh" &&
191+
echo "(echo MIME-Version: 1.0"
192+
echo " echo Content-Type: text/plain\\; charset=iso-8859-1"
193+
echo " echo Content-Transfer-Encoding: 8bit"
194+
echo " echo Subject: foo"
195+
echo " echo "
196+
echo " echo utf8 body: àéìöú) >\$1"
197+
) >fake-editor-utf8-mime &&
198+
chmod +x fake-editor-utf8-mime &&
199+
echo y | \
200+
GIT_EDITOR=$(pwd)/fake-editor-utf8-mime \
201+
GIT_SEND_EMAIL_NOTTY=1 \
202+
git send-email \
203+
--compose --subject foo \
204+
--from="Example <[email protected]>" \
205+
206+
--smtp-server="$(pwd)/fake.sendmail" \
207+
$patches &&
208+
grep "^utf8 body" msgtxt1 &&
209+
grep "^Content-Type: text/plain; charset=iso-8859-1" msgtxt1 &&
210+
! grep "^Content-Type: text/plain; charset=utf-8" msgtxt1
211+
'
212+
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+
169228
test_done

0 commit comments

Comments
 (0)