2010-06-09 35 views
12

Bất cứ ai có thể vui lòng giúp tôi gửi email html với nội dung động. Một cách là sao chép toàn bộ mã html vào một biến và điền mã động bên trong nó trong chế độ xem Django, nhưng điều đó dường như không phải là một ý tưởng hay, vì nó là một tệp html rất lớn.Làm thế nào để gửi email html với django với nội dung động trong đó?

Tôi sẽ đánh giá cao bất kỳ đề xuất nào.

Cảm ơn.

+2

Tại sao không sử dụng mẫu, như với bất kỳ đoạn kết xuất HTML nào khác trong Django? –

+0

Tôi đang sử dụng mẫu và đã kết xuất thành công các biến, nhưng câu hỏi đặt ra là cách gửi mẫu được hiển thị dưới dạng email? –

+2

sử dụng render_to_string – KillianDS

Trả lời

8

này nên làm những gì bạn muốn:

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

Xem django mail docs để biết thêm.

+4

Hoặc như tôi đã nói, trong 3 dòng đầu tiên, hãy sử dụng phím tắt render_to_string, nó tồn tại vì một lý do. – KillianDS

+0

Cảm ơn, tôi sẽ sử dụng nó trong dự án của tôi, và (nếu tôi nhớ) chỉnh sửa câu trả lời của tôi một khi tôi đã thử nghiệm nó. – blokeley

32

Ví dụ:

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

Reference

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

+1

+1 EmailMultiAlternatives. Tài liệu chính thức: https://docs.djangoproject.com/en/1.8/topics/email/#sending-alternative-content-types – dokkaebi

3

Hãy thử điều này ::::

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]m'] 
    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') 
Các vấn đề liên quan