This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import email.parser | |
from email import policy | |
def openFromFile(fp) -> email.message.EmailMessage: | |
""" | |
Read in the open file pointer | |
:param fp: file pointer to open file. | |
:type fp: file pointer | |
:returns: msg which is an email.message.EmailMessage | |
:rtype: email.message.EmailMessage | |
""" | |
msg = email.parser.Parser(policy=policy.default) | |
return msg.parse(fp) | |
def openFromStr(string) -> email.message.EmailMessage: | |
""" | |
Read in the email from string. | |
:param string: | |
:type string: str | |
:returns: msg which is an email.message.EmailMessage | |
:rtype: email.message.EmailMessage | |
""" | |
msg = email.parser.Parser(policy=policy.default) | |
return msg.parsestr(string) |
Using email.parser
and email.policy
along with policy.default
means that
you do not need to worry about character encoding as the modules will take care
of it automatically.
I forget where I saw it but I believe that policy.default will become the real
default in the future.
Current policy.compat32
is the module default.