1# Copyright (c) 2012-2016 Seafile Ltd.
2import os
3import logging
4from django.template import loader
5from django.core.mail import EmailMessage
6
7from seahub.utils import get_site_scheme_and_netloc, get_site_name
8from seahub.settings import MEDIA_URL, LOGO_PATH, \
9        MEDIA_ROOT, CUSTOM_LOGO_PATH
10
11logger = logging.getLogger(__name__)
12
13
14def send_html_email_with_dj_template(recipients, subject, dj_template, context={}):
15    """
16
17    Arguments:
18    - `recipients`:
19    - `subject`:
20    - `dj_template`:
21    - `context`:
22
23    """
24
25    # get logo path
26    logo_path = LOGO_PATH
27    custom_logo_file = os.path.join(MEDIA_ROOT, CUSTOM_LOGO_PATH)
28    if os.path.exists(custom_logo_file):
29        logo_path = CUSTOM_LOGO_PATH
30
31    base_context = {
32        'url_base': get_site_scheme_and_netloc(),
33        'site_name': get_site_name(),
34        'media_url': MEDIA_URL,
35        'logo_path': logo_path,
36    }
37    context.update(base_context)
38    t = loader.get_template(dj_template)
39    html_message = t.render(context)
40
41    mail = EmailMessage(subject=subject, body=html_message, to=[recipients])
42    mail.content_subtype = "html"
43
44    try:
45        mail.send()
46        return True
47    except Exception as e:
48        logger.error(e)
49        return False
50