1👍
✅
Your code should look something like this:
def foo(request):
#need to check that form was submitted
if request.method == "POST":
#this checks that a file exists
if len(request.FILES) != 0:
file1 = request.FILES['file1']
file1str = file1.read()
file_type = str(request.FILES['file1'].content_type)
email_msg = EmailMessage(subject="email subject", body="email body",
from_email="...", to=["..."])
#need to try to attach the file, using the attach method
try:
email_msg.attach('file1', file1str, file_type)
except Exception as e:
print(str(e))
email_msg.send()
return render(request, '/needs-confirmation.html', {})
You will need to fill in the emails again.
You’ve left out a lot of key steps, and your HTML needs a submit button and crsf_token. This works for text files, you may need to do some more processing for other file types.
Hope this helps.
Source:stackexchange.com