簡體   English   中英

使用Django EmailMessage在粗體和垂直空間中設置文本

[英]Set text in bold and vertical space with Django EmailMessage

在我的腳本中,用戶填寫Django表單,然后進行兩件事:將數據保存在我的數據庫中,然后將電子郵件發送到支持帳戶。

我正在尋找我的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, 'support@datasystems.fr', ['support@datasystems.fr'], html_message='This is <b>HTML</b> Content')
            mail.attach_file(image.path)
            mail.send()

我想在每個變量之前用黑體寫這部分。

例如在我的電子郵件中:

  • Nom:測試(當前)
  • Nom :測試(我想要得到什么)

並在兩個放置\\n地方留出垂直空間:

例如 :

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

---

Bonjour Datasystems, 

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

到目前為止,我的電子郵件如下所示:

在此處輸入圖片說明

根據我的兩個問題,您有什么解決方案嗎?

制作兩個模板; 我們稱其為message.txt (文本版本)和message.html (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, 'support@datasystems.fr', ['support@datasystems.fr'])
        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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM