Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Takes an email from stdin and uploads to matrix.
import email
import json
import sys
import random
from matrix_client.api import MatrixHttpApi
html_template = (
"<b>From</b>: %(from)s\n"
"<b>To</b>: %(to)s\n"
"<b>Subject</b>: %(subject)s\n"
"\n"
"%(body)s\n"
)
text_template = (
"From: %(from)s\n"
"To: %(to)s\n"
"Subject: %(subject)s\n"
"\n"
"%(body)s\n"
)
def main(room_id, token):
msg = email.message_from_file(sys.stdin)
info = {
"from": email.utils.parseaddr(msg["From"])[1],
"to": email.utils.parseaddr(msg["To"])[1],
"subject": msg["subject"],
}
for part in msg.walk():
if part.get_content_type() == "text/html":
info["html"] = part.get_payload()
elif part.get_content_type() == "text/plain":
info["text"] = part.get_payload()
if "text" not in info:
# FIXME
return
text_body = text_template % {
"from": info["from"],
"to": info["to"],
"subject": info["subject"],
"body": info["text"],
}
html_body = html_template % {
"from": info["from"],
"to": info["to"],
"subject": info["subject"],
"body": info["text"] # info["html"] if "html" in info else info["text"],
}
content = {
"body": text_body,
"msgtype": "m.text",
"format": "org.matrix.custom.html",
"formatted_body": html_body,
}
api = MatrixHttpApi("https://jki.re", token=token)
resp = api.send_message_event(
room_id, "m.room.message", content=content, txn_id=str(random.random())
)
if __name__ == "__main__":
main(<room_id>, <access_token>)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.