2010-06-09 4 views
12

Kann mir bitte jemand helfen, HTML Email mit dynamischen Inhalten zu senden. Eine Möglichkeit besteht darin, den gesamten HTML-Code in eine Variable zu kopieren und den dynamischen Code darin in Django-Ansichten aufzufüllen, aber das scheint keine gute Idee zu sein, da es sich um eine sehr große HTML-Datei handelt.Wie sende ich HTML-E-Mail mit Django mit dynamischen Inhalten drin?

Ich würde mich über Vorschläge freuen.

Danke.

+2

Warum nicht eine Vorlage verwenden, wie bei jedem anderen HTML-Rendering in Django? –

+0

Ich verwende eine Vorlage und habe auch die Variablen erfolgreich gerendert, aber die Frage ist, wie man diese gerenderte Vorlage als eine E-Mail senden? –

+2

Verwenden Sie render_to_string – KillianDS

Antwort

8

Dies sollte das tun, was Sie wollen:

from django.core.mail import EmailMessage 
from django.template import Context 
from django.template.loader import get_template 


template = get_template('myapp/email.html') 
context = Context({'user': user, 'other_info': info}) 
content = template.render(context) 
if not user.email: 
    raise BadHeaderError('No email address given for {0}'.format(user)) 
msg = EmailMessage(subject, content, from, to=[user.email,]) 
msg.send() 

die django mail docs Siehe für mehr.

+4

Oder wie gesagt, für die ersten 3 Zeilen, verwenden Sie die render_to_string Verknüpfung, es existiert aus einem bestimmten Grund. – KillianDS

+0

Danke, ich werde das in meinem Projekt verwenden und (wenn ich mich erinnere) meine Antwort bearbeiten, sobald ich sie getestet habe. – blokeley

32

Beispiel:

from django.core.mail import EmailMultiAlternatives 
from django.template.loader import render_to_string 
from django.utils.html import strip_tags 

subject, from_email, to = 'Subject', '[email protected]', '[email protected]' 

html_content = render_to_string('mail_template.html', {'varname':'value'}) # render with dynamic value 
text_content = strip_tags(html_content) # Strip the html tag. So people can see the pure text at least. 

# create the email, and attach the HTML version as well. 
msg = EmailMultiAlternatives(subject, text_content, from_email, [to]) 
msg.attach_alternative(html_content, "text/html") 
msg.send() 

Referenz

http://www.djangofoo.com/250/sending-html-email/comment-page-1#comment-11401

+1

+1 EmailMultiAlternativen. Offizielle Dokumente: https://docs.djangoproject.com/en/1.8/topics/email/#sending-alternative-content-types – dokkaebi

3

Try this ::::

https://godjango.com/19-using-templates-for-sending-emails/

sample code link

# views.py 

from django.http import HttpResponse 
from django.template import Context 
from django.template.loader import render_to_string, get_template 
from django.core.mail import EmailMessage 

def email_one(request): 
    subject = "I am a text email" 
    to = ['[email protected]'] 
    from_email = '[email protected]' 

    ctx = { 
     'user': 'buddy', 
     'purchase': 'Books' 
    } 

    message = render_to_string('main/email/email.txt', ctx) 

    EmailMessage(subject, message, to=to, from_email=from_email).send() 

    return HttpResponse('email_one') 

def email_two(request): 
    subject = "I am an HTML email" 
    to = ['[email protected]'] 
    from_email = '[email protected]' 

    ctx = { 
     'user': 'buddy', 
     'purchase': 'Books' 
    } 

    message = get_template('main/email/email.html').render(Context(ctx)) 
    msg = EmailMessage(subject, message, to=to, from_email=from_email) 
    msg.content_subtype = 'html' 
    msg.send() 

    return HttpResponse('email_two') 
Verwandte Themen