#NEWLINE# def send_mail_with_template( app_name , template_name , receipents , address_from = getattr ( settings , 'DEFAULT_FROM_EMAIL' , None ) , context = None , request = None , attachments = None , fail_silently = False ) : #NEWLINE# #INDENT# '\n    Send email rendering text and html versions for the specified template name using the context dictionary passed in.\n    Arguments are as per django\'s send_mail apart from template which should be the common path and name of the text and html templates without the extension,\n    For example it wil look for the template in order:\n\n    Default django behaviour:\n        - /myproject/templates/"email/<app_name>/<template_name>/"\n        - /myproject/templates/"email/<template_name>/"\n        - /myproject/*<app_name>/templates/"email/<app_name>/<template_name>"/ # NOTE: *<app_name> for every installed app\n        - /myproject/*<app_name>/templates/"email/<template_name>"/ # NOTE: *<app_name> for every installed app\n\n    ' #NEWLINE# if ( context is None ) : #NEWLINE# #INDENT# context = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# #UNINDENT# if ( template_name is None ) : #NEWLINE# #INDENT# attachments = [ ] #NEWLINE# #UNINDENT# if request : #NEWLINE# #INDENT# context_instance = RequestContext ( request , context ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# context_instance = Context ( context ) #NEWLINE# #UNINDENT# if ( not hasattr ( receipents , '__iter__' ) ) : #NEWLINE# #INDENT# receipents = [ receipents ] #NEWLINE# #UNINDENT# render_body = ( lambda type : select_template ( [ ( 'email/%s/%s/body.%s' % ( app_name , template_name , type ) ) , ( 'email/%s/body.%s' % ( template_name , type ) ) ] ) . render ( context_instance ) ) #NEWLINE# template_list = [ ( 'email/%s/%s/subject.txt' % ( app_name , template_name ) ) , ( 'email/%s/subject.txt' % template_name ) ] #NEWLINE# subject = select_template ( template_list ) . render ( context_instance ) . strip ( ) #NEWLINE# email = EmailMultiAlternatives ( subject , render_body ( 'txt' ) , address_from , receipents ) #NEWLINE# try : #NEWLINE# #INDENT# email . attach_alternative ( render_body ( 'html' ) , 'text/html' ) #NEWLINE# #UNINDENT# except TemplateDoesNotExist : #NEWLINE# #INDENT# pass #NEWLINE# #UNINDENT# for attachment in attachments : #NEWLINE# #INDENT# email . attach ( * attachment ) #NEWLINE# #UNINDENT# email . send ( fail_silently = fail_silently )