View a markdown version of this page

SendBulkEmailMit einem AWS SDK verwenden - AWS SDK-Codebeispiele

Weitere AWS SDK-Beispiele sind im GitHub Repo AWS Doc SDK Examples verfügbar.

Die vorliegende Übersetzung wurde maschinell erstellt. Im Falle eines Konflikts oder eines Widerspruchs zwischen dieser übersetzten Fassung und der englischen Fassung (einschließlich infolge von Verzögerungen bei der Übersetzung) ist die englische Fassung maßgeblich.

SendBulkEmailMit einem AWS SDK verwenden

Das folgende Codebeispiel zeigt, wie es verwendet wirdSendBulkEmail.

Beispiele für Aktionen sind Codeauszüge aus größeren Programmen und müssen im Kontext ausgeführt werden. Im folgenden Codebeispiel können Sie diese Aktion im Kontext sehen:

Python
SDK für Python (Boto3)
Anmerkung

Es gibt noch mehr dazu GitHub. Hier finden Sie das vollständige Beispiel und erfahren, wie Sie das AWS -Code-Beispiel- einrichten und ausführen.

class SESv2Wrapper: """Encapsulates Amazon SESv2 email sending actions.""" def __init__(self, sesv2_client: Any) -> None: """ Initializes the SESv2Wrapper with an SESv2 client. :param sesv2_client: A Boto3 SESv2 client. """ self.sesv2_client = sesv2_client @classmethod def from_client(cls) -> "SESv2Wrapper": """ Creates an SESv2Wrapper instance with a default Boto3 SESv2 client. :return: A new SESv2Wrapper instance. """ sesv2_client = boto3.client("sesv2") return cls(sesv2_client) def send_bulk_email( self, from_address: str, template_name: str, default_template_data: str, bulk_entries: List[Dict[str, Any]], attachments: Optional[List[Dict[str, Any]]] = None, ) -> List[Dict[str, Any]]: """ Sends a templated email to multiple recipients in a single API call. All recipients receive the same attachment(s) defined in the default content, while template data can be personalized per recipient. :param from_address: The verified sender email address. :param template_name: The name of an existing email template. :param default_template_data: Default JSON template data string. :param bulk_entries: A list of BulkEmailEntry dicts, each containing 'Destination' and optionally 'ReplacementEmailContent'. :param attachments: An optional list of attachment dicts for all recipients. :return: A list of BulkEmailEntryResult dicts with status and MessageId. :raises ClientError: If the message is rejected (MessageRejected). """ try: template_content: Dict[str, Any] = { "TemplateName": template_name, "TemplateData": default_template_data, } if attachments: template_content["Attachments"] = attachments response = self.sesv2_client.send_bulk_email( FromEmailAddress=from_address, DefaultContent={"Template": template_content}, BulkEmailEntries=bulk_entries, ) results = response.get("BulkEmailEntryResults", []) logger.info( "Sent bulk email from %s to %d recipients.", from_address, len(bulk_entries), ) return results except ClientError as err: if err.response["Error"]["Code"] == "MessageRejected": logger.error( "Bulk message was rejected. Check that the template " "exists, attachment file types are supported, and " "total message size is within limits. Details: %s", err.response["Error"]["Message"], ) else: logger.error( "Couldn't send bulk email. Here's why: %s: %s", err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise
  • Einzelheiten zur API finden Sie SendBulkEmailin AWS SDK for Python (Boto3) API Reference.