Skip to content

Commit

Permalink
Fix charset handling in MailSender #5096 (#5118)
Browse files Browse the repository at this point in the history
  • Loading branch information
mmitropoulou committed Sep 7, 2023
1 parent 00527fd commit 721df89
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 5 deletions.
7 changes: 3 additions & 4 deletions scrapy/mail.py
Expand Up @@ -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("/"))
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_mail.py
Expand Up @@ -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"')

Expand Down

0 comments on commit 721df89

Please sign in to comment.