File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -203,7 +203,11 @@ def attach(self, payload):
203203 if self ._payload is None :
204204 self ._payload = [payload ]
205205 else :
206- self ._payload .append (payload )
206+ try :
207+ self ._payload .append (payload )
208+ except AttributeError :
209+ raise TypeError ("Attach is not valid on a message with a"
210+ " non-multipart payload" )
207211
208212 def get_payload (self , i = None , decode = False ):
209213 """Return a reference to the payload.
Original file line number Diff line number Diff line change @@ -124,6 +124,14 @@ def test_set_payload_to_list(self):
124124 msg .set_payload ([])
125125 self .assertEqual (msg .get_payload (), [])
126126
127+ def test_attach_when_payload_is_string (self ):
128+ msg = Message ()
129+ msg ['Content-Type' ] = 'multipart/mixed'
130+ msg .set_payload ('string payload' )
131+ sub_msg = MIMEMessage (Message ())
132+ self .assertRaisesRegex (TypeError , "[Aa]ttach.*non-multipart" ,
133+ msg .attach , sub_msg )
134+
127135 def test_get_charsets (self ):
128136 eq = self .assertEqual
129137
Original file line number Diff line number Diff line change @@ -1188,6 +1188,7 @@ Daniel Shahaf
11881188Ha Shao
11891189Mark Shannon
11901190Richard Shapiro
1191+ Varun Sharma
11911192Vlad Shcherbina
11921193Justin Sheehy
11931194Charlie Shepherd
Original file line number Diff line number Diff line change @@ -20,6 +20,10 @@ Core and Builtins
2020Library
2121-------
2222
23+ - Issue #11558: ``email.message.Message.attach`` now returns a more
24+ useful error message if ``attach`` is called on a message for which
25+ ``is_multipart`` is False.
26+
2327- Issue #20283: RE pattern methods now accept the string keyword parameters
2428 as documented. The pattern and source keyword parameters are left as
2529 deprecated aliases.
You can’t perform that action at this time.
0 commit comments