Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SendGrid v3 with HTML - 400 Bad Request #187

Closed
davegallant opened this issue Jul 4, 2016 · 13 comments
Closed

SendGrid v3 with HTML - 400 Bad Request #187

davegallant opened this issue Jul 4, 2016 · 13 comments
Labels
type: question question directed at the library

Comments

@davegallant
Copy link

davegallant commented Jul 4, 2016

Issue Summary

I've been trying to migrate to v3 API and several of my emails to be sent get BadRequest responses that v2 does not give me (had to revert back). I believe that Json being created for the e-mail is bad as same as here:
sendgrid/sendgrid-csharp#256

Technical details:

  • sendgrid-python Version: 3.0.0
  • Python Version: 3.5.1+
@exp0nge
Copy link

exp0nge commented Jul 4, 2016

I'm having the same error pop up for me while trying to use a template. Should I make another issue?

Here's my code snippet:

def send_welcome_email(user_email, user_name):
    """
    Sends a thank you email
    :param user_email: str
    :param user_name: str
    :return:
    """
    sg = sendgrid.SendGridAPIClient(apikey=os.environ.get('SENDGRID_API_KEY'))
    from_email = sendgrid.Email(email=FROM_EMAIL, name='Me')
    to_email = sendgrid.Email(email=user_email, name=user_name)
    mail = Mail(from_email=from_email, subject='Welcome!', to_email=to_email)
    personalization = Personalization()
    personalization.add_substitution(Substitution(key=':name', value=user_name))
    mail.add_personalization(personalization)
    mail.set_template_id(WELCOME_TEMPLATE_ID)
    response = sg.client.mail.send.post(request_body=mail.get())
    print response

@thinkingserious
Copy link
Contributor

@Davey-Dev,

Could you please provide the code that you are having trouble with? Or at least the return value from mail.get()? Thanks!

@exp0nge,

When you use the Mail constructor, we build the personalization object for you, so to add to it, you would do this: https://github.com/sendgrid/sendgrid-python/blob/master/examples/helpers/mail/mail_example.py#L16

What you are doing is creating a new personalization object. If you print out the value of mail.get() you will see what I mean.

Thanks!

@exp0nge
Copy link

exp0nge commented Jul 4, 2016

@thinkingserious Thanks. I had to add a Content object to get the initialization to work because of if from_email and subject and to_email and content:

Here's the code snippet for those that stumble upon this:

def send_welcome_email(user_email, user_name):
    """
    Sends a welcome email
    :param user_email: str
    :param user_name: str
    :return:
    """
    sg = sendgrid.SendGridAPIClient(apikey=os.environ.get('SENDGRID_API_KEY'))
    from_email = sendgrid.Email(email=FROM_EMAIL, name='Gleam')
    to_email = sendgrid.Email(email=user_email, name=user_name)
    content = Content('text/html', 'text')
    mail = Mail(from_email=from_email, subject='Welcome!', to_email=to_email, content=content)
    mail.personalizations[0].add_substitution(Substitution(key=':name', value=user_name))
    mail.set_template_id(WELCOME_TEMPLATE_ID)
    sg.client.mail.send.post(request_body=mail.get())

@davegallant
Copy link
Author

davegallant commented Jul 4, 2016

The following codes gives me: HTTP Error 400: Bad Request

def send_notification(recipient, first_name, deal):

    sg = sendgrid.SendGridAPIClient(apikey=os.environ.get('SENDGRID_API_KEY'))

    from_email = Email('Notify <notify@example.com>')
    get_subject = str(deal['title'])
    subject = get_subject
    to_email = Email(recipient)
    created_email = create_email(recipient, first_name, deal)
    content = Content("text/html", created_email)

    mail = Mail(from_email, subject, to_email, content)

    try:
        response = sg.client.mail.send.post(request_body=mail.get())
    except Exception as e:
        print(str(e))
        print(created_email)
        sys.exit(1)

@thinkingserious
Copy link
Contributor

Hi @Davey-Dev,

For the exception, can you tell me what print(e.read()) reveals?

Also, can you do print mail.get() before you make the post and let me know what that reveals?

Thanks!

@davegallant
Copy link
Author

davegallant commented Jul 5, 2016

