diff --git a/Doc/library/email.message.rst b/Doc/library/email.message.rst index 58f708c..790090b 100644 --- a/Doc/library/email.message.rst +++ b/Doc/library/email.message.rst @@ -574,6 +574,16 @@ Here are the methods of the :class:`Message` class: will be *failobj*. + .. method:: get_content_disposition() + + Return :mailheader:`Content-Disposition` for the represented subpart if + a :mailheader:`Content-Disposition` header exists or return ``None`` if + it does not. The return values for this method are *inline*, *attachment* + or ``None`` according to :rfc:`2183`. All values are returned in lower + case. + + .. versionadded:: 3.5 + .. method:: walk() The :meth:`walk` method is an all-purpose generator which can be used to diff --git a/Lib/email/message.py b/Lib/email/message.py index aa46deb..819bd6f 100644 --- a/Lib/email/message.py +++ b/Lib/email/message.py @@ -926,6 +926,19 @@ class Message: """ return [part.get_content_charset(failobj) for part in self.walk()] + def get_content_disposition(self): + """Return the message's content-disposition if it exists else + return None. + + The return values can be either 'inline', 'attachment' or None + according to the rfc2183. + """ + value = self.get('content-disposition') + if value is None: + return None + c_d = _splitparam(value)[0].lower() + return c_d + # I.e. def walk(self): ... from email.iterators import walk diff --git a/Lib/test/test_email/test_email.py b/Lib/test/test_email/test_email.py index 2f89a10..3d6692f 100644 --- a/Lib/test/test_email/test_email.py +++ b/Lib/test/test_email/test_email.py @@ -585,6 +585,16 @@ class TestMessageAPI(TestEmailBase): eq(msg.values(), ['One Hundred', 'Twenty', 'Three', 'Eleven']) self.assertRaises(KeyError, msg.replace_header, 'Fourth', 'Missing') + def test_get_content_disposition(self): + msg = Message() + self.assertIsNone(msg.get_content_disposition()) + msg.add_header('Content-Disposition', 'attachment', filename='random.avi') + self.assertEqual(msg.get_content_disposition(),'attachment') + msg.replace_header('Content-Disposition', 'inline') + self.assertEqual(msg.get_content_disposition(), 'inline') + msg.replace_header('Content-Disposition', 'InlinE') + self.asserEqual(msg.get_content_disposition(), 'inline') + # test_defect_handling:test_invalid_chars_in_base64_payload def test_broken_base64_payload(self): x = 'AwDp0P7//y6LwKEAcPa/6Q=9'