Hey phil! I am just getting started w/ sendgrid and I have run into a few issues, maybe you could point me in the right direction.
I tried sending email by replicating the given curl example using requests like this:
@anvil.server.http_endpoint('/send')
def send():
a = requests.post(
'https://api.sendgrid.com/v3/mail/send',
data= {"personalizations":[{"to":[{"email":"brian@myradvocate.com","name":"Docs Team"}],
"subject":"Ahoy, World!"}],
"content": [{"type": "text/plain", "value": "Ahoy from Twilio and SendGrid!"}],
"from":{"email":"ahoy@example.com","name":"Hoot the Owl"},
"reply_to":{"email":"ahoy@example.com","name":"Hoot the Owl"}},
headers={"authorization":str("Bearer "+anvil.secrets.get_secret('sendgrid_api_key')), "content-type":"application/json"}
).json()
return a
which returns this error:
{“errors”:[{“field”:null,“message”:“Bad Request”,“help”:null}]}
I saw that sendgrid was available in anvil, so I ran the given python example as so:
@anvil.server.http_endpoint('/sendit')
def sendit():
message = Mail(
from_email='from_email@example.com',
to_emails='brian@myradvocate.com',
subject='Sending with Twilio SendGrid is Fun',
html_content='<strong>and easy to do anywhere, even with Python</strong>')
try:
sg = SendGridAPIClient(anvil.secrets.get_secret('sendgrid_api_key'))
response = sg.send(message)
print(response.status_code)
print(response.body)
print(response.headers)
except Exception as e:
print(e.message)
return message
sadly this was unsuccesful and, in the app logs I found this message:
TypeError: init() got an unexpected keyword argument ‘html_content’
have you or anyone else here successfully sent sendgrid emails via anvil?
*** UPDATE ***
I saw here that the version of sendgrid available in anvil is an older version (5.3.0). makes sense now.