KostyaEsmukov/smtp_to_telegram

Bad Request: strings must be encoded in UTF-8

delta-whiplash opened this issue · 6 comments

I wish use this script to send notification from my qbitttorent-nox docker to my smartphone when my download finished but I get this error:

(N) 2021-11-20T20:10:01 - Torrent : Magie image par image, envoi de l'email de notification
(C) 2021-11-20T20:10:01 - Erreur de notification e-mail : Message was rejected by the server, error: 554 Error: Non-200 response from Telegram: (400) {"ok":false,"error_code":400,"description":"Bad Request: strings must be encoded in UTF-8"}

it still working on Synology email notifcation service

Please attach the envelope your service is sending to smtp_to_telegram so I could reproduce the issue, thanks.

I don't know how to attach it but you Can just create a qbittorent docker container. And settings UP the email notification server on SMTP to telegram ( you need to be at the 25 port)
If you download any torrent you will not receive any notification but if you go into the log file in the config volume of the qbittorent container you have the utf-8 error

I'm sorry but I am not willing to do that for you.

If you want me to look at this issue please attach the envelope.

You can collect it using tcpdump or a simple smtp server: https://www.djangosnippets.org/snippets/96/

I used this smtpd server

import smtpd
import asyncore

server = smtpd.DebuggingServer(('0.0.0.0', 25), None)

asyncore.loop()

and I got

---------- MESSAGE FOLLOWS ----------
b'Date: Sat, 27 Nov 2021 17:31:21 +0100'
b'From: qBittorrent_notification@example.com'
b"Subject: [qBittorrent] 'Anna-V\xe9ronique El Baze - Je maudis le jour (2019)' a termin\xe9"
b' le t\xe9l\xe9chargement'
b'To: chamber@deltabots.com'
b'MIME-Version: 1.0'
b'Content-Type: text/plain; charset=UTF-8'
b'Content-Transfer-Encoding: base64'
b'X-Peer: 172.16.30.2'
b''
b'Tm9tIGR1IFRvcnJlbnQgOiBBbm5hLVbDqXJvbmlxdWUgRWwgQmF6ZSAtIEplIG1hdWRpcyBsZSBqb3VyICgyMDE5KQ0KVGFpbGxlIGR1IFRvcnJlbnQgOiAyNDguNCBLaW8NCkNoZW1pbiBkZSBzYXV2ZWdhcmRlIDogL2Rvd25sb2Fkcw0KDQpMZSBUb3JyZW50IGEgw6l0w6kgdMOpbMOpY2hhcmfDqSBkYW5zIDwgMW1pbi4NCg0KDQpNZXJjaSBkJ3V0aWxpc2VyIHFCaXR0b3JyZW50Lg0K'
------------ END MESSAGE ------------

Thanks!

Well, the Subject header does indeed contain non-utf8 sequences:

>>> s = b"Subject: [qBittorrent] 'Anna-V\xe9ronique El Baze - Je maudis le jour (2019)' a termin\xe9" b' le t\xe9l\xe9chargement'
>>> s.decode("utf8")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe9 in position 30: invalid continuation byte
>>> hex(s[30])
'0xe9'

Apparently it is encoded in latin1/iso-8859-1:

>>> s.decode("latin1")
"Subject: [qBittorrent] 'Anna-Véronique El Baze - Je maudis le jour (2019)' a terminé le téléchargement"

I'm not sure if this is something that should be handled on the smtp_to_telegram side. Normally mail headers should either contain ascii-only bytes or be encoded via something like =?ISO-8859-1?Q?Anna-V=E9ronique?=. So I would say it's the sender (your qbitttorent-nox application) that should be fixed, because assuming a specific non-ascii encoding in smtp_to_telegram is unsafe and might break other kinds of emails.

If fixing the application is not an option, I would suggest to look at exim4, which is quite configurable and might let you do the charset normalization.

I have added a test ensuring that properly encoded latin1 Subject and body are correctly sent to Telegram as utf8 strings in 3769aa8, but that won't fix your issue, because in your case the Subject is not encoded as it should be.

Ok thank you