Kirim email template ke beberapa tujuan dengan Amazon SES menggunakan SDK AWS - Amazon Simple Email Service

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

Kirim email template ke beberapa tujuan dengan Amazon SES menggunakan SDK AWS

Contoh kode berikut menunjukkan cara mengirim email templated ke beberapa tujuan dengan Amazon SES.

JavaScript
SDK untuk JavaScript (v3)
catatan

Masih ada lagiGitHub. Temukan contoh lengkapnya dan pelajari cara mengatur dan menjalankan di AWSCode Examples Repository.

import { SendBulkTemplatedEmailCommand } from "@aws-sdk/client-ses"; import { getUniqueName, postfix } from "../../libs/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 (err) { console.log("Failed to send bulk template email", err); return err; } };

Untuk daftar lengkap panduan pengembang AWS SDK dan contoh kode, lihatMenggunakan Amazon SES dengan AWS SDK. Topik ini juga mencakup informasi tentang memulai dan detail tentang versi SDK sebelumnya.