update sendgrid code
This commit is contained in:
+15
-7
@@ -59,20 +59,28 @@ def send_email(to, subject, body, html=False):
|
|||||||
logging.debug(f"body: {body}")
|
logging.debug(f"body: {body}")
|
||||||
return
|
return
|
||||||
|
|
||||||
from_email = Email(DEFAULT_EMAIL_FROM_ADDR)
|
from_email = DEFAULT_EMAIL_FROM_ADDR
|
||||||
to_email = To(to)
|
to_email = to
|
||||||
sub = Subject(subject)
|
sub = subject
|
||||||
|
|
||||||
|
html_content = None
|
||||||
|
plain_text_content = None
|
||||||
if (html):
|
if (html):
|
||||||
content = HtmlContent(body)
|
html_content = HtmlContent(body)
|
||||||
else:
|
else:
|
||||||
content = PlainTextContent(body)
|
plain_text_content = PlainTextContent(body)
|
||||||
|
|
||||||
mail = Mail(from_email, to_email, sub, content)
|
mail = Mail(
|
||||||
|
from_email=from_email,
|
||||||
|
to_emails=to_email,
|
||||||
|
subject=sub,
|
||||||
|
html_content=html_content,
|
||||||
|
plain_text_content=plain_text_content
|
||||||
|
)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
sg = sendgrid.SendGridAPIClient(EMAIL_API_KEY)
|
sg = sendgrid.SendGridAPIClient(EMAIL_API_KEY)
|
||||||
response = sg.client.mail.send.post(request_body=mail.get())
|
response = sg.send(mail)
|
||||||
|
|
||||||
if response.status_code != 202:
|
if response.status_code != 202:
|
||||||
raise Exception(f'Failed to send email: {response.body}')
|
raise Exception(f'Failed to send email: {response.body}')
|
||||||
|
|||||||
Reference in New Issue
Block a user