2015-07-26 20:34:46 +00:00
|
|
|
|
2016-03-27 20:38:14 +00:00
|
|
|
import click
|
2015-07-26 20:34:46 +00:00
|
|
|
import os
|
|
|
|
import smtplib
|
2016-03-27 20:38:14 +00:00
|
|
|
from certidude.user import User
|
2016-03-21 21:42:39 +00:00
|
|
|
from markdown import markdown
|
2015-07-26 20:34:46 +00:00
|
|
|
from jinja2 import Environment, PackageLoader
|
|
|
|
from email.mime.multipart import MIMEMultipart
|
|
|
|
from email.mime.text import MIMEText
|
2016-03-21 21:42:39 +00:00
|
|
|
from email.mime.base import MIMEBase
|
2018-05-16 13:10:26 +00:00
|
|
|
from email.header import Header
|
2017-12-30 13:57:48 +00:00
|
|
|
from urllib.parse import urlparse
|
2015-07-26 20:34:46 +00:00
|
|
|
|
2016-03-21 21:42:39 +00:00
|
|
|
env = Environment(loader=PackageLoader("certidude", "templates/mail"))
|
|
|
|
|
2017-12-30 13:57:48 +00:00
|
|
|
def send(template, to=None, secondary=None, include_admins=True, attachments=(), **context):
|
2016-03-21 21:42:39 +00:00
|
|
|
from certidude import authority, config
|
|
|
|
|
2017-04-21 21:22:08 +00:00
|
|
|
recipients = ()
|
|
|
|
if include_admins:
|
|
|
|
recipients = tuple(User.objects.filter_admins())
|
2016-03-27 20:38:14 +00:00
|
|
|
if to:
|
2017-04-21 21:22:08 +00:00
|
|
|
recipients = (to,) + recipients
|
2017-12-30 13:57:48 +00:00
|
|
|
if secondary:
|
|
|
|
recipients = (secondary,) + recipients
|
|
|
|
|
2016-03-27 20:38:14 +00:00
|
|
|
|
|
|
|
click.echo("Sending e-mail %s to %s" % (template, recipients))
|
2016-03-21 21:42:39 +00:00
|
|
|
|
|
|
|
subject, text = env.get_template(template).render(context).split("\n\n", 1)
|
|
|
|
html = markdown(text)
|
|
|
|
|
|
|
|
msg = MIMEMultipart("alternative")
|
2018-05-16 13:10:26 +00:00
|
|
|
msg["Subject"] = Header(subject)
|
|
|
|
msg["From"] = Header(config.MAILER_NAME)
|
|
|
|
msg["From"].append("<%s>" % config.MAILER_ADDRESS)
|
|
|
|
|
|
|
|
msg["To"] = Header()
|
|
|
|
for user in recipients:
|
|
|
|
if isinstance(user, User):
|
|
|
|
full_name, user = user.format()
|
|
|
|
if full_name:
|
|
|
|
msg["To"].append(full_name)
|
|
|
|
msg["To"].append(user)
|
|
|
|
msg["To"].append(", ")
|
2016-03-21 21:42:39 +00:00
|
|
|
|
2017-12-30 13:57:48 +00:00
|
|
|
part1 = MIMEText(text, "plain", "utf-8")
|
|
|
|
part2 = MIMEText(html, "html", "utf-8")
|
2016-03-21 21:42:39 +00:00
|
|
|
|
|
|
|
msg.attach(part1)
|
|
|
|
msg.attach(part2)
|
|
|
|
|
2017-03-13 11:42:58 +00:00
|
|
|
for attachment, content_type, suggested_filename in attachments:
|
|
|
|
part = MIMEBase(*content_type.split("/"))
|
|
|
|
part.add_header('Content-Disposition', 'attachment', filename=suggested_filename)
|
|
|
|
part.set_payload(attachment)
|
2016-03-21 21:42:39 +00:00
|
|
|
msg.attach(part)
|
|
|
|
|
2017-07-05 21:22:02 +00:00
|
|
|
if config.MAILER_ADDRESS:
|
|
|
|
click.echo("Sending to: %s" % msg["to"])
|
|
|
|
conn = smtplib.SMTP("localhost")
|
2018-05-16 13:10:26 +00:00
|
|
|
conn.sendmail(config.MAILER_ADDRESS, [u.mail if isinstance(u, User) else u for u in recipients], msg.as_string())
|