Thanks a lot Alex, you are right the from email import encoders was 
missing. Now it sends the email but without the file attached to it. 

Any idea what could go wrong with this function? (I corrected other things 
on it, it's the result of patchwork of code I took from several places)


def create_message_with_attachment(sender, to, subject, msgHtml, msgPlain):

 # Create message container - the correct MIME type is 
multipart/alternative.
 msg = MIMEMultipart('alternative')
 msg['To'] = to
 msg['From'] = sender
 msg['Subject'] = subject
 
 # Record the MIME types of both parts - text/plain and text/html
 part1 = MIMEText(msgPlain, 'plain')
 part2 = MIMEText(msgHtml, 'html')
 
 # create .txt attachment
 filename=r"C:\Users\xxx\Desktop\test_Attachment.txt"
 fp=open(filename,'rb')
 att = email.mime.application.MIMEApplication(fp.read(),_subtype="txt")
 fp.close()
 att.add_header('Content-Disposition','attachment',filename=filename)


 # Attach parts into message container.
 msg.attach(att)
 msg.attach(part1)
 msg.attach(part2)
 
 # raw = base64.urlsafe_b64encode(msg.as_bytes())
 raw= encoders.encode_base64(msg)
 raw = raw.decode()
 body = {'raw': raw}
 return body

-- 
You received this message because you are subscribed to the Google Groups 
"Google App Engine" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to google-appengine+unsubscr...@googlegroups.com.
To post to this group, send email to google-appengine@googlegroups.com.
Visit this group at https://groups.google.com/group/google-appengine.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/google-appengine/3017717e-019f-47a8-96ce-4c45723e9d31%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to