SendTemplatedEmailAWS SDKOR와 함께 사용 CLI - AWS SDK코드 예제

AWS 문서 AWS SDK SDK 예제 GitHub 리포지토리에 더 많은 예제가 있습니다.

기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.

SendTemplatedEmailAWS SDKOR와 함께 사용 CLI

다음 코드 예제는 SendTemplatedEmail의 사용 방법을 보여 줍니다.

작업 예제는 대규모 프로그램에서 발췌한 코드이며 컨텍스트에 맞춰 실행해야 합니다. 다음 코드 예제에서는 컨텍스트 내에서 이 작업을 확인할 수 있습니다.

.NET
AWS SDK for .NET
참고

더 많은 정보가 있습니다 GitHub. AWS 코드 예시 리포지토리에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요.

/// <summary> /// Send an email using a template. /// </summary> /// <param name="sender">Address of the sender.</param> /// <param name="recipients">Addresses of the recipients.</param> /// <param name="templateName">Name of the email template.</param> /// <param name="templateDataObject">Data for the email template.</param> /// <returns>The messageId of the email.</returns> public async Task<string> SendTemplateEmailAsync(string sender, List<string> recipients, string templateName, object templateDataObject) { var messageId = ""; try { // Template data should be serialized JSON from either a class or a dynamic object. var templateData = JsonSerializer.Serialize(templateDataObject); var response = await _amazonSimpleEmailService.SendTemplatedEmailAsync( new SendTemplatedEmailRequest { Source = sender, Destination = new Destination { ToAddresses = recipients }, Template = templateName, TemplateData = templateData }); messageId = response.MessageId; } catch (Exception ex) { Console.WriteLine("SendTemplateEmailAsync failed with exception: " + ex.Message); } return messageId; }
C++
SDKC++의 경우
참고

더 많은 정보가 있습니다. GitHub AWS 코드 예시 리포지토리에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요.

//! Send a templated email to a list of recipients. /*! \param recipients; Vector of recipient email addresses. \param templateName: The name of the template to use. \param templateData: Map of key-value pairs for replacing text in template. \param senderEmailAddress: Email address of sender. Ignored if empty string. \param ccAddresses: Vector of cc addresses. Ignored if empty. \param replyToAddress: Reply to email address. Ignored if empty string. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::SES::sendTemplatedEmail(const Aws::Vector<Aws::String> &recipients, const Aws::String &templateName, const Aws::Map<Aws::String, Aws::String> &templateData, const Aws::String &senderEmailAddress, const Aws::Vector<Aws::String> &ccAddresses, const Aws::String &replyToAddress, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::SES::SESClient sesClient(clientConfiguration); Aws::SES::Model::Destination destination; if (!ccAddresses.empty()) { destination.WithCcAddresses(ccAddresses); } if (!recipients.empty()) { destination.WithToAddresses(recipients); } Aws::SES::Model::SendTemplatedEmailRequest sendTemplatedEmailRequest; sendTemplatedEmailRequest.SetDestination(destination); sendTemplatedEmailRequest.SetTemplate(templateName); std::ostringstream templateDataStream; templateDataStream << "{"; size_t dataCount = 0; for (auto &pair: templateData) { templateDataStream << "\"" << pair.first << "\":\"" << pair.second << "\""; dataCount++; if (dataCount < templateData.size()) { templateDataStream << ","; } } templateDataStream << "}"; sendTemplatedEmailRequest.SetTemplateData(templateDataStream.str()); if (!senderEmailAddress.empty()) { sendTemplatedEmailRequest.SetSource(senderEmailAddress); } if (!replyToAddress.empty()) { sendTemplatedEmailRequest.AddReplyToAddresses(replyToAddress); } auto outcome = sesClient.SendTemplatedEmail(sendTemplatedEmailRequest); if (outcome.IsSuccess()) { std::cout << "Successfully sent templated message with ID " << outcome.GetResult().GetMessageId() << "." << std::endl; } else { std::cerr << "Error sending templated message. " << outcome.GetError().GetMessage() << std::endl; } return outcome.IsSuccess(); }
Java
SDK자바 2.x의 경우
참고

