|
@@ -7,10 +7,13 @@ import binascii
|
|
|
import base64
|
|
|
import html2text
|
|
|
from datetime import date, timedelta
|
|
|
+
|
|
|
from django.core.mail import EmailMultiAlternatives
|
|
|
-from django.template import TemplateDoesNotExist
|
|
|
from django.core.files.storage import FileSystemStorage
|
|
|
from django.conf import settings
|
|
|
+from django.template.loader import get_template
|
|
|
+from django.template import Context, TemplateDoesNotExist
|
|
|
+from django.contrib.sites.models import Site
|
|
|
|
|
|
|
|
|
# Stockage des fichiers privés (comme les factures par exemple)
|
|
@@ -31,38 +34,42 @@ def ldap_hash(password):
|
|
|
return password
|
|
|
|
|
|
|
|
|
-def send_templated_email(subject, to, template_to_use, context, attachements, from_email=None):
|
|
|
+def send_templated_email(to, subject_template, body_template, context={}, attachements=[]):
|
|
|
"""
|
|
|
Send a multialternative email based on html and optional txt template.
|
|
|
"""
|
|
|
|
|
|
- # Get default sender if not specified
|
|
|
- from_email = from_email if from_email else 'coin@illyse.org'
|
|
|
-
|
|
|
# Ensure arrays when needed
|
|
|
if not isinstance(to, list):
|
|
|
to = [to]
|
|
|
if not isinstance(attachements, list):
|
|
|
attachements = [attachements]
|
|
|
|
|
|
- # If .html is specified in template name remove it
|
|
|
- if template_to_use.endswith('.html'):
|
|
|
- template_to_use = template_to_use[:-5]
|
|
|
+ # Add domain in context
|
|
|
+ context['domain'] = Site.objects.get_current()
|
|
|
|
|
|
- # Get html template, fail if not exists
|
|
|
- template_html = get_template('%s.html' % (template_to_use,))
|
|
|
+ # If .html/.txt is specified in template name remove it
|
|
|
+ body_template = body_template.split('.')[0]
|
|
|
+ subject_template = subject_template.split('.')[0]
|
|
|
+
|
|
|
+ # Get html template for body, fail if not exists
|
|
|
+ template_html = get_template('%s.html' % (body_template,))
|
|
|
html_content = template_html.render(Context(context))
|
|
|
|
|
|
+ # Get txt template for subject, fail if not exists
|
|
|
+ subject_template = get_template('%s.txt' % (subject_template,))
|
|
|
+ subject = subject_template.render(Context(context))
|
|
|
+
|
|
|
# Try to get a txt version, convert from html to markdown style
|
|
|
# (using html2text) if fail
|
|
|
try:
|
|
|
- template_txt = get_template('%s.txt' % (template_to_use,))
|
|
|
+ template_txt = get_template('%s.txt' % (body_template,))
|
|
|
text_content = template_txt.render_to_string(Context(context))
|
|
|
except TemplateDoesNotExist:
|
|
|
text_content = html2text.html2text(html_content)
|
|
|
|
|
|
# make multipart email default : text, alternative : html
|
|
|
- msg = EmailMultiAlternatives(subject, text_content, from_email, to)
|
|
|
+ msg = EmailMultiAlternatives(subject=subject, body=text_content, to=to)
|
|
|
msg.attach_alternative(html_content, "text/html")
|
|
|
|
|
|
# Set attachements
|