<commit_before>"""\
Contains a utility method to send an e-mail based on a Django template.
"""

from django.template import Context, loader
from django.core.mail import EmailMessage
from django.conf import settings

def send_email(request, context, template_name, cls=None, to_email=None):

    if to_email is not None:
        pass
    elif cls and hasattr(cls.conf, 'to_email'):
        to_email = cls.conf.to_email
    else:
        to_email = ('%s <%s>' % admin for admin in settings.MANAGERS)

    if cls and hasattr(cls.conf, 'from_email'):
        from_email = cls.conf.from_email
    else:
        from_email = settings.DEFAULT_FROM_EMAIL

    email_context = Context({
        'session_key': request.session.session_key,
        'devid': request.device.devid,
        'ua': request.META['HTTP_USER_AGENT'],
        'lon': request.session.get('geolocation:location', (None, None))[0],
        'lat': request.session.get('geolocation:location', (None, None))[1],
        'from_email': from_email,
        'host': request.META.get('HTTP_HOST'),
        'to_email': to_email,
        'request': request,
    })
    email_context.update(context)

    template = loader.get_template(template_name)
    email = template.render(email_context)

    headers, last_header = {}, None
    headers_section, body = email.split('\n\n', 1)
    for header in headers_section.split('\n'):
        if header.startswith(' '):
            headers[last_header] += ' ' + header.strip()
        else:
            key, value = header.split(': ', 1)
            headers[key] = value
            last_header = key

    subject = headers.pop('Subject', '[no subject]')
    from_email = headers.pop('from_email', from_email)
    if 'to_email' in headers:<commit_msg>Fixed bug where e-mail sending would fail if there was no request object in the call stack.<commit_after>"""\
Contains a utility method to send an e-mail based on a Django template.
"""

from django.template import Context, loader
from django.core.mail import EmailMessage
from django.conf import settings

def send_email(request, context, template_name, cls=None, to_email=None):

    if to_email is not None:
        pass
    elif cls and hasattr(cls.conf, 'to_email'):
        to_email = cls.conf.to_email
    else:
        to_email = ('%s <%s>' % admin for admin in settings.MANAGERS)

    if cls and hasattr(cls.conf, 'from_email'):
        from_email = cls.conf.from_email
    else:
        from_email = settings.DEFAULT_FROM_EMAIL

    email_context = Context({
        'from_email': from_email,
        'to_email': to_email,
    })
    if request:
        email_context.update({
            'session_key': request.session.session_key,
            'devid': request.device.devid,
            'ua': request.META['HTTP_USER_AGENT'],
            'lon': request.session.get('geolocation:location', (None, None))[0],
            'lat': request.session.get('geolocation:location', (None, None))[1],
            'host': request.META.get('HTTP_HOST'),
            'request': request,
        })
    email_context.update(context)

    template = loader.get_template(template_name)
    email = template.render(email_context)

    headers, last_header = {}, None
    headers_section, body = email.split('\n\n', 1)
    for header in headers_section.split('\n'):
        if header.startswith(' '):
            headers[last_header] += ' ' + header.strip()
        else:
            key, value = header.split(': ', 1)
            headers[key] = value
            last_header = key

    subject = headers.pop('Subject', '[no subject]')
    from_email = headers.pop('from_email', from_email)
    if 'to_email' in headers:
{'fraction_non_alphanumeric': 0.12691466083150985, 'fraction_numerical': 0.002188183807439825, 'mean_word_length': 2.849473684210526, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6856815', 'n_tokens_mistral': 1117, 'n_tokens_neox': 1114, 'n_words': 276}