I need to send a models.FileField as an email attachment using Django. I’ve seen snippets that show how to do this with the raw request.FILES data (which still contains the Content-Type), but have not been able to find anything that shows how to do it once you’ve already saved the file in a models.FileField. The content type seems to be inaccessible from the models.FileField.
Can someone give me an example of how this would work? I’m beginning to think that I might have to store the Content-Type in the model when I save the file.
Thanks!
I would just not supply a content type and let the recipient’s email client work it out. Unless it will be something unusual it shouldn’t be a problem.
RFC2616 states:
but…
If you want to specify it then storing the content type on upload is a very good idea. It should be noted that django’s own docs say to verify the data from users
If you are on a *unix OS you could try to guess/inspect it:
(from How to find the mime type of a file in python? )