더 많은 내용이 있습니다. GitHub AWS 코드 예시 리포지토리에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요.

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.sesv2.model.Destination; import software.amazon.awssdk.services.sesv2.model.EmailContent; import software.amazon.awssdk.services.sesv2.model.SendEmailRequest; import software.amazon.awssdk.services.sesv2.model.SesV2Exception; import software.amazon.awssdk.services.sesv2.SesV2Client; import software.amazon.awssdk.services.sesv2.model.Template; /** * Before running this AWS SDK for Java (v2) example, set up your development * environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html * * Also, make sure that you create a template. See the following documentation * topic: * * https://docs.aws.amazon.com/ses/latest/dg/send-personalized-email-api.html */ public class SendEmailTemplate { public static void main(String[] args) { final String usage = """ Usage: <template> <sender> <recipient>\s Where: template - The name of the email template. sender - An email address that represents the sender.\s recipient - An email address that represents the recipient.\s """; if (args.length != 3) { System.out.println(usage); System.exit(1); } String templateName = args[0]; String sender = args[1]; String recipient = args[2]; Region region = Region.US_EAST_1; SesV2Client sesv2Client = SesV2Client.builder() .region(region) .build(); send(sesv2Client, sender, recipient, templateName); } public static void send(SesV2Client client, String sender, String recipient, String templateName) { Destination destination = Destination.builder() .toAddresses(recipient) .build(); /* * Specify both name and favorite animal (favoriteanimal) in your code when * defining the Template object. * If you don't specify all the variables in the template, Amazon SES doesn't * send the email. */ Template myTemplate = Template.builder() .templateName(templateName) .templateData("{\n" + " \"name\": \"Jason\"\n," + " \"favoriteanimal\": \"Cat\"\n" + "}") .build(); EmailContent emailContent = EmailContent.builder() .template(myTemplate) .build(); SendEmailRequest emailRequest = SendEmailRequest.builder() .destination(destination) .content(emailContent) .fromEmailAddress(sender) .build(); try { System.out.println("Attempting to send an email based on a template using the AWS SDK for Java (v2)..."); client.sendEmail(emailRequest); System.out.println("email based on a template was sent"); } catch (SesV2Exception e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } } }
JavaScript
SDK JavaScript (v3) 에 대한
참고

더 많은 정보가 있습니다. GitHub AWS 코드 예시 리포지토리에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요.

import { SendTemplatedEmailCommand } from "@aws-sdk/client-ses"; import { getUniqueName, postfix, } from "@aws-doc-sdk-examples/lib/utils/util-string.js"; import { sesClient } from "./libs/sesClient.js"; /** * Replace this with the name of an existing template. */ const TEMPLATE_NAME = getUniqueName("ReminderTemplate"); /** * Replace these with existing verified emails. */ const VERIFIED_EMAIL = postfix(getUniqueName("Bilbo"), "@example.com"); const USER = { firstName: "Bilbo", emailAddress: VERIFIED_EMAIL }; /** * * @param { { emailAddress: string, firstName: string } } user * @param { string } templateName - The name of an existing template in Amazon SES. * @returns { SendTemplatedEmailCommand } */ const createReminderEmailCommand = (user, templateName) => { return new SendTemplatedEmailCommand({ /** * Here's an example of how a template would be replaced with user data: * Template: <h1>Hello {{contact.firstName}},</h1><p>Don't forget about the party gifts!</p> * Destination: <h1>Hello Bilbo,</h1><p>Don't forget about the party gifts!</p> */ Destination: { ToAddresses: [user.emailAddress] }, TemplateData: JSON.stringify({ contact: { firstName: user.firstName } }), Source: VERIFIED_EMAIL, Template: templateName, }); }; const run = async () => { const sendReminderEmailCommand = createReminderEmailCommand( USER, TEMPLATE_NAME, ); try { return await sesClient.send(sendReminderEmailCommand); } catch (caught) { if (caught instanceof Error && caught.name === "MessageRejected") { /** @type { import('@aws-sdk/client-ses').MessageRejected} */ const messageRejectedError = caught; return messageRejectedError; } throw caught; } };
Python
SDK파이썬용 (보토3)
참고

더 많은 정보가 있습니다. GitHub AWS 코드 예시 리포지토리에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요.

class SesMailSender: """Encapsulates functions to send emails with Amazon SES.""" def __init__(self, ses_client): """ :param ses_client: A Boto3 Amazon SES client. """ self.ses_client = ses_client def send_templated_email( self, source, destination, template_name, template_data, reply_tos=None ): """ Sends an email based on a template. A template contains replaceable tags each enclosed in two curly braces, such as {{name}}. The template data passed in this function contains key-value pairs that define the values to insert in place of the template tags. Note: If your account is in the Amazon SES sandbox, the source and destination email accounts must both be verified. :param source: The source email account. :param destination: The destination email account. :param template_name: The name of a previously created template. :param template_data: JSON-formatted key-value pairs of replacement values that are inserted in the template before it is sent. :return: The ID of the message, assigned by Amazon SES. """ send_args = { "Source": source, "Destination": destination.to_service_format(), "Template": template_name, "TemplateData": json.dumps(template_data), } if reply_tos is not None: send_args["ReplyToAddresses"] = reply_tos try: response = self.ses_client.send_templated_email(**send_args) message_id = response["MessageId"] logger.info( "Sent templated mail %s from %s to %s.", message_id, source, destination.tos, ) except ClientError: logger.exception( "Couldn't send templated mail from %s to %s.", source, destination.tos ) raise else: return message_id
  • 자세한 API AWS SDK내용은 Python (Boto3) API 참조를 참조하십시오 SendTemplatedEmail.