Can't figure out how to attach file to email using EmailMessage

293 views
Skip to first unread message

Patrick Beeson

unread,
Feb 21, 2014, 11:28:40 AM2/21/14
to django...@googlegroups.com
Good morning y'all!

I'm having a heck of a time figuring out what I'm doing wrong in attaching a file uploaded via a ModelForm to an email that's sent if the form is valid. Here's a link to my code on Stackoverflow: http://stackoverflow.com/questions/21938849/inmemoryuploadedfile-object-has-no-attribute-rfind-when-sending-email-with-a

Thanks for your help!

Tom Evans

unread,
Feb 21, 2014, 8:15:57 PM2/21/14
to django...@googlegroups.com
> resume = form.cleaned_data.get('resume')
>
> #Compose message
> email = EmailMessage()
> email.body = '...'
> email.subject = 'A new application has been submitted'
> email.from_email = 'Job application <nor...@email.com>'
> email.to = ['em...@email.com',]
> email.attach_file(resume)

EmailMessage.attach_file() takes the path to a file on disk, not a
django.core.files.File subclass.

You may want to use EmailMessage.attach(), which takes a filename (as
listed in the email, not used for anything else), the contents of the
file as a string, and the content type of the file.

Both are documented here:

https://docs.djangoproject.com/en/1.6/topics/email/#the-emailmessage-class

Cheers

Tom
Reply all
Reply to author
Forward
0 new messages