Decoding Email Header and Contents

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.

Project: Self Hosted Zapier Alternative

I am currently in the process of working out how I might replace some of my Zapier services. Why would I want to do this?

  1. I use the free tier. (The process only runs once every 15 minutes. It used to be once every 5 mins.) Which means it has become a bit slow in sending the notifications.
  2. I want to learn how to use Docker

Once I get a bit further along I will hopefully publish the python code to GitHub and document some of the issues and successes as I move along. Check the Projects page section if you are interested.

[Read More]