有一个令人讨厌的问题。我正在使用Django将数据发送到sendGrid模板。但是,当我添加替换项时,会收到HTTP错误400:错误的请求。如果我没有在字典中添加任何内容,则可以按预期的方式通过电子邮件发送模板。因此,我确定问题出在标签格式中,而且我似乎找不到正确的组合
Python
def emailSendGrid(self):
mail = EmailMultiAlternatives(
subject="Your Subject from sendGrid",
body="SendGrid sent This is a simple text email body.",
from_email="anon@test",
to=["dude@test"],
headers={"Reply-To": "anon@test"}
)
# Add template
mail.template_id = '#######################'
# Replace substitutions in sendgrid template
mail.substitutions = {'testTag': 'new content from Django'}
fail_silently = False
mail.send()
SendGrid模板
<html>
<head>
<title></title>
</head>
<body>
<h3>This is a test email template from sendGrid</h3>
<p> -testTag- </p>
</body>
</html>
这应该非常简单,但是我已经尝试过使用我的组合并用Google搜索它而没有决心。
答案 0 :(得分:0)
我最终使用了Django的curl帖子。希望这对以后的人有所帮助。我还发现了一个非常好的curl代码到python,php,R和node.js转换器,这也有很大帮助。 https://curl.trillworks.com/
import requests
headers = {
'authorization': 'Bearer API_KEY',
'Content-Type': 'application/json',
}
testData = 'This is new testing data'
data = '{"personalizations": [{"to": [{"email": "to_email_Address"}], "dynamic_template_data": {"testTag": "My new data"}}],"from": {"email": "from_email"},"subject":"Hello, World!","content": [{"type": "text/plain","value": "Heya!"}] ,"template_id" : "TEMPLAT_ID"}'
对dirkgroten的特殊感谢,向我指出了正确的方向。 https://stackoverflow.com/users/1252711/dirkgroten