Attach csv file to email in django

I need to create mail that should have a csv file as an attachment. How to connect csv file to mail in django?

+3
source share
1 answer

To attach files to emails sent by django, you need to create an instance EmailMessageand attach the file with .attach().

For example, if you have 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 about sending emails, see the docs .

+8
source

All Articles