Skip to content

Commit 8291db6

Browse files
peffgitster
authored andcommitted
git-send-email: add charset header if we add encoded 'From'
We sometimes pick out the original rfc822 'From' header and include it in the body of the message. If the original author's name needs encoding, then we should specify that in the content-type header. If we already had a content-type header in the mail, then we may need to re-encode. The logic is there to detect this case, but it doesn't actually do the re-encoding. Signed-off-by: Jeff King <peff@peff.net> Acked-by: Uwe Kleine-König <Uwe.Kleine-Koenig@digi.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent b57321f commit 8291db6

File tree

1 file changed

+31
-3
lines changed

1 file changed

+31
-3
lines changed

git-send-email.perl

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -468,11 +468,13 @@ sub make_message_id
468468

469469
sub unquote_rfc2047 {
470470
local ($_) = @_;
471-
if (s/=\?utf-8\?q\?(.*)\?=/$1/g) {
471+
my $encoding;
472+
if (s/=\?([^?]+)\?q\?(.*)\?=/$2/g) {
473+
$encoding = $1;
472474
s/_/ /g;
473475
s/=([0-9A-F]{2})/chr(hex($1))/eg;
474476
}
475-
return "$_";
477+
return wantarray ? ($_, $encoding) : $_;
476478
}
477479

478480
# use the simplest quoting being able to handle the recipient
@@ -599,6 +601,9 @@ sub send_message
599601
open(F,"<",$t) or die "can't open file $t";
600602

601603
my $author = undef;
604+
my $author_encoding;
605+
my $has_content_type;
606+
my $body_encoding;
602607
@cc = @initial_cc;
603608
@xh = ();
604609
my $input_format = undef;
@@ -624,12 +629,20 @@ sub send_message
624629
next if ($suppress_from);
625630
}
626631
elsif ($1 eq 'From') {
627-
$author = unquote_rfc2047($2);
632+
($author, $author_encoding)
633+
= unquote_rfc2047($2);
628634
}
629635
printf("(mbox) Adding cc: %s from line '%s'\n",
630636
$2, $_) unless $quiet;
631637
push @cc, $2;
632638
}
639+
elsif (/^Content-type:/i) {
640+
$has_content_type = 1;
641+
if (/charset="?[^ "]+/) {
642+
$body_encoding = $1;
643+
}
644+
push @xh, $_;
645+
}
633646
elsif (!/^Date:\s/ && /^[-A-Za-z]+:\s+\S/) {
634647
push @xh, $_;
635648
}
@@ -686,6 +699,21 @@ sub send_message
686699

687700
if (defined $author) {
688701
$message = "From: $author\n\n$message";
702+
if (defined $author_encoding) {
703+
if ($has_content_type) {
704+
if ($body_encoding eq $author_encoding) {
705+
# ok, we already have the right encoding
706+
}
707+
else {
708+
# uh oh, we should re-encode
709+
}
710+
}
711+
else {
712+
push @xh,
713+
'MIME-Version: 1.0',
714+
"Content-Type: text/plain; charset=$author_encoding";
715+
}
716+
}
689717
}
690718

691719
send_message();

0 commit comments

Comments
 (0)