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

2👍

Make two templates; let’s call them message.txt for text version and message.html for the html one. Render them and pass the results to 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()

Example message.txt:

Bonjour Datasystems,

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

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

Example 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>

Leave a comment