#!/usr/bin/python3
+import base64
import email
import email.mime.multipart
import email.mime.text
import email.utils
import logging
+import mimetypes
+import os.path
import random
import smtplib
import socket
templates_dir = self.backend.config.get("global", "templates_dir")
assert templates_dir
- return tornado.template.Loader(templates_dir, autoescape=None)
+ # Setup namespace
+ namespace = {
+ "embed_image" : self.embed_image,
+ }
+
+ return tornado.template.Loader(templates_dir, namespace=namespace, autoescape=None)
def make_recipient(self, recipient):
# Use the contact instead of the account
if self.backend.debug:
self.template_loader.reset()
+ def embed_image(self, path):
+ static_dir = self.backend.config.get("global", "static_dir")
+ assert static_dir
+
+ # Make the path absolute
+ path = os.path.join(static_dir, path)
+
+ # Fetch the mimetype
+ mimetype, encoding = mimetypes.guess_type(path)
+
+ # Read the file
+ with open(path, "rb") as f:
+ data = f.read()
+
+ # Convert data into base64
+ data = base64.b64encode(data)
+
+ # Return everything
+ return "data:%s;base64,%s" % (mimetype, data.decode())
+
async def send_cli(self, template, recipient):
"""
Send a test message from the CLI