SORU
11 Mayıs 2010, Salı


Oluşturma Django şablonları, e-posta

HTML e-postalar göndermek için, bu gibi Django şablonları kullanmak istiyorum

<html>
<body>
hello <strong>{{username}}</strong>
your account activated.
<img src="mysite.com/logo.gif" />
</body>

send_mail, hakkında hiçbir şey bulamıyorum ve django-mailler sadece HTML şablonlar, dinamik veri olmadan gönderir.

Nasıl Django şablon motoru oluşturmak için kullanın e-posta mı?

CEVAP
11 Mayıs 2010, Salı


E-posta alternatif kullanmak istediğiniz içerik türlerini, bu gibi: HTML göndermek için the docs

from django.core.mail import EmailMultiAlternatives

subject, from_email, to = 'hello', 'from@example.com', 'to@example.com'
text_content = 'This is an important message.'
html_content = '<p>This is an <strong>important</strong> message.</p>'
msg = EmailMultiAlternatives(subject, text_content, from_email, [to])
msg.attach_alternative(html_content, "text/html")
msg.send()

Muhtemelen e-posta - bu gibi görünen bir düz metin, şablonlar saklı dizini email.txt altında için: iki şablonları isteyeceksiniz

Hello {{ username }} - your account is activated.

ve bir HTMLy, email.html altında saklı:

Hello <strong>{{ username }}</strong> - your account is activated.

O zaman bu e-posta gibi get_template, yararlanarak hem bu şablonları kullanarak gönderebilirsiniz

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

plaintext = get_template('email.txt')
htmly     = get_template('email.html')

d = Context({ 'username': username })

subject, from_email, to = 'hello', 'from@example.com', 'to@example.com'
text_content = plaintext.render(d)
html_content = htmly.render(d)
msg = EmailMultiAlternatives(subject, text_content, from_email, [to])
msg.attach_alternative(html_content, "text/html")
msg.send()

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • Floortile83

    Floortile83

    16 Ocak 2010
  • GWTLecturer

    GWTLecturer

    18 EKİM 2012
  • PorterRobinsonVEVO

    PorterRobins

    11 Kasım 2013