Ticket #5963: render_to_mail.diff

File render_to_mail.diff, 3.3 KB (added by Marinho Brandão, 12 years ago)

render_to_mail.diff

  • django/core/mail.py

     
    314314        """Attach an alternative content representation."""
    315315        self.attach(content=content, mimetype=mimetype)
    316316
    317 def send_mail(subject, message, from_email, recipient_list, fail_silently=False, auth_user=None, auth_password=None):
     317def send_mail(subject, message, from_email, recipient_list, fail_silently=False, auth_user=None, auth_password=None, auto_send=True):
    318318    """
    319319    Easy wrapper for sending a single message to a recipient list. All members
    320320    of the recipient list will see the other recipients in the 'To' field.
     
    327327    """
    328328    connection = SMTPConnection(username=auth_user, password=auth_password,
    329329                                 fail_silently=fail_silently)
    330     return EmailMessage(subject, message, from_email, recipient_list, connection=connection).send()
     330    email_obj = EmailMessage(subject, message, from_email, recipient_list, connection=connection)
    331331
     332    if auto_send:
     333        return email_obj.send()
     334
     335    # Returns email message object if auto_send is not True
     336    else:
     337        return email_obj
     338
    332339def send_mass_mail(datatuple, fail_silently=False, auth_user=None, auth_password=None):
    333340    """
    334341    Given a datatuple of (subject, message, from_email, recipient_list), sends
  • django/shortcuts/__init__.py

     
    88from django.http import HttpResponse, Http404
    99from django.db.models.manager import Manager
    1010from django.db.models.query import QuerySet
     11from django.core.mail import send_mail
     12from django.conf import settings
     13from django.template import Context
     14from django.template.loader import get_template
    1115
    1216def render_to_response(*args, **kwargs):
    1317    """
     
    1721    httpresponse_kwargs = {'mimetype': kwargs.pop('mimetype', None)}
    1822    return HttpResponse(loader.render_to_string(*args, **kwargs), **httpresponse_kwargs)
    1923
     24def render_to_mail(template_path, params, subject, from_email=None, recipient_list=[], auth_user=None, auth_password=None, fail_silently=False, content_subtype=None, attachments=[], **kwargs):
     25    """
     26    Sends a e-mail message with content parsed from a template. The syntax is
     27    the same of render_to_response function.
     28    Returns then boolean of mail sending, using core.mail.send_mail function.
     29    """
     30    content_subtype = content_subtype or getattr(settings, 'DEFAULT_EMAIL_CONTENT_SUBTYPE', 'html')
     31    from_email = from_email or getattr(settings, 'DEFAULT_FROM_EMAIL', None)
     32
     33    if not recipient_list:
     34        return False
     35
     36    # Loads template for mail content
     37    t = get_template(template_path)
     38
     39    # Render content
     40    message = t.render(Context(params))
     41
     42    # Email object
     43    email_obj = send_mail(subject, message, from_email, recipient_list, fail_silently, auth_user, auth_password)
     44
     45    email_obj.content_subtype = content_subtype
     46
     47    if attachments:
     48        for att in attachments:
     49            email_obj.attach_file(att)
     50
     51    return email_obj.send()
     52
    2053def _get_queryset(klass):
    2154    """
    2255    Returns a QuerySet from a Model, Manager, or QuerySet. Created to make
Back to Top