When debugging, don't send emails

This commit is contained in:
Yingtong Li 2019-06-20 01:05:49 +10:00
parent 1afc3f3db7
commit a0d6164d95
Signed by: RunasSudo
GPG Key ID: 7234E476BF21C61A
2 changed files with 30 additions and 18 deletions

View File

@ -23,6 +23,7 @@ SECRET_KEY = None # IMPORTANT: Set this to a secret string
SECRET_KEY_MEMBERSIG = None # IMPORTANT: Set this to a secret string
DEBUG = True
EMAIL_DEBUG = False
ALLOWED_HOSTS = []

View File

@ -24,6 +24,11 @@ from jinja2 import Markup
import markdown
# Debugging
import subprocess
import tempfile
import time
class Emailer:
def __init__(self):
self.client = boto3.client('ses', aws_access_key_id=settings.AWS_KEY_ID, aws_secret_access_key=settings.AWS_SECRET, region_name=settings.AWS_REGION)
@ -47,28 +52,34 @@ class Emailer:
raise Exception('Reached maximum number of retries')
def send_raw_mail(self, recipients, subject, content_txt, content_html):
self.boto3_send(
Destination={
'ToAddresses': recipients,
},
Message={
'Body': {
'Html': {
'Charset': 'utf-8',
'Data': content_html,
if settings.EMAIL_DEBUG:
with tempfile.NamedTemporaryFile(mode='w', encoding='utf-8', suffix='.eml') as f:
print('Subject:' + subject + '\nContent-Type: multipart/alternative; boundary=boundary\n\n--boundary\nContent-Type: text/html; charset=utf-8\n\n' + content_html + '\n--boundary\nContent-Type: text/plain; charset=utf-8\n\n' + content_txt + '\n--boundary', file=f)
subprocess.run(['thunderbird', f.name])
time.sleep(0.5)
else:
self.boto3_send(
Destination={
'ToAddresses': recipients,
},
Message={
'Body': {
'Html': {
'Charset': 'utf-8',
'Data': content_html,
},
'Text': {
'Charset': 'utf-8',
'Data': content_txt,
},
},
'Text': {
'Subject': {
'Charset': 'utf-8',
'Data': content_txt,
'Data': subject,
},
},
'Subject': {
'Charset': 'utf-8',
'Data': subject,
},
},
Source='{} <{}>'.format(settings.ORG_NAME, settings.AWS_SENDER_EMAIL),
)
Source='{} <{}>'.format(settings.ORG_NAME, settings.AWS_SENDER_EMAIL),
)
def render_mail(self, template_loc, params={}):
params['baseurl'] = 'https://' + settings.ALLOWED_HOSTS[0]