2010-06-09 10 views
12

क्या कोई मुझे गतिशील सामग्री के साथ HTML ईमेल भेजने में मदद कर सकता है। एक तरीका यह एक चर में पूरे HTML कोड की प्रतिलिपि Django विचारों में यह भीतर गतिशील कोड को भरने के लिए है, लेकिन यह एक अच्छा विचार हो प्रतीत नहीं होता है, इसकी के रूप में एक बहुत बड़ी html फ़ाइल।गतिशील सामग्री के साथ django के साथ एचटीएमएल ईमेल कैसे भेजें?

मैं किसी भी सुझाव की सराहना करेंगे।

धन्यवाद।

+2

क्यों Django में HTML प्रतिपादन के किसी भी अन्य टुकड़े के साथ के रूप में एक टेम्पलेट का उपयोग नहीं? –

+0

मैं एक टेम्पलेट का उपयोग कर रहा है और सफलतापूर्वक के साथ-साथ चर प्रदान की गई है, लेकिन सवाल यह है कि एक ईमेल के रूप में प्रदान की गई है कि टेम्पलेट भेजने के लिए किया जाता है? –

+2

उपयोग – KillianDS

उत्तर

8

यह आप क्या चाहते हैं करना चाहिए:

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

अधिक के लिए django mail docs देखें।

+4

render_to_string या जैसा कि मैंने कहा, पहले 3 लाइनों के लिए, render_to_string शॉर्टकट का उपयोग, यह एक कारण के लिए मौजूद है। – KillianDS

+0

धन्यवाद, मैं इसे अपने प्रोजेक्ट में उपयोग करूँगा, और (यदि मुझे याद है) मैंने इसका जवाब देने के बाद अपना जवाब संपादित किया है। – blokeley

32

उदाहरण:

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

संदर्भ

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

+1

+1 ईमेलमल्टी विकल्प। आधिकारिक दस्तावेज़: https://docs.djangoproject.com/en/1.8/topics/email/#sending-alternative-content-types – dokkaebi

3

इस प्रयास करें ::::

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') 
संबंधित मुद्दे