[Django]-Attaching csv file to email in django

9👍

To attach files to emails sent by django, you’ll have to create an EmailMessage instance and attach the file using the .attach() method.

For example, assuming you have the CSV content in csv_data:

email = EmailMessage('Subject', 'email body', 'from@mail.com', ['to@mail.com'])
email.attach('name.csv', csv_data, 'text/csv')
email.send()

Or, if the CSV data is in a file, you can use:

email.attach_file('/full/path/to/file.csv')

For more information on sending emails, see the docs.

Leave a comment