- return msgRoot.as_string()
-
-def send_email(subject, recipients, template, context={}, sender=None, images=[], threaded=True):
- if sender is None:
- sender = (unicode(settings.APP_SHORT_NAME), unicode(settings.DEFAULT_FROM_EMAIL))
-
- if not len(images):
- images = [(os.path.join(str(settings.UPFILES_FOLDER), os.path.basename(str(settings.APP_LOGO))), 'logo')]
-
- context.update(application_settings(None))
- html_body = loader.get_template(template).render(Context(context))
- txt_body = html2text(html_body)
-
- if isinstance(recipients, str):
- recipients = [recipients]
-
- msgs = []
-
- for recipient in recipients:
- if isinstance(recipient, str):
- recipient_data = ('recipient', recipient)
- recipient_context = None
- elif isinstance(recipient, (list, tuple)) and len(recipient) == 2:
- name, email = recipient
- recipient_data = (name, email)
- recipient_context = None
- elif isinstance(recipient, (list, tuple)) and len(recipient) == 3:
- name, email, recipient_context = recipient
- recipient_data = (name, email)
- else:
- raise Exception('bad argument for recipients')
-
- if recipient_context is not None:
- recipient_context = Context(recipient_context)
- msg_html = Template(html_body).render(recipient_context)
- msg_txt = Template(txt_body).render(recipient_context)
- else:
- msg_html = html_body
- msg_txt = txt_body
-
- msg = create_msg(subject, sender, recipient_data, msg_html, msg_txt, images)
- msgs.append((email, msg))
-
- if threaded:
- thread = Thread(target=send_msg_list, args=[msgs])
- thread.setDaemon(True)
- thread.start()
- else:
- send_msg_list(msgs)