How to Add Various File Attachments to an Email in Django

In this article, we show to add various file attachments to an email in Django.
When you're building a website, many times, it's not simply good enough to send plain text in an email. Many times, we want to add images, videos, or attachments such as PDF.
PDFs are actually widely used to giving customer back data such as various statements, order history, and things of that sort.
We can use the EmailMultiAlternatives() function in order to send attachments in Django. This function will allow for sending of documents like PDFs.
Below is a sample code that sends an email with an attachment in Django.
Just like the send_email() function, this function takes in the subject (or title) of the email, the body, the sender email, and the email that you are sending to.
MIME stands for Multipurpose Internet Mail Extensions. It's an internet standard that defines how email messages are formatted when they contain data other than ASCII character sets. MIME is an extension of the Simple Mail Transport Protocol (SMTP) email protocol, and it allows users to exchange different types of data files in their emails, including images, audio, video, and application programs.
This is exactly what we are looking to do with a document such as a PDF.
In order to send any attachment such as a PDF, first, you must specify the file path to that document. This is may be in your static folder or media folder. If it is user generated, then it would be in some given path in the media folder.
'rb' means that you are opening the file in read mode and binary mode.
With the attach() function, we are actually attaching the file to the email.
The first parameter allows us to name the file, which we do, "diploma.pdf"
The second argument allows us to read this attachment.
The third argument specifies the type of file that it is, 'application/pdf'
If it is not a PDF that you are sending, then this argument would change to the type of file that we are attaching.
The file path should be a relative path on the website.
Below is such an email sent in Django using the EmailMultiAlternatives()
with its corresponding attributes.
If you want to send an image as an attachment, such as a png, a jpg, then you need to modify the code for this.
Below is the code to send a png image as an attachment in Django.
So any time you are sending an image first, you specify 'image/' followed by the type of image file that it is.
To send a text file, we specify 'text/' followed by the type of file.
To send a text file for a Microsoft notepad document, we specify 'text/txt'
Below is the code to send a txt image as an attachment in Django.
For all other files that are more complex like rtf files, microsoft word file, powerpoint files, you specify 'application/' followed by the type of file that it is.
So if you are sending a rtf file, you would specify 'application/rtf'
Videos are a little bit more complex to attach due to large file sizes and things of that sort, but for video files, you would specify 'video/' followed by the type of file that it is.
So if you are sending an mp4 file, you would specify, 'video/mp4'.
This is all that is required to send an email in Django with any of
various attachments, giving emails a great, professional look.
Related Resources
How to Randomly Select From or Shuffle a List in Python