2017-07-20 3 views
1

私のスクリプトでは、ユーザはDjangoフォームを2つ入力します。データはデータベースに保存され、電子メールはサポートアカウントに送られます。Django EmailMessageで太字と縦のスペースにテキストを設定

私はDjango Webアプリケーションからこの小さな部分を改善したいと考えています。このすべてのプロセスが正常に動作しますが、私は私の見解では、この部分を改善したいと思います:

subject = "Ticket n° " + str(post.id) + " : " + str(post.Objet.encode('utf-8')) 
      message = "Bonjour Datasystems, \n \n Vous avez un nouveau ticket en attente comportant les informations suivantes : \n " + "Nom : " + str(post.Nom.encode('utf-8')) + " \n Prénom : " + str(post.Prenom.encode('utf-8')) + " \n Société/Association client : " + str(request.user.last_name.encode("utf-8")) + " \n N° Téléphone : " + str(post.Telephone.encode("utf-8")) + " \n Adresse Email : " + str(post.Mail.encode("utf-8")) + " \n Description du problème : " + str(post.Description.encode("utf-8")) 
      image = post.Image 

      mail = EmailMessage(subject, message, '[email protected]', ['[email protected]'], html_message='This is <b>HTML</b> Content') 
      mail.attach_file(image.path) 
      mail.send() 

私は、各変数の前に大胆な文字で、この部分を書きたいと思います。

私の電子メールでの例

  • ノム:テスト(現在は)
  • ノム:テスト(私は取得したいと思い何)

そして私縦のスペースを作ります両方を入れる\n

例:

今まで
Bonjour Datasystems, 
Vous avez un nouveau ticket ... (currently) 

--- 

Bonjour Datasystems, 

Vous avez un nouveau ticket ... (What I would like to get) 

は、私の電子メールは次のようになります。

enter image description here

あなたは私の両方の問題に応じて任意の解決策を持っていますか?

答えて

2

2つのテンプレートを作成します。テキストバージョンの場合はmessage.txt、htmlの場合はmessage.htmlとします。それらをレンダリングし、EmailMessage()に結果を渡す:

from django.template.loader import get_template 

context = { 
      'Nom' : str(post.Nom.encode('utf-8')), 
      'Prenom' : str(post.Prenom.encode('utf-8')), 
      'Telephone' : str(post.Telephone.encode('utf-8')), 
      'Email' : str(post.Mail.encode('utf-8')), 
      'Objet' : str(post.Objet.encode('utf-8')), 
      'Description' : str(post.Description.encode('utf-8')), 
      'Client' : str(request.user.last_name.encode("utf-8")), 
     } 

     message = get_template('message.txt').render(context) 
     html_message = get_template('message.html').render(context) 
     subject = "Ticket n° " + str(post.id) + " : " + str(post.Objet.encode('utf-8')) 

     image = post.Image 

     mail = EmailMultiAlternatives(subject, message, '[email protected]', ['[email protected]']) 
     mail.attach_alternative(html_message, "text/html") 
     mail.attach_file(image.path) 
     mail.send() 

message.txt

Bonjour Datasystems, 

Vous avez un nouveau ticket en attente comportant les informations suivantes : 

Nom : {{ nom }} 
foo : {{ bar }} 

message.html

<html> 
<body> 
    <h1>Bonjour Datasystems,</h1> 

    <p>Vous avez un nouveau ticket en attente comportant les informations suivantes :</p><br><br> 
    <p> 
     <strong>Nom</strong> : {{ nom }}<br> 
     <strong>foo</strong> : {{ bar }} 
    </p> 
</body> 
</html> 
+0

私はこのような何かを読んでいました。 .txtと.htmlのコンテンツの違いは何ですか?これは、同じ拡張子のファイルですか?次に、htmlやtxtファイルでdjango変数 'str(post.Nom.encode( 'utf-8'))'を呼びたい場合、 '' nom''と書かなければなりません。 – Deadpool

+0

'.txt'はあなたのメッセージのテキスト版と' .html'を含んでいなければなりません。私は私の答えに例を追加しました。変数は '{{nom}}'のように使われます - これらのテンプレートは標準のDjangoテンプレート構文を使います。 – rafalmp

+0

ありがとう!私はあなたの答えを更新し、あなたの答えから一部を編集します;) CSSはEmailMessageで動作しますか? – Deadpool

関連する問題