Skip to content Skip to sidebar Skip to footer

Create Word Document And Then Attach It To Email Django

I'm currently using python_docx in order to create Word Documents in Python. What I'm trying to achieve is that I need to create Document File in Django and then attach it to an em

Solution 1:

The problem is in this code :

def sendmail(self, name,email,description,location):
    message = EmailMessage('Custom Mail', 'Name: '+str(name)+'\nEmail: '+str(email)+'\nDescription: '+str(description)+'\nLocation: '+str(location), 'test@gmail.com',to=['testreceiver@gmail.com'])
    docattachment = generate('Test','CONTENT')
    message.attach(docattachment.name,docattachment.read(),docattachment.content_type)
    message.send()

In this line :

message.attach(docattachment.name,docattachment.read(),docattachment.content_type)

docattachment is the response got from generate() fucntion, and docattachment does not have any attribute named : name or read()

You need to replace above code with this:

message.attach("Test.doc",docattachment,'application/vnd.openxmlformats-officedocument.wordprocessingml.document')

And the making of the file, it shouldn't be an HttpResponse and instead use BytesIO to deliver the file.

Post a Comment for "Create Word Document And Then Attach It To Email Django"