use filefield for email attachments, add logging for emailworker
This commit is contained in:
parent
16eb7cf594
commit
a8eb0ffe97
|
@ -1,4 +1,4 @@
|
||||||
from utils.email import _send_email
|
from utils.email import add_outgoing_email
|
||||||
import logging
|
import logging
|
||||||
logger = logging.getLogger("bornhack.%s" % __name__)
|
logger = logging.getLogger("bornhack.%s" % __name__)
|
||||||
|
|
||||||
|
@ -11,11 +11,11 @@ def send_creditnote_email(creditnote):
|
||||||
|
|
||||||
subject = 'BornHack creditnote %s' % creditnote.pk
|
subject = 'BornHack creditnote %s' % creditnote.pk
|
||||||
|
|
||||||
# send mail
|
# add email to outgoing email queue
|
||||||
return _send_email(
|
return add_outgoing_email(
|
||||||
text_template='emails/creditnote_email.txt',
|
text_template='emails/creditnote_email.txt',
|
||||||
html_template='emails/creditnote_email.html',
|
html_template='emails/creditnote_email.html',
|
||||||
recipient=creditnote.user.email,
|
recipients=creditnote.user.email,
|
||||||
formatdict=formatdict,
|
formatdict=formatdict,
|
||||||
subject=subject,
|
subject=subject,
|
||||||
attachment=creditnote.pdf.read(),
|
attachment=creditnote.pdf.read(),
|
||||||
|
@ -33,11 +33,11 @@ def send_invoice_email(invoice):
|
||||||
|
|
||||||
subject = 'BornHack invoice %s' % invoice.pk
|
subject = 'BornHack invoice %s' % invoice.pk
|
||||||
|
|
||||||
# send mail
|
# add email to outgoing email queue
|
||||||
return _send_email(
|
return add_outgoing_email(
|
||||||
text_template='emails/invoice_email.txt',
|
text_template='emails/invoice_email.txt',
|
||||||
html_template='emails/invoice_email.html',
|
html_template='emails/invoice_email.html',
|
||||||
recipient=invoice.order.user.email,
|
recipients=invoice.order.user.email,
|
||||||
formatdict=formatdict,
|
formatdict=formatdict,
|
||||||
subject=subject,
|
subject=subject,
|
||||||
attachment=invoice.pdf.read(),
|
attachment=invoice.pdf.read(),
|
||||||
|
@ -46,8 +46,8 @@ def send_invoice_email(invoice):
|
||||||
|
|
||||||
|
|
||||||
def send_test_email(recipient):
|
def send_test_email(recipient):
|
||||||
return _send_email(
|
return add_outgoing_email(
|
||||||
text_template='emails/testmail.txt',
|
text_template='emails/testmail.txt',
|
||||||
recipient=recipient,
|
recipients=recipient,
|
||||||
subject='testmail from bornhack website'
|
subject='testmail from bornhack website'
|
||||||
)
|
)
|
||||||
|
|
|
@ -1,7 +1,8 @@
|
||||||
from django.core.mail import EmailMultiAlternatives
|
from django.core.mail import EmailMultiAlternatives
|
||||||
|
from django.core.validators import validate_email
|
||||||
|
from django.core.files.base import ContentFile
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.template.loader import render_to_string
|
from django.template.loader import render_to_string
|
||||||
from django.core.validators import validate_email
|
|
||||||
from .models import OutgoingEmail
|
from .models import OutgoingEmail
|
||||||
import logging
|
import logging
|
||||||
logger = logging.getLogger("bornhack.%s" % __name__)
|
logger = logging.getLogger("bornhack.%s" % __name__)
|
||||||
|
@ -13,7 +14,7 @@ def _send_email(
|
||||||
subject,
|
subject,
|
||||||
html_template='',
|
html_template='',
|
||||||
sender='BornHack <info@bornhack.dk>',
|
sender='BornHack <info@bornhack.dk>',
|
||||||
attachment='',
|
attachment=None,
|
||||||
attachment_filename=''
|
attachment_filename=''
|
||||||
):
|
):
|
||||||
if not isinstance(recipient, list):
|
if not isinstance(recipient, list):
|
||||||
|
@ -39,13 +40,16 @@ def _send_email(
|
||||||
# is there a pdf attachment to this mail?
|
# is there a pdf attachment to this mail?
|
||||||
if attachment:
|
if attachment:
|
||||||
msg.attach(attachment_filename, attachment, 'application/pdf')
|
msg.attach(attachment_filename, attachment, 'application/pdf')
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.exception('exception while rendering email: {}'.format(e))
|
logger.exception('exception while rendering email: {}'.format(e))
|
||||||
return False
|
return False
|
||||||
|
|
||||||
# send the email
|
# send the email
|
||||||
msg.send()
|
try:
|
||||||
|
msg.send(fail_silently=False)
|
||||||
|
except Exception as e:
|
||||||
|
logger.exception('exception while sending email: {}'.format(e))
|
||||||
|
return False
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
@ -57,7 +61,7 @@ def add_outgoing_email(
|
||||||
subject,
|
subject,
|
||||||
html_template='',
|
html_template='',
|
||||||
sender='BornHack <info@bornhack.dk>',
|
sender='BornHack <info@bornhack.dk>',
|
||||||
attachment='',
|
attachment=None,
|
||||||
attachment_filename=''
|
attachment_filename=''
|
||||||
):
|
):
|
||||||
""" adds an email to the outgoing queue
|
""" adds an email to the outgoing queue
|
||||||
|
@ -74,13 +78,17 @@ def add_outgoing_email(
|
||||||
else:
|
else:
|
||||||
validate_email(recipients)
|
validate_email(recipients)
|
||||||
|
|
||||||
OutgoingEmail.objects.create(
|
email = OutgoingEmail.objects.create(
|
||||||
text_template=text_template,
|
text_template=text_template,
|
||||||
html_template=html_template,
|
html_template=html_template,
|
||||||
subject=subject,
|
subject=subject,
|
||||||
sender=sender,
|
sender=sender,
|
||||||
recipient=recipients,
|
recipient=recipients
|
||||||
attachment=attachment,
|
|
||||||
attachment_filename=attachment_filename
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if attachment:
|
||||||
|
django_file = ContentFile(attachment)
|
||||||
|
django_file.name = attachment_filename
|
||||||
|
email.attachment.save(attachment_filename, django_file, save=True)
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
|
@ -76,6 +76,8 @@ class OutgoingEmail(CreatedUpdatedModel):
|
||||||
html_template = models.TextField(blank=True)
|
html_template = models.TextField(blank=True)
|
||||||
recipient = models.CharField(max_length=500)
|
recipient = models.CharField(max_length=500)
|
||||||
sender = models.CharField(max_length=500)
|
sender = models.CharField(max_length=500)
|
||||||
attachment = models.CharField(max_length=500, blank=True)
|
attachment = models.FileField(blank=True)
|
||||||
attachment_filename = models.CharField(max_length=500, blank=True)
|
|
||||||
processed = models.BooleanField(default=False)
|
processed = models.BooleanField(default=False)
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return 'Email {} for {}'.format(self.subject, self.recipient)
|
||||||
|
|
|
@ -12,19 +12,30 @@ def do_work():
|
||||||
"""
|
"""
|
||||||
not_processed_email = OutgoingEmail.objects.filter(processed=False)
|
not_processed_email = OutgoingEmail.objects.filter(processed=False)
|
||||||
|
|
||||||
|
logger.info('about to process {} emails'.format(len(not_processed_email)))
|
||||||
for email in not_processed_email:
|
for email in not_processed_email:
|
||||||
if ',' in email.recipient:
|
if ',' in email.recipient:
|
||||||
recipient = email.recipient.split(',')
|
recipient = email.recipient.split(',')
|
||||||
else:
|
else:
|
||||||
recipient = [email.recipient]
|
recipient = [email.recipient]
|
||||||
|
|
||||||
_send_email(
|
attachment = None
|
||||||
|
attachment_filename = ''
|
||||||
|
if email.attachment:
|
||||||
|
attachment = email.attachment.read()
|
||||||
|
attachment_filename = email.attachment.name
|
||||||
|
|
||||||
|
mail_send_success = _send_email(
|
||||||
text_template=email.text_template,
|
text_template=email.text_template,
|
||||||
recipient=recipient,
|
recipient=recipient,
|
||||||
subject=email.subject,
|
subject=email.subject,
|
||||||
html_template=email.html_template,
|
html_template=email.html_template,
|
||||||
attachment=email.attachment,
|
attachment=attachment,
|
||||||
attachment_filename=email.attachment_filename
|
attachment_filename=attachment_filename
|
||||||
)
|
)
|
||||||
email.processed = True
|
if mail_send_success:
|
||||||
email.save()
|
email.processed = True
|
||||||
|
email.save()
|
||||||
|
logger.info('successfully sent {}'.format(email))
|
||||||
|
else:
|
||||||
|
logger.error('unable to sent {}'.format(email))
|
||||||
|
|
Loading…
Reference in a new issue