classification
Title: EmailMessage Object Creation
Type: behavior Stage: resolved
Components: email Versions: Python 3.6
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Michael Salsone, barry, r.david.murray
Priority: normal Keywords:

Created on 2017-06-19 21:19 by Michael Salsone, last changed 2017-06-26 23:38 by r.david.murray. This issue is now closed.

Messages (3)
msg296378 - (view) Author: Michael Salsone (Michael Salsone) Date: 2017-06-19 21:19
I am looking to use the get_body() method from email.message.EmailMessage, but I am unable to create an object of this type. I am attempting to create an object of this from message_from_string (as well as some of the other email.parser methods). But it won't allow me to change the class to email.message.EmailMessage. I've read through the source for some this code, but I don't see where you thread the needle here, is it even possible?

Example:
from email.message import EmailMessage
email.message_from_string(self.original_message, _class=EmailMessage)
msg296389 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2017-06-19 23:20
This isn't the place to get help on Python coding, but since this is a relatively new API I'll give you a pointer, and then you can tell me where the docs need improvement :)

If you do message_from_string(yourstring, policy=email.policy.default), you should get back an EmailMessage object.

That said, your example does return an EmailMessage for me, if I put a string in place of your self.original_message, which must be something from your program.  However, that won't *work* very well, because the policy is set to Compat32, and Compat32 and EmailMessage aren't designed to work together.  A lot of things will work fine, but you'll get some weird errors.  However, I don't think that that is worth a documentation note, since it is a logical consequence of using _class=EmailMessage without specifying a policy.
msg296974 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2017-06-26 23:38
Given that there hasn't been any response that would help us improve the docs on this, I'm going to close the issue.  I hope you've figured out how to do what you want to do!
History
Date User Action Args
2017-06-26 23:38:54r.david.murraysetstatus: open -> closed
resolution: not a bug
messages: + msg296974

stage: resolved
2017-06-19 23:20:33r.david.murraysetmessages: + msg296389
2017-06-19 21:19:46Michael Salsonecreate