import email
import email.charset
-import email.mime.multipart
import email.mime.text
import email.policy
+import email.utils
import logging
-import markdown
+import re
import smtplib
import socket
import tornado.locale
import tornado.template
from . import base
-from . import users
from .constants import TEMPLATESDIR
from .decorators import *
class Messages(base.Object):
def init(self):
- self.templates = tornado.template.Loader(TEMPLATESDIR)
+ self.template_loader = tornado.template.Loader(TEMPLATESDIR)
@lazy_property
def queue(self):
return Queue(self.backend)
def send(self, message, priority=None):
+ # Check if To is set
+ if not "To" in message:
+ raise ValueError("Message has no To: header")
+
# Add a message ID if non existant
if not "Message-ID" in message:
message.add_header("Message-ID", self.make_msgid())
# Render the message
try:
- message_part = t.generate(**namespace)
+ body = t.generate(**namespace)
# Reset the rendered template when it could not be rendered
except:
raise
# Parse the message and extract the header
- body = email.message_from_string(message_part.decode(), policy=policy)
+ body = email.message_from_string(body.decode(), policy=policy)
- for header in message_part:
+ for header in body:
value = body[header]
# Make sure addresses are properly encoded
payload = body.get_payload()
# Replace any multiple chains of newlines with only one newline
- if mimetype == "plain":
- payload = re.sub(r"\n{2,}", "\n\n", payload)
+ payload = re.sub(r"\n{2,}", "\n\n", payload)
# Create a MIMEText object out of it
- body = email.mime.text.MIMEText(payload, mimetype)
+ body = email.mime.text.MIMEText(payload, "text")
# Collect all parts of this message
message_parts = [