2017-10-28 00:44:19 +02:00
|
|
|
import logging
|
|
|
|
|
2017-10-25 01:54:43 +02:00
|
|
|
from typing import List
|
2018-05-09 20:57:01 +02:00
|
|
|
import configparser
|
2017-10-25 01:58:05 +02:00
|
|
|
|
|
|
|
import smtplib
|
|
|
|
from email.mime.multipart import MIMEMultipart
|
|
|
|
from email.mime.text import MIMEText
|
2017-10-25 01:54:43 +02:00
|
|
|
|
|
|
|
from django.conf import settings
|
|
|
|
from django.core.mail.backends.base import BaseEmailBackend
|
|
|
|
from django.core.mail import EmailMultiAlternatives
|
|
|
|
from django.template import loader
|
|
|
|
|
2017-11-27 14:35:36 +01:00
|
|
|
def get_forward_address() -> str:
|
2017-10-25 01:58:05 +02:00
|
|
|
config = configparser.ConfigParser()
|
|
|
|
config.read(settings.FORWARD_ADDRESS_CONFIG_FILE)
|
|
|
|
try:
|
|
|
|
return config.get("DEV_EMAIL", "forward_address")
|
2018-05-24 16:41:34 +02:00
|
|
|
except (configparser.NoSectionError, configparser.NoOptionError):
|
2017-10-25 01:58:05 +02:00
|
|
|
return ""
|
|
|
|
|
2017-11-27 14:35:36 +01:00
|
|
|
def set_forward_address(forward_address: str) -> None:
|
2017-10-25 01:58:05 +02:00
|
|
|
config = configparser.ConfigParser()
|
|
|
|
config.read(settings.FORWARD_ADDRESS_CONFIG_FILE)
|
|
|
|
|
|
|
|
if not config.has_section("DEV_EMAIL"):
|
|
|
|
config.add_section("DEV_EMAIL")
|
|
|
|
config.set("DEV_EMAIL", "forward_address", forward_address)
|
|
|
|
|
|
|
|
with open(settings.FORWARD_ADDRESS_CONFIG_FILE, "w") as cfgfile:
|
2019-01-31 14:32:37 +01:00
|
|
|
config.write(cfgfile)
|
2017-10-25 01:58:05 +02:00
|
|
|
|
2017-10-25 01:54:43 +02:00
|
|
|
class EmailLogBackEnd(BaseEmailBackend):
|
2017-11-27 14:35:36 +01:00
|
|
|
def send_email_smtp(self, email: EmailMultiAlternatives) -> None:
|
2017-10-25 01:58:05 +02:00
|
|
|
from_email = email.from_email
|
|
|
|
to = get_forward_address()
|
|
|
|
|
|
|
|
msg = MIMEMultipart('alternative')
|
|
|
|
msg['Subject'] = email.subject
|
|
|
|
msg['From'] = from_email
|
|
|
|
msg['To'] = to
|
|
|
|
|
|
|
|
text = email.body
|
|
|
|
html = email.alternatives[0][0]
|
|
|
|
|
|
|
|
# Here, we replace the email addresses used in development
|
|
|
|
# with chat.zulip.org, so that web email providers like Gmail
|
|
|
|
# will be able to fetch the illustrations used in the emails.
|
|
|
|
localhost_email_images_base_uri = settings.ROOT_DOMAIN_URI + '/static/images/emails'
|
|
|
|
czo_email_images_base_uri = 'https://chat.zulip.org/static/images/emails'
|
|
|
|
html = html.replace(localhost_email_images_base_uri, czo_email_images_base_uri)
|
|
|
|
|
|
|
|
msg.attach(MIMEText(text, 'plain'))
|
|
|
|
msg.attach(MIMEText(html, 'html'))
|
|
|
|
|
|
|
|
smtp = smtplib.SMTP(settings.EMAIL_HOST)
|
|
|
|
smtp.starttls()
|
|
|
|
smtp.login(settings.EMAIL_HOST_USER, settings.EMAIL_HOST_PASSWORD)
|
|
|
|
smtp.sendmail(from_email, to, msg.as_string())
|
|
|
|
smtp.quit()
|
|
|
|
|
2017-10-27 13:03:15 +02:00
|
|
|
def log_email(self, email: EmailMultiAlternatives) -> None:
|
2017-10-25 01:54:43 +02:00
|
|
|
"""Used in development to record sent emails in a nice HTML log"""
|
|
|
|
html_message = 'Missing HTML message'
|
|
|
|
if len(email.alternatives) > 0:
|
|
|
|
html_message = email.alternatives[0][0]
|
|
|
|
|
|
|
|
context = {
|
|
|
|
'subject': email.subject,
|
|
|
|
'from_email': email.from_email,
|
|
|
|
'recipients': email.to,
|
|
|
|
'body': email.body,
|
|
|
|
'html_message': html_message
|
|
|
|
}
|
|
|
|
|
|
|
|
new_email = loader.render_to_string('zerver/email.html', context)
|
|
|
|
|
|
|
|
# Read in the pre-existing log, so that we can add the new entry
|
|
|
|
# at the top.
|
|
|
|
try:
|
2020-04-09 21:51:58 +02:00
|
|
|
with open(settings.EMAIL_CONTENT_LOG_PATH) as f:
|
2017-10-25 01:54:43 +02:00
|
|
|
previous_emails = f.read()
|
|
|
|
except FileNotFoundError:
|
|
|
|
previous_emails = ""
|
|
|
|
|
|
|
|
with open(settings.EMAIL_CONTENT_LOG_PATH, "w+") as f:
|
|
|
|
f.write(new_email + previous_emails)
|
|
|
|
|
2017-10-27 13:03:15 +02:00
|
|
|
def send_messages(self, email_messages: List[EmailMultiAlternatives]) -> int:
|
2017-10-25 01:54:43 +02:00
|
|
|
for email in email_messages:
|
2017-10-25 01:58:05 +02:00
|
|
|
if get_forward_address():
|
|
|
|
self.send_email_smtp(email)
|
2018-08-06 11:40:31 +02:00
|
|
|
if settings.DEVELOPMENT_LOG_EMAILS:
|
|
|
|
self.log_email(email)
|
|
|
|
email_log_url = settings.ROOT_DOMAIN_URI + "/emails"
|
2020-05-02 08:44:14 +02:00
|
|
|
logging.info("Emails sent in development are available at %s", email_log_url)
|
2017-10-25 01:54:43 +02:00
|
|
|
return len(email_messages)
|