搭UpdateTemplate配 AWS 開發套件或 CLI 使用 - Amazon Simple Email Service

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

UpdateTemplate配 AWS 開發套件或 CLI 使用

下列程式碼範例會示範如何使用UpdateTemplate

動作範例是大型程式的程式碼摘錄,必須在內容中執行。您可以在下列程式碼範例的內容中看到此動作:

C++
適用於 C++ 的 SDK
注意

還有更多關於 GitHub。尋找完整範例,並了解如何在AWS 設定和執行程式碼範例儲存庫

//! Update an Amazon Simple Email Service (Amazon SES) template. /*! \param templateName: The name of the template. \param htmlPart: The HTML body of the email. \param subjectPart: The subject line of the email. \param textPart: The plain text version of the email. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::SES::updateTemplate(const Aws::String &templateName, const Aws::String &htmlPart, const Aws::String &subjectPart, const Aws::String &textPart, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::SES::SESClient sesClient(clientConfiguration); Aws::SES::Model::Template templateValues; templateValues.SetTemplateName(templateName); templateValues.SetSubjectPart(subjectPart); templateValues.SetHtmlPart(htmlPart); templateValues.SetTextPart(textPart); Aws::SES::Model::UpdateTemplateRequest updateTemplateRequest; updateTemplateRequest.SetTemplate(templateValues); Aws::SES::Model::UpdateTemplateOutcome outcome = sesClient.UpdateTemplate(updateTemplateRequest); if (outcome.IsSuccess()) { std::cout << "Successfully updated template." << std::endl; } else { std::cerr << "Error updating template. " << outcome.GetError().GetMessage() << std::endl; } return outcome.IsSuccess(); }
  • 如需 API 詳細資訊,請參閱 AWS SDK for C++ API 參考UpdateTemplate中的。

JavaScript
適用於 JavaScript (v3) 的開發套件
注意

還有更多關於 GitHub。尋找完整範例,並了解如何在AWS 設定和執行程式碼範例儲存庫

import { UpdateTemplateCommand } 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 HTML_PART = "<h1>Hello, World!</h1>"; const createUpdateTemplateCommand = () => { return new UpdateTemplateCommand({ Template: { TemplateName: TEMPLATE_NAME, HtmlPart: HTML_PART, SubjectPart: "Example", TextPart: "Updated template text.", }, }); }; const run = async () => { const updateTemplateCommand = createUpdateTemplateCommand(); try { return await sesClient.send(updateTemplateCommand); } catch (err) { console.log("Failed to update template.", err); return err; } };
  • 如需 API 詳細資訊,請參閱 AWS SDK for JavaScript API 參考UpdateTemplate中的。

Python
適用於 Python (Boto3) 的 SDK
注意

還有更多關於 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 update_template(self, name, subject, text, html): """ Updates a previously created email template. :param name: The name of the template. :param subject: The subject of the email. :param text: The plain text version of the email. :param html: The HTML version of the email. """ try: template = { "TemplateName": name, "SubjectPart": subject, "TextPart": text, "HtmlPart": html, } self.ses_client.update_template(Template=template) logger.info("Updated template %s.", name) self.template = template self._extract_tags(subject, text, html) except ClientError: logger.exception("Couldn't update template %s.", name) raise
  • 如需 API 的詳細資訊,請參閱AWS 開發套件UpdateTemplate中的 Python (博托 3) API 參考。

如需 AWS SDK 開發人員指南和程式碼範例的完整清單,請參閱搭配 AWS 開發套件使用 Amazon SES。此主題也包含有關入門的資訊和舊版 SDK 的詳細資訊。