Issue Summary
I'm using sendgrid-python version 5.6.0. I'm able to send emails as long as there are only the following fields in the email
- from
- to
- subject
- content/body
Apart from this any extra field if added to personalization object, I get a HTTP 400 BadRequest. Moreover, the response body is empty so there is no specific error message. I'm using mail helper function but you can clearly see the data structure which get posted in the final call. See below examples which I've tried out. Actual email ids have been masked out.
bcc field doesn't work
In [118]: pprint(m.get())
{'content': [{'type': 'text/plain', 'value': 'This is a test email'}],
'from': {'email': 'abc@mydomain.com', 'name': 'ABC'},
'personalizations': [{'bcc': [{'email': 'xyz@gmail.com'}],
'subject': 'Test email 2',
'to': [{'email': 'def@gmail.com'}]}],
'subject': 'Test email 2'}
In [119]: response = sg.client.mail.send.post(request_body=mail.get())
---------------------------------------------------------------------------
BadRequestsError Traceback (most recent call last)
<ipython-input-119-32b16380a934> in <module>
----> 1 response = sg.client.mail.send.post(request_body=mail.get())
~/env/lib/python3.5/site-packages/python_http_client/client.py in http_request(*_, **kwargs)
250 request.get_method = lambda: method
251 timeout = kwargs.pop('timeout', None)
--> 252 return Response(self._make_request(opener, request, timeout=timeout))
253 return http_request
254 else:
~/env/lib/python3.5/site-packages/python_http_client/client.py in _make_request(self, opener, request, timeout)
174 exc = handle_error(err)
175 exc.__cause__ = None
--> 176 raise exc
177
178 def _(self, name):
BadRequestsError: HTTP Error 400: Bad Request
Note that although the subject field is repeated above in mail object as well as personalization object, I've tried having it only in mail object and only in personalization object as well. It doesn't matter. It's a bit weird that subject can get added to two places in the object even while using helper functions which is not the case with to field. Irrespective of whether you add to field using Mail object constructor or via Personalization.subject, it always correctly adds it under personalization field in the resulting object. That is not the case with subject.
cc does not work
In [127]: pprint(m.get())
{'content': [{'type': 'text/plain', 'value': 'This is a test email'}],
'from': {'email': 'abc@mydomain.com', 'name': 'ABC'},
'personalizations': [{'cc': [{'email': 'abc@mydomain.com',
'name': 'ABC'}],
'to': [{'email': 'xyz@gmail.com'}]}],
'subject': 'Test email 2'}
In [128]: response = sg.client.mail.send.post(request_body=mail.get())
---------------------------------------------------------------------------
BadRequestsError Traceback (most recent call last)
<ipython-input-128-32b16380a934> in <module>
----> 1 response = sg.client.mail.send.post(request_body=mail.get())
~/env/lib/python3.5/site-packages/python_http_client/client.py in http_request(*_, **kwargs)
250 request.get_method = lambda: method
251 timeout = kwargs.pop('timeout', None)
--> 252 return Response(self._make_request(opener, request, timeout=timeout))
253 return http_request
254 else:
~/env/lib/python3.5/site-packages/python_http_client/client.py in _make_request(self, opener, request, timeout)
174 exc = handle_error(err)
175 exc.__cause__ = None
--> 176 raise exc
177
178 def _(self, name):
BadRequestsError: HTTP Error 400: Bad Request
multiple to fields also do not work
In [134]: pprint(m.get())
{'content': [{'type': 'text/plain', 'value': 'This is a test email'}],
'from': {'email': 'abc@mydomain.com', 'name': 'ABC'},
'personalizations': [{'to': [{'email': 'xyz@gmail.com'}]},
{'to': [{'email': 'abc@mydomain.com',
'name': 'ABC'}]}],
'subject': 'Test email 2'}
In [135]: response = sg.client.mail.send.post(request_body=mail.get())
---------------------------------------------------------------------------
BadRequestsError Traceback (most recent call last)
<ipython-input-135-32b16380a934> in <module>
----> 1 response = sg.client.mail.send.post(request_body=mail.get())
~/env/lib/python3.5/site-packages/python_http_client/client.py in http_request(*_, **kwargs)
250 request.get_method = lambda: method
251 timeout = kwargs.pop('timeout', None)
--> 252 return Response(self._make_request(opener, request, timeout=timeout))
253 return http_request
254 else:
~/env/lib/python3.5/site-packages/python_http_client/client.py in _make_request(self, opener, request, timeout)
174 exc = handle_error(err)
175 exc.__cause__ = None
--> 176 raise exc
177
178 def _(self, name):
BadRequestsError: HTTP Error 400: Bad Request
In some cases above the from email id is also added as cc or bcc since I have that requirement. But I've also tried adding some other email id there and it doesn't matter. In all the above cases the response body is empty.
In [136]: response.body
Out[136]: b''
In all the above cases if I remove the the additional cc or bcc or to field from the personalization, I'm able to successfully send the email.
Technical details:
- OS : Ubuntu 16.04
- sendgrid-python Version: 5.6.0
- Python Version: 3.7.0
- Django: 2.1.0
Issue Summary
I'm using sendgrid-python version 5.6.0. I'm able to send emails as long as there are only the following fields in the email
Apart from this any extra field if added to personalization object, I get a HTTP 400 BadRequest. Moreover, the response body is empty so there is no specific error message. I'm using mail helper function but you can clearly see the data structure which get posted in the final call. See below examples which I've tried out. Actual email ids have been masked out.
bcc field doesn't work
Note that although the subject field is repeated above in mail object as well as personalization object, I've tried having it only in mail object and only in personalization object as well. It doesn't matter. It's a bit weird that subject can get added to two places in the object even while using helper functions which is not the case with
tofield. Irrespective of whether you addtofield usingMailobject constructor or viaPersonalization.subject, it always correctly adds it under personalization field in the resulting object. That is not the case withsubject.ccdoes not workmultiple
tofields also do not workIn some cases above the
fromemail id is also added asccorbccsince I have that requirement. But I've also tried adding some other email id there and it doesn't matter. In all the above cases the response body is empty.In all the above cases if I remove the the additional
ccorbccortofield from the personalization, I'm able to successfully send the email.Technical details: