2π
β
If you want body of your email message to be the content of myfile.txt
then (suppose myfile.txt
is stored in media > files > myfile.txt
):
from mysite.settings import MEDIA_ROOT
import os
file = open(MEDIA_ROOT + os.path.sep + 'files/myfile.txt', 'r')
content = file.read()
file.close()
# EmailMessage class version
mail = EmailMessage('subject', content, 'from@example.com', ['to@example.com'])
mail.send()
# send_mail version
send_mail('TEST', content, 'admin@test.com', [test@test.com], fail_silently=False)
π€Aamir Rind
4π
What you need is render_to_string
,
from django.template.loader import render_to_string
body = render_to_string('myfile.txt', foovars)
send_mail('TEST', body, 'admin@test.com', [test@test.com], fail_silently=False)
where foovars
is a dict with the values that you need to render in the βtemplateβ myfile.txt
if proceed of course, else {}
be sure your myfile.txt
lies on your template directory, that is all π
π€trinchet
Source:stackexchange.com