Question

Hi I've just found out that I can't use smtplib to send emails from GAE, but I need to specify custom mime-types as in:

part = MIMEBase('application', "vnd.openxmlformats-officedocument.wordprocessingml.document")
part.set_payload( doc )
Encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment; filename="%s"' % "ackReceived.docx")
msg.attach(part)

If I try to use the api in the documented way it sends the attachment with 'application/msword' which is causing my docx to corrupt.

Can I specify the mime-type manually using google's mail api?

Edit: A little more about the problem I face when my docx is sent by mail api:

smtplib sends the attachment like this:

Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document
MIME-Version: 1.0
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="ackReceived.docx"

UEsDBBQABgAIAAAAIQCCVgdJnwEAAMgIAAATAAAAW0NvbnRlbnRfVHlwZXNdLnhtbMWWS0/DMBCE
70j8h8hXlLhwQAg17YHHEZAo4mzsTWMRP2Qvj/571k1bIaiaiDbiEilZz3wzsSJnPP00TfYOIWpn
S3ZajFgGVjql7bxkT7Pb/IJlEYVVonEWSraAyKaT46PxbOEhZqS2sWQ1or/kPMoajIiF82BpUrlg...

Google mail api sends the same file like this:

Content-Type: application/msword
MIME-Version: 1.0
Content-Disposition: attachment; filename="ackReceived.docx"

PK�����!�‚VIŸ��È�����[Content_Types].xmlÅ–KOÃ0„ïHü‡ÈW”¸p@5íÇ(âlìMc?d/þ{ÖM[!¨šˆ6â)YÏ|3±"g<ý4Mö!jgKvZŒXV:¥í¼dO³Ûü‚e…U¢qJ¶€È¦“ã£ñlá!f¤¶±d5¢¿ä<ÊŒˆ…ó`iR¹`Òm˜s/䫘?ιtÁbŽÉƒMÆ×P‰·³›OzÜ&!9Ë®Úu    U2á}£¥@ó4å[uÞÎè´IÜô|»"@w Þ­úÑ'_u)H¹\kíãÉ*Ó=½Ì d"à0dÇ?\P\9ùfQ쮶…çªJKØè“›NBŒ´K¦)6#´íÌqÑ@<|ŠÖ·'þYc}SU ±Oó¤-~i»i€Hñ†¨»rîŒð/ƒ¥øfÞ¤r­Ã!ö~cݬ(ÃÚ¹3B
BA8=|‚Ö¸'ÿìßøi³éß÷äп7Ÿxâ¥!¬¬;C ”Ð^÷߉¥Í.$­|ÎG:yÃj¯¼¤Î©°‡€z÷—¶!’õÞý Ö...
Was it helpful?

Solution

In short, you can't specify custom mime types using Google AppEngine e-mail.

In order to send e-mail via Google AppEngine through Google you need to use the Mail API provided. You can see why .docx is using the content type application/msword here: https://cloud.google.com/appengine/docs/standard/python/mail/mail-with-headers-attachments. You might want to submit an issue to the issue tracker in order to resolve this.

It may be worth looking at 3rd party e-mail providers such as Amazon's SES or SendGrid. I've personally been using Amazon's SES to get around a lot of the lack of features and restrictions in GAE's Mail API (though Google is constantly improving this API).

You can use the boto library to communicate with AWS SES and send RAW e-mail messages. (Look at this question for an example on how to create the e-mail, small modifications may be needed for your purposes)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top