Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mandrill python API attachment error message

I'm writing a web app for customer/order handling in python with asana integration.

For a registered incoming order, an invoice is created as .pdf. This file I want to send to asana as an email attachment using mandrill, because the asana python API doesn't provide attachments yet.

Because mandrill wants the content of the attachment as a base64-encoded string, I create a binary of the pdf using this function:

def binaryFile(self, pathToFile):
    binary_obj = xmlrpclib.Binary( open(pathToFile).read() )
    return binary_obj

Together with the path of the file, I throw this into mandrill like so:

'attachments': [{'content': binaryFile,
                 'name': pathOfFile,
                 'type': 'application/pdf'}]

When I try to send the whole thing, this is what I get:

  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-   packages/mandrill.py", line 1215, in send
return self.master.call('messages/send', _params)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/mandrill.py", line 131, in call
  params = json.dumps(params)
  OverflowError: Overlong 3 byte UTF-8 sequence detected when encoding string

Who can hint at what I am doing wrong?

Thank you.

like image 290
creimers Avatar asked Jan 12 '23 05:01

creimers


1 Answers

All right ok, I found the mistake myself, it's in the base64-encoding of course. I'm now doing it like so:

import base64

def filetobase64(self, inputfilename):
    return base64.b64encode(open(inputfilename, 'rb').read())

Everything works fine now.

No harm meant!

like image 72
creimers Avatar answered Jan 22 '23 05:01

creimers