2010-06-09 14 views
12

Qualcuno può aiutarmi a inviare email html con contenuti dinamici. Un modo è copiare l'intero codice html in una variabile e compilare il codice dinamico al suo interno nelle viste Django, ma non sembra essere una buona idea, dato che è un file html molto grande.Come inviare email html con django con contenuti dinamici al suo interno?

Gradirei qualche suggerimento.

Grazie.

+2

Perché non utilizzare un modello, come con qualsiasi altro rendering HTML in Django? –

+0

Sto usando un modello e ho reso correttamente anche le variabili, ma la domanda è come inviare il modello reso come e-mail? –

+2

usa render_to_string – KillianDS

risposta

8

Questo dovrebbe fare quello che vuoi:

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() 

Vedere la django mail docs di più.

+4

O come ho detto, per le prime 3 righe, usa la scorciatoia render_to_string, esiste per un motivo. – KillianDS

+0

Grazie, lo userò nel mio progetto e (se ricordo) modifica la mia risposta una volta che l'ho provata. – blokeley

32

Esempio:

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() 

Riferimento

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

+1

+1 EmailMultiAlternatives. Documenti ufficiali: https://docs.djangoproject.com/en/1.8/topics/email/#sending-alternative-content-types – dokkaebi

3

Prova questa ::::

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') 
Problemi correlati