AttributeError: Mail instance has no attribute '__len__'
Following the example here:
https://sendgrid.com/docs/Integrate/Code_Examples/python.html
I get:
File "c:\Python27\lib\site-packages\sendgrid\sendgrid.py", line 116, in send return self._legacy_send(message) File "c:\Python27\lib\site-packages\sendgrid\sendgrid.py", line 120, in _legacy_send return self._make_request(message) File "c:\Python27\lib\site-packages\sendgrid\sendgrid.py", line 100, in _make_request data = urlencode(self._build_body(message), True).encode('utf-8') File "c:\Python27\lib\urllib.py", line 1343, in urlencode len(v) AttributeError: Mail instance has no attribute 'len'
I just tried that example code and it worked for me.
Could you please share exactly what you are doing? Please remove any sensitive information.
eddiezane, are you on Python 3.x?
I don't know what _build_body or _make_request do, exactly, but I see one issue. urlencode calls len(v) and catches TypeError to decide what's a sequence. This raises TypeError for new-style classes, but AttributeError for old-style classes: https://gist.github.com/dirn/5478924
SendGridClient is a new-style class, but Mail is an old-style class. I don't know if it will solve everything, but making Mail into a new-style class would prevent this particular exception on Python 2.x.
hi Eddiezane,
sorry I missed your update until kengruven's. Unfortunately I've lost my recreate and attempting to follow the sample again works fine for me now.
So I'm afraid I don't know how I caused that problem now. I was using python 2.7 as can be seen from the stack, so not sure whether Kengruven's suspicion applies to what I was seeing. From my memory it was literally what appeared in the sample with my api_user and api_key but clearly it wasn't. Sorry I can't recreate now :( please feel free to close.
Thanks for the update, slaupster.
In any event, as long as Python 2 is supported, it'd be a good idea to upgrade Mail to a new-style class. Old-style classes can cause weird issues (like this one) that can be tricky to track down.
This upgrade to the new-style class fixed my issue. I'm still using Python 2.7.9 on OS X, so i hacked my sendgrid lib directly -- not sure if that's the 'right' thing to do.
vi /usr/local/lib/python2.7/site-packages/sendgrid/message.py
Release 1.4.2 implements the fix mentioned by @shmooth, thanks to @kengruven.