The following sending example fails silently if the IP Pool Name does not exist. This is a serious bug, as it can easily go unnoticed.
It fails to send when this happens.
I think the issue may be server-side, and less-so in the client library.
importsendgridfromloguruimportloggerfromsendgrid.helpers.mailimport (
Attachment,
Content,
Email,
IpPoolName,
Mail,
ReplyTo,
Subject,
To,
)
defsend_email(
*,
message_html: str,
subject: str,
to_emails: list[str],
reply_to_email: str|None=None,
attachments: list[Attachment] |None=None,
) ->None:
"""Send email using Sendgrid."""sendgrid_api_key="API_KEY_HERE"sg_api_client=sendgrid.SendGridAPIClient(api_key=sendgrid_api_key)
msg=Mail()
msg.from_email=Email("someone@example.com") # Replace with your own.msg.to= [To(i) foriinto_emails]
msg.subject=Subject(subject)
msg.content=Content(mime_type="text/html", content=message_html)
ifreply_to_emailisnotNone:
msg.reply_to=ReplyTo(reply_to_email)
ifattachments:
msg.attachment=attachments# See doc above for how to craft attachments.# Use our dedicated IP pool to increase trust score.# Note: If you give a bad/wrong value here, sending fails silently. Wacky.msg.ip_pool_name=IpPoolName("ip-pool")
response=sg_api_client.send(msg)
ifresponse.status_code!=202: # 202=Acceptedlogger.warning(
"While sending email, the SendGrid API return a non-success status code "f"({response.status_code}). "f"Error Message: {response.body}. Headers: {response.headers}."
)
The following sending example fails silently if the IP Pool Name does not exist. This is a serious bug, as it can easily go unnoticed.
It fails to send when this happens.
I think the issue may be server-side, and less-so in the client library.