sendgrid / sendgrid-python

The Official Twilio SendGrid Python API Library
https://sendgrid.com
MIT License
1.53k stars 711 forks source link

400 Error Bad Request Dynamic Templates with Substitutions not working #907

Closed akaushik759 closed 4 years ago

akaushik759 commented 4 years ago

Issue Summary

I am trying to send multiple emails using a Dynamic Template, with each email having their own unique substitutions using the Mail Helper class. If I don't include the substitutions then the code works perfectly and mails are delivered, but as soon as I include substitution I get a "HTTP Error 400: Bad Request". I am using Virtual Environment and running it in my Flask app. I have installed sendgrid using "pip install sendgrid" and its updated to the latest version.

Code Snippet

to_emails = [
        To(
            email='person1@gmail.com',
            name='person1',
            substitutions={
                '-unsubscribeLink-':'https://www.xyz.com'
            }
            ),
        To(
            email='person2@gmail.com',
            name='person2',
            substitutions={
                '-unsubscribeLink-':'https://www.abc.com'
            }
            ),
    ]

    message = Mail(
                from_email=From('support@website.com','Website'),
                to_emails=to_emails,
                is_multiple=True)

    message.template_id = 'd-c8d1a24b4539459b831407e2562045a9'
    try:
        sg = SendGridAPIClient('API KEY')
        response = sg.send(message)
        print(response.status_code)
        print(response.body)
        print(response.headers)
    except Exception as e:
        print(str(e))

Exception/Log

HTTP Error 400: Bad Request

Technical details:

I tried upgrading and this is what it prints

pip install sendgrid --upgrade

Requirement already up-to-date: sendgrid in ./p37ve/lib/python3.6/site-packages (6.3.1)
Requirement already satisfied, skipping upgrade: python-http-client>=3.2.1 in ./p37ve/lib/python3.6/site-packages (from sendgrid) (3.2.7)
eshanholtz commented 4 years ago

It looks like this issue may have been fixed in #908. You can check by installing this library locally, or wait until the fix is rolled out in the next release on 2020-06-24

childish-sambino commented 4 years ago

I don't think that will help. Best to log the error body response to see a more detailed reason for the failure. Example here: https://github.com/sendgrid/sendgrid-python/blob/master/use_cases/error_handling.md

akaushik759 commented 4 years ago

I don't think that will help. Best to log the error body response to see a more detailed reason for the failure. Example here: https://github.com/sendgrid/sendgrid-python/blob/master/use_cases/error_handling.md

I did that and here is the response

b'{"errors":[{"message":"Substitutions may not be used with dynamic templating","field":"personalizations.0.substitutions","help":"http://sendgrid.com/docs/API_Reference/Web_API_v3/Mail/errors.html#message.personalizations.substitutions"},{"message":"Substitutions may not be used with dynamic templating","field":"personalizations.1.substitutions","help":"http://sendgrid.com/docs/API_Reference/Web_API_v3/Mail/errors.html#message.personalizations.substitutions"}]}'

@eshanholtz I tried the #908 fix, but it still throws up error, not sure what I am doing wrong. to_emails = [ To(email='test+to0@example.com', name='Example Name 0', dynamic_template_data={ 'name': 'Dynamic Name 0', 'url': 'https://example.com/test0', }, subject='Override Global Subject'), To(email='test+to1@example.com', name='Example Name 1', dynamic_template_data={ 'name': 'Dynamic Name 1', 'url': 'https://example.com/test1', }), ]

I got another exception for this :

TypeError: __init__() got an unexpected keyword argument 'dynamic_template_data'

Will the fix for this specific issue (substitution support for dynamic templates) be released on 2020-06-24?

childish-sambino commented 4 years ago

The fix will be to switch from using substitutions to dynamic_template_data in your code. Support for doing this via the To() initializer will be released on 2020-06-24.

keatbro commented 3 years ago

Was support for the To() initializer actually released? I'm trying a similar setup to this initial issue and running into an error Substitutions may not be used with dynamic templating. I'm using the latest SendGrid python package