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

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

Closed
akaushik759 opened this issue Jun 12, 2020 · 5 comments
Closed
Labels
status: waiting for feedback waiting for feedback from the submitter type: question question directed at the library

Comments

@akaushik759
Copy link

akaushik759 commented Jun 12, 2020

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='[email protected]',
    		name='person1',
    		substitutions={
    			'-unsubscribeLink-':'https://www.xyz.com'
    		}
    		),
    	To(
    		email='[email protected]',
    		name='person2',
    		substitutions={
    			'-unsubscribeLink-':'https://www.abc.com'
    		}
    		),
    ]

	message = Mail(
    			from_email=From('[email protected]','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:

  • sendgrid-python version: I had installed using "pip install sendgrid" and its version is 6.3.1, so I don't think I am using "sendgrid-python".
  • python version: Python 3.6.7

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
Copy link
Contributor

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

@eshanholtz eshanholtz added status: waiting for feedback waiting for feedback from the submitter type: twilio enhancement feature request on Twilio's roadmap labels Jun 17, 2020
@childish-sambino
Copy link
Contributor

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

@childish-sambino childish-sambino added type: question question directed at the library and removed type: twilio enhancement feature request on Twilio's roadmap labels Jun 17, 2020
@akaushik759
Copy link
Author

akaushik759 commented Jun 20, 2020

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='[email protected]', name='Example Name 0', dynamic_template_data={ 'name': 'Dynamic Name 0', 'url': 'https://example.com/test0', }, subject='Override Global Subject'), To(email='[email protected]', 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
Copy link
Contributor

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
Copy link

keatbro commented Feb 13, 2021

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

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

No branches or pull requests

4 participants