Skip to content

Instantly share code, notes, and snippets.

@Bas-Man
Last active October 15, 2020 11:14
Show Gist options
  • Save Bas-Man/7c7a6c9a7c141033063efc879d5be46e to your computer and use it in GitHub Desktop.
Save Bas-Man/7c7a6c9a7c141033063efc879d5be46e to your computer and use it in GitHub Desktop.
Extract Email Subject Line, Single line only support.
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment