I am trying to attach a file to an email. The file is uploaded by the user and put into the media folder.
I have tried two ways.
First:
def email_view(request, pk):
person = Person.objects.get(pk=pk)
email_to = person.email
email = EmailMessage(
'Subject here', 'Here is the message.', 'email@email.com', [email_to])
email.attach_file(person.upload)
email.send()
return redirect('home')
This gives me an error 'FieldFile' object has no attribute 'rfind'
Second:
def email_view(request, pk):
person = Person.objects.get(pk=pk)
email_to = person.email
attachment = str(person.upload)
attachment = 'http://my_site:8080/media/' + attachment.replace('./', '')
email = EmailMessage(
'Subject here', 'Here is the message.', 'email@email.com', [email_to])
email.attach_file(attachment)
email.send()
return redirect('home')
This gives me a page not found. If i copy the url from the error though it takes me to the file. I assume this is happening because of the string format