Sending a file as an attachment for email in Django Ask Question

16 views
Skip to first unread message

krishn...@gmail.com

unread,
Jun 28, 2017, 10:12:49 PM6/28/17
to Django users

I am trying to send a file as an attachment in an email, the file is submitted through a web form. My views.py is as shown below, the problem I am facing is the attached file when seen in the outlook mail doesn't have any content in it. Is there something that I am missing here?

if request.method == 'POST':
    form = RequestForm(request.POST, request.FILES)
    if form.is_valid():
        form.save()
        messages.info(request, '')
        subject = 'abc'
        message = 'xyz'
        attachment = request.FILES['attachment']
        mail = EmailMessage(subject, message, '', [''])
        mail.attach(attachment.name, attachment.read(), attachment.content_type)
        mail.send()
Reply all
Reply to author
Forward
0 new messages