2017-01-31 11 views
-1
from django.core.mail import EmailMultiAlternatives 

subject, from_email, to = 'hello', '[email protected]', '[email protected]' 
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() 

可能であれば、画像を含め、適切に設計されたニュースレターや電子メールを送信したいと考えています。私は、HTMLテンプレートファイルにこのdjangoからプロの電子メールを送信

html_content = '<p>This is an <strong>important</strong> message.</p>' 

を変更することができれば、私は思っていました。その後、どのように私はQuoraのと他のwebsites.`

+0

[Djangoで電子メールテンプレートを作成する]の可能な複製(0120-919-03-J) – Sayse

答えて

2

のような電子メールを送信して行くんでない場合は、この

html_content = 'newsletter/news.html' 

のように簡単に任意のテンプレートを取ることができ、送信を含む任意の目的のために、文字列にそれを回しますメール:

from django.template.loader import get_template 

template = get_template('newsletter/news.html') 
html_content = template.render({pass any context you might need}) 
2

あなたが直接そのようなテンプレートを渡すことはできませんが、あなたがrender_to_string

from django.template.loader import render_to_string 
html_content = render_to_string('newsletter/news.html', {'foo': 'bar'}) 
を使用することができます

は、しかし、HTML形式の電子メールに一定の制限があることに注意してください。たとえば、javascriptを使用することはできません。画像は表示される場合と表示されない場合があり、外部スタイルシートは使用できません。

+0

render_to_stringを忘れて、行が保存されません。 – RemcoGerlich

+0

しかし、あなたが先にそうここに私@RemcoGerlichの5秒になった非常に私 – e4c5

+0

感謝から+1です...私はこれを試してみましたが、それも働いhtml_content = get_template(「ニュースレター/ news.html」)。(レンダリング) – sambeth

関連する問題