2016-05-30 14:58:55 +00:00
|
|
|
from django.core.mail import EmailMultiAlternatives
|
|
|
|
from django.conf import settings
|
|
|
|
from django.template.loader import render_to_string
|
2017-03-23 17:32:13 +00:00
|
|
|
import logging
|
|
|
|
logger = logging.getLogger("bornhack.%s" % __name__)
|
|
|
|
|
2016-05-30 14:58:55 +00:00
|
|
|
|
|
|
|
|
2016-05-30 21:13:52 +00:00
|
|
|
def send_email(emailtype, recipient, formatdict, subject, sender='BornHack <info@bornhack.dk>', attachment=None):
|
2016-05-30 14:58:55 +00:00
|
|
|
### determine email type, set template and attachment vars
|
|
|
|
html_template=None
|
|
|
|
|
|
|
|
if emailtype == 'invoice':
|
2016-05-30 16:09:21 +00:00
|
|
|
text_template = 'emails/invoice_email.txt'
|
|
|
|
html_template = 'emails/invoice_email.html'
|
|
|
|
attachment_filename = formatdict['filename']
|
2016-06-22 07:30:03 +00:00
|
|
|
elif emailtype == 'creditnote':
|
2016-06-18 18:51:53 +00:00
|
|
|
text_template = 'emails/creditnote_email.txt'
|
|
|
|
html_template = 'emails/creditnote_email.html'
|
|
|
|
attachment_filename = formatdict['creditnote'].filename
|
2016-05-30 14:58:55 +00:00
|
|
|
elif emailtype == 'testmail':
|
|
|
|
text_template = 'emails/testmail.txt'
|
|
|
|
else:
|
2017-03-23 17:32:13 +00:00
|
|
|
logger.error('Unknown email type: %s' % emailtype)
|
2016-05-30 14:58:55 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
try:
|
|
|
|
### put the basic email together
|
|
|
|
msg = EmailMultiAlternatives(subject, render_to_string(text_template, formatdict), sender, [recipient], [settings.ARCHIVE_EMAIL])
|
2017-03-23 17:32:13 +00:00
|
|
|
|
2016-05-30 14:58:55 +00:00
|
|
|
### is there a html version of this email?
|
|
|
|
if html_template:
|
|
|
|
msg.attach_alternative(render_to_string(html_template, formatdict), 'text/html')
|
|
|
|
|
2016-05-30 16:09:21 +00:00
|
|
|
### is there a pdf attachment to this mail?
|
2016-05-30 14:58:55 +00:00
|
|
|
if attachment:
|
|
|
|
msg.attach(attachment_filename, attachment, 'application/pdf')
|
|
|
|
|
|
|
|
except Exception as E:
|
2017-03-23 17:32:13 +00:00
|
|
|
logger.exception('exception while rendering email: %s' % E)
|
2016-05-30 14:58:55 +00:00
|
|
|
return False
|
2017-03-23 17:32:13 +00:00
|
|
|
|
2016-05-30 14:58:55 +00:00
|
|
|
### send the email
|
|
|
|
msg.send()
|
|
|
|
|
|
|
|
### all good
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2016-06-18 18:51:53 +00:00
|
|
|
def send_creditnote_email(creditnote):
|
|
|
|
# put formatdict together
|
|
|
|
formatdict = {
|
|
|
|
'creditnote': creditnote,
|
|
|
|
}
|
|
|
|
|
|
|
|
subject = 'BornHack creditnote %s' % creditnote.pk
|
|
|
|
|
|
|
|
# send mail
|
|
|
|
return send_email(
|
|
|
|
emailtype='creditnote',
|
|
|
|
recipient=creditnote.user.email,
|
|
|
|
formatdict=formatdict,
|
|
|
|
subject=subject,
|
|
|
|
sender='info@bornhack.dk',
|
|
|
|
attachment=creditnote.pdf.read(),
|
|
|
|
)
|
|
|
|
|
2016-05-30 18:30:52 +00:00
|
|
|
def send_invoice_email(invoice):
|
2016-05-30 14:58:55 +00:00
|
|
|
# put formatdict together
|
|
|
|
formatdict = {
|
2016-05-30 16:09:21 +00:00
|
|
|
'ordernumber': invoice.order.pk,
|
|
|
|
'invoicenumber': invoice.pk,
|
|
|
|
'filename': invoice.filename,
|
2016-05-30 14:58:55 +00:00
|
|
|
}
|
|
|
|
|
2016-05-30 16:09:21 +00:00
|
|
|
subject = 'BornHack invoice %s' % invoice.pk
|
2016-05-30 14:58:55 +00:00
|
|
|
|
|
|
|
# send mail
|
|
|
|
return send_email(
|
|
|
|
emailtype='invoice',
|
2016-05-30 16:09:21 +00:00
|
|
|
recipient=invoice.order.user.email,
|
2016-05-30 14:58:55 +00:00
|
|
|
formatdict=formatdict,
|
|
|
|
subject=subject,
|
2016-05-30 21:13:52 +00:00
|
|
|
sender='info@bornhack.dk',
|
2016-05-30 18:50:39 +00:00
|
|
|
attachment=invoice.pdf.read(),
|
2016-05-30 14:58:55 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def send_test_email(recipient):
|
|
|
|
return send_email(
|
|
|
|
emailtype='testmail',
|
|
|
|
recipient=recipient,
|
|
|
|
subject='testmail from bornhack website',
|
|
|
|
)
|
|
|
|
|