Gunakan SendTemplatedEmail dengan AWS SDK atau CLI - AWS SDKContoh Kode

Ada lebih banyak AWS SDK contoh yang tersedia di GitHub repo SDKContoh AWS Dokumen.

Terjemahan disediakan oleh mesin penerjemah. Jika konten terjemahan yang diberikan bertentangan dengan versi bahasa Inggris aslinya, utamakan versi bahasa Inggris.

Gunakan SendTemplatedEmail dengan AWS SDK atau CLI

Contoh kode berikut menunjukkan cara menggunakanSendTemplatedEmail.

Contoh tindakan adalah kutipan kode dari program yang lebih besar dan harus dijalankan dalam konteks. Anda dapat melihat tindakan ini dalam konteks dalam contoh kode berikut:

.NET
AWS SDK for .NET
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara pengaturan dan menjalankannya di Repositori Contoh Kode 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++
SDKuntuk C ++
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara pengaturan dan menjalankannya di Repositori Contoh Kode 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
SDKuntuk Java 2.x
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara pengaturan dan menjalankannya di Repositori Contoh Kode 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
SDKuntuk JavaScript (v3)
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara pengaturan dan menjalankannya di Repositori Contoh Kode 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
SDKuntuk Python (Boto3)
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara pengaturan dan menjalankannya di Repositori Contoh Kode 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