Found the answer in this excellent blob post:
http://john-smith.appspot.com/app-engine--what-the-docs-dont-tell-you-about-processing-inbound-mail
This is how to decode an email attachment for GAE inbound mail:
for attach in mail_message.attachments:
filename, encoded_data = attach
data = encoded_data.payload
if encoded_data.encoding:
data = data.decode(encoded_data.encoding)
On Aug 4, 6:26 pm, Kwame <[email protected]> wrote:
> I have properly implemented InboundMailHandler and I'm able to
> process all other mail_message fields except mail_message.attachments.
> The attachement filename is read properly but the contents are not
> being saved in the proper mime_type:
>
> if not hasattr(mail_message, 'attachments'):
> raise ProcessingFailedError('Email had no attached
> documents')
>
> else:
> logging.info("Email has %i attachment(s) " %
> len(mail_message.attachments))
>
> for attach in mail_message.attachments:
> filename = attach[0]
> contents = attach[1]
>
> # Create the file
> file_name = files.blobstore.create(mime_type =
> "application/pdf")
>
> # Open the file and write to it
> with files.open(file_name, 'a') as f:
> f.write(contents)
>
> # Finalize the file. Do this before attempting to read it.
> files.finalize(file_name)
>
> # Get the file's blob key
> blob_key = files.blobstore.get_blob_key(file_name)
> return blob_key
>
> blob_info = blobstore.BlobInfo.get(blob_key)
>
> When I try to display the imported pdf file by going to the url: '/
> serve/%s' % blob_info.key()
> I get a page with what seems like encoded data, instead of the actual
> pdf file.
>
> Any ideas? Thanks
--
You received this message because you are subscribed to the Google Groups
"Google App Engine" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/google-appengine?hl=en.