I would recommend you post this code and question to stackoverflow.com -- a
very popular site regularly visited by many programming experts who like to
answer technical questions (me included, when I have free time). You may
want to just tag the question as python and email, since it's not really
germane to google-app-engine (if you tagged it with the latter anyway, your
question might be avoided by experts on python and email who are not also
expert on App Engine).


Alex


On Fri, Mar 3, 2017 at 12:19 PM, Guillaume France <gc.com...@gmail.com>
wrote:

>
> 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
> <https://groups.google.com/d/msgid/google-appengine/3017717e-019f-47a8-96ce-4c45723e9d31%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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/CAE46Be-tAdEGj84ExamTXv5HOGptW-CanLpDxYy8t%3DnTgqmhVw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to