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()