Bad mail encoding (breaks DKIM signature)

Hi,

I have a problem with the encoding of my mails, resulting in bad mail part encoding, which makes DKIM signature invalid.

Server softwares:

  • Debian 10 Buster (previously 9 Stretch, problem was already present)
  • PostgreSQL 11 (previously 9.6)
  • Perl 5.28 (previously 5.24)
  • RT 4.4.4

DB encoding:

  • Collationnement: fr_FR.UTF-8
  • Type caract.: fr_FR.UTF-8

RT settings:

Set(@EmailInputEncodings, qw(utf-8 iso-8859-1 us-ascii));
Set($EmailOutputEncoding, "UTF-8");
Set($HTMLFormatter, undef);
Set($MailCommand, "sendmail");
Set($SendmailArguments, "-oi");

Here’s an example of badly encoded autoreply HTML mail:

Return-Path: <tech@framasoft.org>
Received: from bm.didry.org (localhost [127.0.0.1])
	 by fry with LMTPA;
	 Thu, 25 Jul 2019 16:36:37 +0200
X-Sieve: CMU Sieve 2.4
Received: from /192.168.1.42 (LHLO bm.didry.org) by unknown with LMTP;
 7/25/19 2:36 PM
Received: from wolf.framasoft.org (wolf.framasoft.org [IPv6:2a01:4f8:191:74::150])
	(using TLSv1.2 with cipher ADH-AES256-GCM-SHA384 (256/256 bits))
	(No client certificate requested)
	by bm.didry.org (Postfix) with ESMTPS id CE6D1C0062
	for <luc@didry.org>; Thu, 25 Jul 2019 16:36:36 +0200 (CEST)
Received: by wolf.framasoft.org (Postfix, from userid 33)
	id 4BAE32E0D06; Thu, 25 Jul 2019 16:36:36 +0200 (CEST)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=framasoft.org;
	s=dkim; t=1564065396;
	h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date:
	 message-id:message-id:to:to:cc:mime-version:mime-version:
	 content-type:content-type:
	 content-transfer-encoding:content-transfer-encoding:
	 in-reply-to:in-reply-to:references:references;
	bh=+dbJTMbPjNwLbeJPuM8wqGIpgZBI9hA+vfH988/Aqdo=;
	b=aCAmRD++86SnUkFX+zim9PRm/x6w7G4hrFw10rrof8rrg6vanRikP038F//xzQIBVUtXnV
	BT6L33/qORgTWjyDHPXGzLBkrGP6ebgPm6bHFhS7TmBOcUuuhrPLeJy8hcMCeQ5HtnDlBd
	/jee1ut6MYamjfgaXbh7PjJ8scFqjHQ=
