how can I send a mail with a file saved in a FileField? I don't know how to access the file in the code below.
mail = EmailMessage(subject, message, 'from@from,com', ['to@to.com'])
mail.attach(?, ?, 'application/pdf')
mail.send()
EDIT
I tried to open the file with
f = list_pca.pdf_file.open(mode='rb')
where list_pca is an instance of
class ListPCA(models.Model):
pdf_file = models.FileField(upload_to=get_file_path_j, null=True, blank=True)
but I get an error "No such file or directory", because the path is wrong.
and
list_pca.pdf_file.path
return the wrong path too. Isn't it supposed to know where is the file thanks to the upload_to
option?
Thanks
Thanks
mail = EmailMessage(subject, message, 'from@from,com', ['to@to.com'])
mail.attach('arbitrary_filename', myfile.read(), 'application/pdf')
mail.send()
Since you're using EmailMessage, you can just pass it the attachments kwarg as well.
email = EmailMessage(subject, message, 'from@from,com', ['to@to.com'],
attachments=(('foo.pdf', myfile.read(), 'application/pdf'),))