GetTemplate搭配使用 AWS SDK或 CLI - Amazon Simple Email Service

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

GetTemplate搭配使用 AWS SDK或 CLI

以下代码示例演示如何使用 GetTemplate

操作示例是大型程序的代码摘录,必须在上下文中运行。在以下代码示例中,您可以查看此操作的上下文:

C++
SDK对于 C++
注意

还有更多相关信息 GitHub。在中查找完整的示例,学习如何设置和运行 AWS 代码示例存储库

//! Get a template's attributes. /*! \param templateName: The name for the template. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::SES::getTemplate(const Aws::String &templateName, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::SES::SESClient sesClient(clientConfiguration); Aws::SES::Model::GetTemplateRequest getTemplateRequest; getTemplateRequest.SetTemplateName(templateName); Aws::SES::Model::GetTemplateOutcome outcome = sesClient.GetTemplate( getTemplateRequest); if (outcome.IsSuccess()) { std::cout << "Successfully got template." << std::endl; } else { std::cerr << "Error getting template. " << outcome.GetError().GetMessage() << std::endl; } return outcome.IsSuccess(); }
  • 有关API详细信息,请参阅GetTemplate中的 AWS SDK for C++ API参考

JavaScript
SDK对于 JavaScript (v3)
注意

还有更多相关信息 GitHub。在中查找完整的示例,学习如何设置和运行 AWS 代码示例存储库

import { GetTemplateCommand } from "@aws-sdk/client-ses"; import { getUniqueName } from "@aws-doc-sdk-examples/lib/utils/util-string.js"; import { sesClient } from "./libs/sesClient.js"; const TEMPLATE_NAME = getUniqueName("TemplateName"); const createGetTemplateCommand = (templateName) => new GetTemplateCommand({ TemplateName: templateName }); const run = async () => { const getTemplateCommand = createGetTemplateCommand(TEMPLATE_NAME); try { return await sesClient.send(getTemplateCommand); } catch (caught) { if (caught instanceof Error && caught.name === "MessageRejected") { /** @type { import('@aws-sdk/client-ses').MessageRejected} */ const messageRejectedError = caught; return messageRejectedError; } throw caught; } };
  • 有关API详细信息,请参阅GetTemplate中的 AWS SDK for JavaScript API参考

Python
SDK适用于 Python (Boto3)
注意

还有更多相关信息 GitHub。在中查找完整的示例,学习如何设置和运行 AWS 代码示例存储库

class SesTemplate: """Encapsulates Amazon SES template functions.""" def __init__(self, ses_client): """ :param ses_client: A Boto3 Amazon SES client. """ self.ses_client = ses_client self.template = None self.template_tags = set() def _extract_tags(self, subject, text, html): """ Extracts tags from a template as a set of unique values. :param subject: The subject of the email. :param text: The text version of the email. :param html: The html version of the email. """ self.template_tags = set(re.findall(TEMPLATE_REGEX, subject + text + html)) logger.info("Extracted template tags: %s", self.template_tags) def get_template(self, name): """ Gets a previously created email template. :param name: The name of the template to retrieve. :return: The retrieved email template. """ try: response = self.ses_client.get_template(TemplateName=name) self.template = response["Template"] logger.info("Got template %s.", name) self._extract_tags( self.template["SubjectPart"], self.template["TextPart"], self.template["HtmlPart"], ) except ClientError: logger.exception("Couldn't get template %s.", name) raise else: return self.template
  • 有关API详细信息,请参阅GetTemplate中的 AWS SDK供参考 Python (Boto3) API。

有关完整列表 AWS SDK开发者指南和代码示例,请参阅将 Amazon SES 与 AWS 软件开发工具包一起使用。本主题还包括有关入门的信息以及有关先前SDK版本的详细信息。