Date: Thu, 25 Jul 2019 16:36:36 +0200
X-RT-Loop-Prevention: FramaTicket
From: File perso pour Luc <rt+luc@framasoft.org>
Reply-To: rt+luc@framasoft.org
Sender: "www-data" <REDACTED>
Precedence: bulk
In-Reply-To: <201907251436.x6PEaWNP011103@odysseus.fiat-tux.fr>
X-RT-Ticket: FramaTicket #31179
Message-ID: <rt-4.4.4-21746-1564065395-923.31179-7-0@framasoft.org>
Subject: =?UTF8?B?W0RlbWFuZGUgcGVyc28gcG91ciBMdWMgIzMxMTc5XSBSw6lwb25zZSBhdXRvbWF0?= =?UTF8?B?aXF1ZSA6IHRlc3Q=?=
Content-Transfer-Encoding: 8bit
X-Mailer: Perl5 Mail::Internet v2.18
X-RT-Originator: REDACTED
X-Managed-BY: RT 4.4.4 (http://www.bestpractical.com/rt/)
To: luc@didry.org
Content-Type: multipart/alternative; boundary="----------=_1564065395-21746-0"
MIME-Version: 1.0
References: <RT-Ticket-31179@framasoft.org>
 <201907251436.x6PEaWNP011103@odysseus.fiat-tux.fr>
Auto-Submitted: auto-replied
X-Bm-Milter-Handled: 3651b1e0-29d9-4f34-bcad-cf1db18b5f60
X-Bm-Transport-Timestamp: 1564065396913
X-Rspamd-Queue-Id: CE6D1C0062
X-Spamd-Result: default: False [-0.50 / 30.00];
	 ARC_NA(0.00)[];
	 HAS_REPLYTO(0.00)[rt+luc@framasoft.org];
	 MID_RHS_MATCH_FROM(0.00)[];
	 FROM_HAS_DN(0.00)[];
	 R_DKIM_REJECT(0.00)[framasoft.org:s=dkim];
	 PRECEDENCE_BULK(0.00)[];
	 MIME_GOOD(-0.10)[multipart/alternative,text/plain];
	 R_SPF_ALLOW(-0.20)[+a:wolf.framasoft.org];
	 TO_DN_NONE(0.00)[];
	 RCPT_COUNT_ONE(0.00)[1];
	 REPLYTO_DOM_EQ_FROM_DOM(0.00)[];
	 TO_MATCH_ENVRCPT_ALL(0.00)[];
	 DKIM_TRACE(0.00)[framasoft.org:-];
	 DMARC_POLICY_ALLOW(0.00)[framasoft.org,none];
	 DMARC_POLICY_ALLOW_WITH_FAILURES(-0.50)[];
	 FORGED_SENDER(0.30)[rt@framasoft.org,REDACTED];
	 RCVD_COUNT_ZERO(0.00)[0];
	 MIME_TRACE(0.00)[0:+,1:+,2:~];
	 ASN(0.00)[asn:24940, ipnet:2a01:4f8::/29, country:DE];
	 TAGGED_FROM(0.00)[luc];
	 FROM_NEQ_ENVFROM(0.00)[rt@framasoft.org,REDACTED]
X-Rspamd-Server: fry

 This is a multi-part message in MIME format...

------------=_1564065395-21746-0
Subject: Réponse automatique : test
Content-Type: text/plain; charset="utf8"
X-RT-Original-Encoding: utf-8

Bonjour,

Ceci est un email automatique en réponse à votre demande concernant : test,
dont un résumé apparaît ci-dessous.

Oui, à Framasoft, on est comme vous : on n'aime pas les réponses automatiques.
Nous utilisons juste un système de tickets parce que c'est le meilleur moyen
de vous mettre en contact rapidement avec la personne la plus apte à vous
répondre.

Bref, pas d'inquiétude : ceci n'est qu'un accusé de réception, y a de vrais
gens (souvent des bénévoles de notre association) qui arrivent pour discuter
avec vous !

Votre "ticket" a reçu l'identifiant [Demande perso pour Luc #31179].
Veuillez inclure cet identifiant : [Demande perso pour Luc #31179] dans le
sujet de tout futur message relatif à ce sujet.

Il n'y a pas besoin de répondre à ce message pour l'instant.

Merci,
rt+luc@framasoft.org

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

test


------------=_1564065395-21746-0
Content-Type: text/html; charset="utf8"
X-RT-Original-Encoding: utf-8

<p>Bonjour,</p>

<p>Ceci est un email automatique en réponse à votre demande concernant : <b>test</b>,
dont un résumé apparaît ci-dessous.</p>

<p>Oui, à Framasoft, on est comme vous : on n'aime pas les réponses automatiques. Nous utilisons juste un système de tickets parce que c'est le meilleur moyen de vous mettre en contact rapidement avec la personne la plus apte à vous répondre.</p>

<p>Bref, pas d'inquiétude : ceci n'est qu'un accusé de réception, y a de vrais gens (souvent des bénévoles de notre association) qui arrivent pour discuter avec vous !</p>

<p>Votre "ticket" a reçu l'identifiant <b>[Demande perso pour Luc #31179]</b>.<br>
Veuillez inclure cet identifiant : <b>[Demande perso pour Luc #31179]</b>
dans le sujet de tout futur message relatif à ce sujet.</p>

<p>Il n'y a pas besoin de répondre à ce message pour l'instant.</p>

<p>Merci,<br/>
rt+luc@framasoft.org</p>

<hr/>
<pre style="white-space: pre-wrap; font-family: monospace;">test
</pre>
------------=_1564065395-21746-0--

As you can see, the multiparts are badly encoded. My mail client shows the text correctly though.

I tried to change the output encoding to iso-8859-1 (Set($EmailOutputEncoding, "iso-8859-1");). It fixed the HTML part, but not the text/plain part:

Return-Path: <tech@framasoft.org>
Received: from bm.didry.org (localhost [127.0.0.1])
	 by fry with LMTPA;
	 Thu, 25 Jul 2019 16:46:46 +0200
X-Sieve: CMU Sieve 2.4
Received: from /192.168.1.42 (LHLO bm.didry.org) by unknown with LMTP;
 7/25/19 2:46 PM
Received: from wolf.framasoft.org (wolf.framasoft.org [136.243.187.150])
	(using TLSv1.2 with cipher ADH-AES256-GCM-SHA384 (256/256 bits))
	(No client certificate requested)
	by bm.didry.org (Postfix) with ESMTPS id D6139C0062
	for <luc@didry.org>; Thu, 25 Jul 2019 16:46:45 +0200 (CEST)
Received: by wolf.framasoft.org (Postfix, from userid 33)
	id 494012E0D06; Thu, 25 Jul 2019 16:46:45 +0200 (CEST)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=framasoft.org;
	s=dkim; t=1564066005;
	h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date:
	 message-id:message-id:to:to:cc:mime-version:mime-version:
	 content-type:content-type:
	 content-transfer-encoding:content-transfer-encoding:
	 in-reply-to:in-reply-to:references:references;
	bh=eLwuUMWKkXbQ4JuVJcCJcovq1vA4W2p+8KTL4i+7o8w=;
	b=jDynPCWPGSTwhSrod2L5x/J/lxfVBQIb5FbqO74w8OFU/XqQns+cMvkMkzQGNXtF9hHJFh
	nBNRMYlWi4VDz2qQ9H2rSS+F0rNkrag7cPGaBbDClk0k+5HKOwyd03skeMwiKI4K4Ci7bO
	fLn9lLsZolqQ3fcYjjaj6nqiZ1P/KTg=
Content-Type: multipart/alternative; boundary="----------=_1564066005-24293-3"
To: luc@didry.org
Precedence: bulk
In-Reply-To: <201907251446.x6PEkhos011968@odysseus.fiat-tux.fr>
X-Managed-BY: RT 4.4.4 (http://www.bestpractical.com/rt/)
X-RT-Originator: REDACTED
MIME-Version: 1.0
Auto-Submitted: auto-replied
X-Mailer: Perl5 Mail::Internet v2.18
Reply-To: rt+luc@framasoft.org
Subject: =?ISO-8859-1?B?W0RlbWFuZGUgcGVyc28gcG91ciBMdWMgIzMxMTg1XSBS6XBvbnNlIGF1?= =?ISO-8859-1?B?dG9tYXRpcXVlIDogdGVzdA==?=
From: File perso pour Luc <rt+luc@framasoft.org>
X-RT-Ticket: FramaTicket #31185
Sender: "www-data" <tech@framasoft.org>
X-RT-Loop-Prevention: FramaTicket
References: <RT-Ticket-31185@framasoft.org>
 <201907251446.x6PEkhos011968@odysseus.fiat-tux.fr>
Date: Thu, 25 Jul 2019 16:46:45 +0200
Message-ID: <rt-4.4.4-24293-1564066005-746.31185-7-0@framasoft.org>
Content-Transfer-Encoding: 8bit
X-Bm-Milter-Handled: 3651b1e0-29d9-4f34-bcad-cf1db18b5f60
X-Bm-Transport-Timestamp: 1564066005927
X-Rspamd-Queue-Id: D6139C0062
X-Spamd-Result: default: False [-0.50 / 30.00];
	 ARC_NA(0.00)[];
	 HAS_REPLYTO(0.00)[rt+luc@framasoft.org];
	 MID_RHS_MATCH_FROM(0.00)[];
	 FROM_HAS_DN(0.00)[];
	 R_SPF_ALLOW(-0.20)[+a:wolf.framasoft.org:c];
	 R_DKIM_REJECT(0.00)[framasoft.org:s=dkim];
	 MIME_GOOD(-0.10)[multipart/alternative,text/plain];
	 TO_DN_NONE(0.00)[];
	 PRECEDENCE_BULK(0.00)[];
	 RCPT_COUNT_ONE(0.00)[1];
	 REPLYTO_DOM_EQ_FROM_DOM(0.00)[];
	 TO_MATCH_ENVRCPT_ALL(0.00)[];
	 DKIM_TRACE(0.00)[framasoft.org:-];
	 DMARC_POLICY_ALLOW(0.00)[framasoft.org,none];
	 DMARC_POLICY_ALLOW_WITH_FAILURES(-0.50)[];
	 FORGED_SENDER(0.30)[rt@framasoft.org,REDACTED];
	 RCVD_COUNT_ZERO(0.00)[0];
	 MIME_TRACE(0.00)[0:+,1:+,2:~];
	 ASN(0.00)[asn:24940, ipnet:136.243.0.0/16, country:DE];
	 TAGGED_FROM(0.00)[luc];
	 FROM_NEQ_ENVFROM(0.00)[rt@framasoft.org,REDACTED]
X-Rspamd-Server: fry

 This is a multi-part message in MIME format...

------------=_1564066005-24293-3
Subject: Réponse automatique : test
Content-Type: text/plain; charset="utf-8"
X-RT-Original-Encoding: utf-8

Bonjour,

Ceci est un email automatique en réponse à votre demande concernant : test,
dont un résumé apparaît ci-dessous.

Oui, à Framasoft, on est comme vous : on n'aime pas les réponses automatiques.
Nous utilisons juste un système de tickets parce que c'est le meilleur moyen
de vous mettre en contact rapidement avec la personne la plus apte à vous
répondre.

Bref, pas d'inquiétude : ceci n'est qu'un accusé de réception, y a de vrais
gens (souvent des bénévoles de notre association) qui arrivent pour discuter
avec vous !

Votre "ticket" a reçu l'identifiant [Demande perso pour Luc #31185].
Veuillez inclure cet identifiant : [Demande perso pour Luc #31185] dans le
sujet de tout futur message relatif à ce sujet.

Il n'y a pas besoin de répondre à ce message pour l'instant.

Merci,
rt+luc@framasoft.org

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

test


------------=_1564066005-24293-3
Content-Type: text/html; charset="iso-8859-1"
X-RT-Original-Encoding: utf-8

<p>Bonjour,</p>

<p>Ceci est un email automatique en réponse à votre demande concernant : <b>test</b>,
dont un résumé apparaît ci-dessous.</p>

<p>Oui, à Framasoft, on est comme vous : on n'aime pas les réponses automatiques. Nous utilisons juste un système de tickets parce que c'est le meilleur moyen de vous mettre en contact rapidement avec la personne la plus apte à vous répondre.</p>

<p>Bref, pas d'inquiétude : ceci n'est qu'un accusé de réception, y a de vrais gens (souvent des bénévoles de notre association) qui arrivent pour discuter avec vous !</p>

<p>Votre "ticket" a reçu l'identifiant <b>[Demande perso pour Luc #31185]</b>.<br>
Veuillez inclure cet identifiant : <b>[Demande perso pour Luc #31185]</b>
dans le sujet de tout futur message relatif à ce sujet.</p>

<p>Il n'y a pas besoin de répondre à ce message pour l'instant.</p>

<p>Merci,<br/>
rt+luc@framasoft.org</p>

<hr/>
<pre style="white-space: pre-wrap; font-family: monospace;">test
</pre>
------------=_1564066005-24293-3--

The autoreply is correctly encoded in DB (when I select it, it prints without encoding errors).

Does anybody have an idea on how to fix that, please? I tried for hours, but found nothing. It’s driving me nuts.

Regards,

1 Like

I’m having the same issue. Any solution with that?

I don’t think you have the content badly encoded, ldidry.

Just that you are viewing a utf-8 file with a program that is treating it as if it was iso-8859-1. I can’t be sure, since I don’t know the way it was copied here (it could be a double-encoding), but the fact that both your mail client and viewing the db contents show it properly makes me think that’s the case.

As s simple way to verify it, take the “wrongly encoded” file, open it with an hex editor and see which bytes were used for e.g. the “réponse” text. If they were “72 c3 a9 70 6f 6e 73 65”, the email itself is fine, and you should look for the DKIM breakage somewhere else.

Also note that it is extremely rare that the generated output breaks DKIM, since DKIM signing is performed by your MTA after it is sent by RT.

The only questionable thing I see here is that your email contains 8bit body data instead of encoding it, so all the mail agents should support 8BITMIME (and if a remote MTA was “helpfully”, restructuring the mail to use another transfer encoding, that would break the DKIM signature,).