Le traduzioni sono generate tramite traduzione automatica. In caso di conflitto tra il contenuto di una traduzione e la versione originale in Inglese, quest'ultima prevarrà.
Utilizzare SendBulkTemplatedEmail
con un AWS SDK
Il seguente esempio di codice mostra come utilizzareSendBulkTemplatedEmail
.
- JavaScript
-
- SDKper JavaScript (v3)
-
import { SendBulkTemplatedEmailCommand } 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_1 = postfix(getUniqueName("Bilbo"), "@example.com");
const VERIFIED_EMAIL_2 = postfix(getUniqueName("Frodo"), "@example.com");
const USERS = [
{ firstName: "Bilbo", emailAddress: VERIFIED_EMAIL_1 },
{ firstName: "Frodo", emailAddress: VERIFIED_EMAIL_2 },
];
/**
*
* @param { { emailAddress: string, firstName: string }[] } users
* @param { string } templateName the name of an existing template in SES
* @returns { SendBulkTemplatedEmailCommand }
*/
const createBulkReminderEmailCommand = (users, templateName) => {
return new SendBulkTemplatedEmailCommand({
/**
* Each 'Destination' uses a corresponding set of replacement data. We can map each user
* to a 'Destination' and provide user specific replacement data to create personalized emails.
*
* Here's an example of how a template would be replaced with user data:
* Template: <h1>Hello {{name}},</h1><p>Don't forget about the party gifts!</p>
* Destination 1: <h1>Hello Bilbo,</h1><p>Don't forget about the party gifts!</p>
* Destination 2: <h1>Hello Frodo,</h1><p>Don't forget about the party gifts!</p>
*/
Destinations: users.map((user) => ({
Destination: { ToAddresses: [user.emailAddress] },
ReplacementTemplateData: JSON.stringify({ name: user.firstName }),
})),
DefaultTemplateData: JSON.stringify({ name: "Shireling" }),
Source: VERIFIED_EMAIL_1,
Template: templateName,
});
};
const run = async () => {
const sendBulkTemplateEmailCommand = createBulkReminderEmailCommand(
USERS,
TEMPLATE_NAME,
);
try {
return await sesClient.send(sendBulkTemplateEmailCommand);
} catch (caught) {
if (caught instanceof Error && caught.name === "MessageRejected") {
/** @type { import('@aws-sdk/client-ses').MessageRejected} */
const messageRejectedError = caught;
return messageRejectedError;
}
throw caught;
}
};
Per un elenco completo delle guide per AWS SDK sviluppatori e degli esempi di codice, consultaUsare Amazon SES con un AWS SDK. Questo argomento include anche informazioni su come iniziare e dettagli sulle SDK versioni precedenti.