In my script, User fills a Django form then two things : data are saved in my database and an email is sent to a support account.
I'm looking for improve this little part from my Django web application. All this process works fine, but I would like to improve this part in my view :
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()
I would like to write this part with bold character before each variable.
For example in my email :
And make vertical space where I put both \\n
:
For example :
Bonjour Datasystems,
Vous avez un nouveau ticket ... (currently)
---
Bonjour Datasystems,
Vous avez un nouveau ticket ... (What I would like to get)
Up to now, my email looks like this :
Do you have any solution according to my both issues ?
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>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.