Sendgrid-python: Bad Request with v3 Mail API

Created on 19 Oct 2016  路  6Comments  路  Source: sendgrid/sendgrid-python

Issue Summary

sending email with 400 return, no further information help debug, looking forward for any helpful info.

Steps to Reproduce

The only info I got is (400, 'Bad Request')
Here is the result of mail.get()

{
    'reply_to': {'email': '[email protected]'},
    'personalizations': [{'to': [{'email': u'[email protected]'}]}, {'bcc': [{'email': '[email protected]'}]}],
    'from': {'email': '[email protected]'},
    'subject': u'[LocalCache] Welcome to LocalCache',
    'content' : [{'type': 'text/plain', 'value': u'Hi Mission Liao,\n\nWelcome to LocalCache! Please take a second to confirm [email protected] as your email address by clicking this link:\n\nhttp://localhost:8080/other-api/confirm-email?code=SSZZCIQW8NGBMW6Z&email=missionaryliao%40gmail.com\n\nHigh fives,\n\nLocalCache t eam\nhttps://www.localcache.co'}]
}

note:

Technical details:

  • sendgrid-python Version: 3.6.0
  • Python Version: 2.7.8
question

All 6 comments

I finally make it passed: the reply_to and bcc has duplicated email. Once remove the "bcc" part it worked.

The error is throw by urllib2.open, in the form of HTTPError, there are only "code" and "reaon" property in that exception class, in my case, they are (400, "Bad Request"), which is not very helpful.

Is there any way for python user to receive more error information?

thanks for pointing out this document, after I tried e.read(), got this:

{
    "errors": [
        {
            "message":"The to array is required for all personalization objects, and must have at least one email object with a valid email address.",
            "field":"personalizations.1.to",
            "help":"http://sendgrid.com/docs/API_Reference/Web_API_v3/Mail/errors.html#message.personalizations.to"
        }
    ]
}

Although I don't why it reports "to" personalization object when the request contains one, but it's much better than nothing, thanks.

import sendgrid
from sendgrid.helpers.mail import *
import traceback
import urllib
from sendgrid.helpers.mail import Mail, Email, Content

class SendMail():
def send_mail(self, to_email, from_email, subject, message, bcc, cc, name):
try:
print(cc)
sg = sendgrid.SendGridAPIClient(apikey='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')
From_Email = Email(from_email,name)
To_Email = Email(to_email)
Subject = subject
content = Content("text/html", message)
mail = Mail(From_Email, Subject, To_Email, content)
personalization = Personalization()
mail_list_cc = []
mail_list_bcc = []
mail_list_bcc.append(bcc)
mail_list_cc.append(cc)
personalization.add_bcc(Email(bcc))
personalization.add_cc(Email(cc))
mail.add_personalization(personalization)
print(mail.get())
response = sg.client.mail.send.post(request_body=mail.get())
print(response)
except Exception as error:
print('Exception in send_mail...' + str(error))
print(traceback.format_exc().splitlines())

if name == 'main':
sm = SendMail()
sm.send_mail(''[email protected]'',''[email protected]'', 'Testing_Sendgrid','Hey this is xyz from XYZ', ''[email protected]' ',''[email protected]'','qwerty')

and my mail.get() is:

{'subject': 'Testing_Sendgrid', 'from': {'email': ''[email protected]'', 'name': 'qwerty'}, 'personalizations': [{'to': [{'email': ''[email protected]''}]}, {'bcc': [{'email': ''[email protected]''}], 'cc': [{'email': '[email protected]'}]}], 'content': [{'value': 'Hey this is sagar from digicollect', 'type': 'text/html'}]}

and i get this error

HTTP Error 400: Bad Request

can someone guide me through this

@sachin2811,

Please see my response on the other thread.

@thinkingserious Is it mandatory to add To for sending email to BCC list? because i am getting
this BadRequestError while sending without To in code. thanks

Was this page helpful?
0 / 5 - 0 ratings