{'subject': '[MERGED] Discount magazines (Wired, Popular Science, Motor Trend, etc.) $3 to $6/year', 'personalizations': [{'to': [{'email': 'test@mailinator.com'}]}], 'content': [{'value': '\n <html><head></head><body style="font-size:18px">\n <h2>[MERGED] Discount magazines (Wired, Popular Science, Motor Trend, etc.) $3 to $6/year</h2>\n \n <p>User: <strong>mstrouble</strong>\n </p><p>First Posted: <strong>Jun 4th, 2010\xa0 07:48 AM</strong></p><p><a href="http://example.com/merged-discount-magazines-wired-popular-science-motor-trend-etc-3-6-year-898259/">[SOURCE]</a></p>\n <div style="border: 1px solid black;border-radius: 5px; background-color: #c9d2e3">\n <div style="margin: 10px;">\r\n\t\t\t\tMany thanks to Pathfinder35, who put together this FAQ section. </body></html>', 'type': 'text/html'}], 'from': {'email': 'Notify <notify@example.com>'}}

b'{"errors":[{"message":"The subject must not be longer than 78 characters.","field":"subject","help":"http://sendgrid.com/docs/API_Reference/Web_API_v3/Mail/errors.html#message.subject"}]}'

Looks like v2 wasn't truncating or throwing exceptions. I have a message that was sent with an 84 character count subject.

@thinkingserious thinkingserious added the type: question question directed at the library label Jul 5, 2016
@thinkingserious
Copy link
Contributor

Hello @Davey-Dev,

Yes, that is a new behavior for v3.

The Internet Message Format RFC recommends that subject lines be kept less than 78 characters, so to help provide better delivery while promoting best practices, that limit was imposed through the API.

Thank you for following up!

@davidkaminsky
Copy link

ergdfgfdg

@iMerica
Copy link

iMerica commented Feb 20, 2019

Hi @thinkingserious,

I'm a new customer and I just want to express how disappointed I am the quality of the Sendgrid's Client SDKs, Server responses and in general – the poor decision making theme I see across your developer-facing technologies.

I decided this evening to stop tip toeing around Python SDK bugs and write my own thin API client using the requests library. Lo and behold even speaking directly to your REST API is a crummy user experience because your server errors are very unhelpful.

Here is an actual raw JSON response I just got from your API:

{"errors":[{"message":"Bad Request","field":null,"help":null}]}

What am I supposed to do with an error response like that? I'm sorry, but this is unacceptable.

My only suggestion is to invest some time studying other popular REST APIs to see what makes them great. A good example is Stripe's Python Client SDK as well as the actual REST API responses for error states.

Thanks,

Michael

@96f3e4c
Copy link

96f3e4c commented May 17, 2019

I'm using sendgrid-python(sendgrid) 6.0.5 and still getting this response

{"errors":[{"message":"Bad Request","field":null,"help":null}]}

Turns out the substitution values need to be a string as mentioned here

In my case, I have numbers as a substitution value. Fixed by just adding Substitution(tag, str(value))

But, the error response is not helpful

@thinkingserious
Copy link
Contributor

My apologies @iMerica, @RamKrish2079 and any others effected by these non-useful error messages. It looks like a PR was created to update our docs for this and I failed to include it in our last release. Please take a look and let me know if this helps: https://github.com/sendgrid/sendgrid-python/blob/master/TROUBLESHOOTING.md#error

With Best Regards,

Elmer

@davidbernat
Copy link

@Davey-Dev,

Could you please provide the code that you are having trouble with? Or at least the return value from mail.get()? Thanks!

@exp0nge,

When you use the Mail constructor, we build the personalization object for you, so to add to it, you would do this: https://github.com/sendgrid/sendgrid-python/blob/master/examples/helpers/mail/mail_example.py#L16

What you are doing is creating a new personalization object. If you print out the value of mail.get() you will see what I mean.

Thanks!

This link is broken. Arrived via Google. Thanks.

@thinkingserious
Copy link
Contributor

I believe this is the correct link:

"personalizations": [

Thank you for the heads up @davidbernat! If you are having issues, please open a new issue. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: question question directed at the library
Projects
None yet
Development

No branches or pull requests

7 participants