python - 使用 Django EmailMessage 将文本设置为粗体和垂直空间

标签 python django

在我的脚本中,用户填写 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)

到目前为止,我的电子邮件如下所示:

enter image description here

针对我的这两个问题,你们有什么解决办法吗?

最佳答案

制作两个模板;我们将文本版本称为 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, '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>

关于python - 使用 Django EmailMessage 将文本设置为粗体和垂直空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45215007/

相关文章:

python - python pandas 数据框中的热图标签和位置

python - 可以在 pandas 数据框中创建子列吗?

python - django 和 uwsgi + nginx 中的内部服务器错误

django - 自定义标签在开始和结束时做一些事情

python - 如何使用 django mptt?

python - Django 仅创建用于测试的模型

Python - Beautiful Soup 查找文本不起作用

python - 如何应用 ConvLSTM 层

python - 当我将 python 线程设置为守护进程时有什么区别

python - .po 文件中的 Django 生成的波兰语复数形式不起作用