0

I have an app that creates a word document, and I need to send it out without saving it locally. In other words, I need to send out an object. I know the code below doesn't work because my document is an object. Is there any way to do this?

TypeError: coercing to Unicode: need string or buffer, Document found

Does anyone know how to do this? Here is what I have:

def document_writer():
document = Document()
document.add_heading('Sample Document', 0)
document.add_paragraph("fsfsa")

document.add_heading('Addresses', 2)


return document

def mailsend(send_from, send_to, subject, text, login, password, document, files=None,
          smtpserver='smtp.gmail.com:587'):

msg = MIMEMultipart()
msg['From'] = send_from
msg['To'] = send_to
msg['Date'] = formatdate(localtime=True)
msg['Subject'] = subject
msg.attach(MIMEText(text))
part = MIMEBase('application', "octet-stream")
part.set_payload(open(document, "rb").read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment; filename="crap.docx"')
msg.attach(part)
server = smtplib.SMTP(smtpserver)
server.starttls()
#server.starttls()
server.login(login, password)
server.sendmail(send_from, send_to, msg.as_string())

server.close()
user3558939
  • 155
  • 1
  • 7

1 Answers1

0

at some point in your code a function expects a string but gets a Document class which python then does not know how to convert to a string.

In your function document_writer you are creating such a class Document So you should check where such created Document ojects are used.

To then solve this Problem there could be three solutions:

  1. This Document Class can be converted to a string. Then you can just do str(Document)
  2. This Document Class can not be converted to a string via str() but has a special function to do this. Then you can just do document_variable.convert_to_str_function()
  3. You are able to save this Document Class to disk as a file. Then you should have a look at those answers: Binary file email attachment problem to send a disk based file via email
quantumbyte
  • 379
  • 1
  • 5