From 721df895f9ea9d8073c13fbd2f75a6fbdc75ffc7 Mon Sep 17 00:00:00 2001 From: Maria Mitropoulou <56678215+mmitropoulou@users.noreply.github.com> Date: Thu, 7 Sep 2023 08:47:42 +0300 Subject: [PATCH] Fix charset handling in MailSender #5096 (#5118) --- scrapy/mail.py | 7 +++---- tests/test_mail.py | 2 +- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/scrapy/mail.py b/scrapy/mail.py index c11f3898d0d..237327451ee 100644 --- a/scrapy/mail.py +++ b/scrapy/mail.py @@ -96,10 +96,9 @@ def send( rcpts.extend(cc) msg["Cc"] = COMMASPACE.join(cc) - if charset: - msg.set_charset(charset) - if attachs: + if charset: + msg.set_charset(charset) msg.attach(MIMEText(body, "plain", charset or "us-ascii")) for attach_name, mimetype, f in attachs: part = MIMEBase(*mimetype.split("/")) @@ -110,7 +109,7 @@ def send( ) msg.attach(part) else: - msg.set_payload(body) + msg.set_payload(body, charset) if _callback: _callback(to=to, subject=subject, body=body, cc=cc, attach=attachs, msg=msg) diff --git a/tests/test_mail.py b/tests/test_mail.py index 504c7848647..2535e58db26 100644 --- a/tests/test_mail.py +++ b/tests/test_mail.py @@ -111,7 +111,7 @@ def test_send_utf8(self): msg = self.catched_msg["msg"] self.assertEqual(msg["subject"], subject) - self.assertEqual(msg.get_payload(), body) + self.assertEqual(msg.get_payload(decode=True).decode("utf-8"), body) self.assertEqual(msg.get_charset(), Charset("utf-8")) self.assertEqual(msg.get("Content-Type"), 'text/plain; charset="utf-8"')