sendgrid-python icon indicating copy to clipboard operation
sendgrid-python copied to clipboard

Silent failure if IP Pool Name doesn't exist

Open DeflateAwning opened this issue 1 month ago • 0 comments

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.


import sendgrid
from loguru import logger
from sendgrid.helpers.mail import (
    Attachment,
    Content,
    Email,
    IpPoolName,
    Mail,
    ReplyTo,
    Subject,
    To,
)

def send_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("[email protected]") # Replace with your own.
    msg.to = [To(i) for i in to_emails]
    msg.subject = Subject(subject)
    msg.content = Content(mime_type="text/html", content=message_html)

    if reply_to_email is not None:
        msg.reply_to = ReplyTo(reply_to_email)

    if attachments:
        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)

    if response.status_code != 202:  # 202=Accepted
        logger.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}."
        )

DeflateAwning avatar Dec 09 '25 18:12 DeflateAwning