Index: Lib/email/test/test_email.py =================================================================== --- Lib/email/test/test_email.py (revision 77712) +++ Lib/email/test/test_email.py (working copy) @@ -103,6 +103,11 @@ eq(msg['content-type'], 'text/x-weird; charset="iso-8859-1"') eq(msg['content-transfer-encoding'], 'quinted-puntable') + def test_set_charset_non_text_maintype(self): + msg = MIMEMultipart() + msg.attach(MIMEText('foo', 'plain')) + self.assertRaises(TypeError, msg.set_charset, 'utf-8') + def test_set_charset_from_string(self): eq = self.assertEqual msg = Message() Index: Lib/email/message.py =================================================================== --- Lib/email/message.py (revision 77712) +++ Lib/email/message.py (working copy) @@ -238,6 +238,9 @@ Content-Type, Content-Transfer-Encoding) will be added as needed. """ + if self.get_content_maintype() != 'text': + raise TypeError( + "set_charset() called on a message not of type text/*.") if charset is None: self.del_param('charset') self._charset = None