SESEsempi di Amazon che utilizzano SDK for JavaScript (v3) - AWS SDK for JavaScript

La guida API di riferimento AWS SDK for JavaScript V3 descrive in dettaglio tutte le API operazioni per la AWS SDK for JavaScript versione 3 (V3).

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à.

SESEsempi di Amazon che utilizzano SDK for JavaScript (v3)

I seguenti esempi di codice mostrano come eseguire azioni e implementare scenari comuni utilizzando AWS SDK for JavaScript (v3) con AmazonSES.

Le operazioni sono estratti di codice da programmi più grandi e devono essere eseguite nel contesto. Sebbene le operazioni mostrino come richiamare le singole funzioni del servizio, è possibile visualizzarle contestualizzate negli scenari correlati e negli esempi tra servizi.

Scenari: esempi di codice che mostrano come eseguire un'attività specifica richiamando più funzioni all'interno dello stesso servizio.

Ogni esempio include un collegamento a GitHub, dove puoi trovare istruzioni su come configurare ed eseguire il codice nel contesto.

Argomenti

Azioni

Il seguente esempio di codice mostra come utilizzareCreateReceiptFilter.

SDKper JavaScript (v3)
Nota

C'è di più su. GitHub Trova l'esempio completo e scopri di più sulla configurazione e l'esecuzione nel Repository di esempi di codice AWS.

import { CreateReceiptFilterCommand, ReceiptFilterPolicy, } from "@aws-sdk/client-ses"; import { sesClient } from "./libs/sesClient.js"; import { getUniqueName } from "@aws-doc-sdk-examples/lib/utils/util-string.js"; const createCreateReceiptFilterCommand = ({ policy, ipOrRange, name }) => { return new CreateReceiptFilterCommand({ Filter: { IpFilter: { Cidr: ipOrRange, // string, either a single IP address (10.0.0.1) or an IP address range in CIDR notation (10.0.0.1/24)). Policy: policy, // enum ReceiptFilterPolicy, email traffic from the filtered addressesOptions. }, /* The name of the IP address filter. Only ASCII letters, numbers, underscores, or dashes. Must be less than 64 characters and start and end with a letter or number. */ Name: name, }, }); }; const FILTER_NAME = getUniqueName("ReceiptFilter"); const run = async () => { const createReceiptFilterCommand = createCreateReceiptFilterCommand({ policy: ReceiptFilterPolicy.Allow, ipOrRange: "10.0.0.1", name: FILTER_NAME, }); try { return await sesClient.send(createReceiptFilterCommand); } catch (caught) { if (caught instanceof Error && caught.name === "MessageRejected") { /** @type { import('@aws-sdk/client-ses').MessageRejected} */ const messageRejectedError = caught; return messageRejectedError; } throw caught; } };

Il seguente esempio di codice mostra come utilizzareCreateReceiptRule.

SDKper JavaScript (v3)
Nota

C'è di più su. GitHub Trova l'esempio completo e scopri di più sulla configurazione e l'esecuzione nel Repository di esempi di codice AWS.

import { CreateReceiptRuleCommand, TlsPolicy } from "@aws-sdk/client-ses"; import { sesClient } from "./libs/sesClient.js"; import { getUniqueName } from "@aws-doc-sdk-examples/lib/utils/util-string.js"; const RULE_SET_NAME = getUniqueName("RuleSetName"); const RULE_NAME = getUniqueName("RuleName"); const S3_BUCKET_NAME = getUniqueName("S3BucketName"); const createS3ReceiptRuleCommand = ({ bucketName, emailAddresses, name, ruleSet, }) => { return new CreateReceiptRuleCommand({ Rule: { Actions: [ { S3Action: { BucketName: bucketName, ObjectKeyPrefix: "email", }, }, ], Recipients: emailAddresses, Enabled: true, Name: name, ScanEnabled: false, TlsPolicy: TlsPolicy.Optional, }, RuleSetName: ruleSet, // Required }); }; const run = async () => { const s3ReceiptRuleCommand = createS3ReceiptRuleCommand({ bucketName: S3_BUCKET_NAME, emailAddresses: ["email@example.com"], name: RULE_NAME, ruleSet: RULE_SET_NAME, }); try { return await sesClient.send(s3ReceiptRuleCommand); } catch (err) { console.log("Failed to create S3 receipt rule.", err); throw err; } };

Il seguente esempio di codice mostra come utilizzareCreateReceiptRuleSet.

SDKper JavaScript (v3)
Nota

C'è di più su. GitHub Trova l'esempio completo e scopri di più sulla configurazione e l'esecuzione nel Repository di esempi di codice AWS.

import { CreateReceiptRuleSetCommand } from "@aws-sdk/client-ses"; import { sesClient } from "./libs/sesClient.js"; import { getUniqueName } from "@aws-doc-sdk-examples/lib/utils/util-string.js"; const RULE_SET_NAME = getUniqueName("RuleSetName"); const createCreateReceiptRuleSetCommand = (ruleSetName) => { return new CreateReceiptRuleSetCommand({ RuleSetName: ruleSetName }); }; const run = async () => { const createReceiptRuleSetCommand = createCreateReceiptRuleSetCommand(RULE_SET_NAME); try { return await sesClient.send(createReceiptRuleSetCommand); } catch (err) { console.log("Failed to create receipt rule set", err); return err; } };

Il seguente esempio di codice mostra come utilizzareCreateTemplate.

SDKper JavaScript (v3)
Nota

C'è di più su. GitHub Trova l'esempio completo e scopri di più sulla configurazione e l'esecuzione nel Repository di esempi di codice AWS.

import { CreateTemplateCommand } from "@aws-sdk/client-ses"; import { sesClient } from "./libs/sesClient.js"; import { getUniqueName } from "@aws-doc-sdk-examples/lib/utils/util-string.js"; const TEMPLATE_NAME = getUniqueName("TestTemplateName"); const createCreateTemplateCommand = () => { return new CreateTemplateCommand({ /** * The template feature in Amazon SES is based on the Handlebars template system. */ Template: { /** * The name of an existing template in Amazon SES. */ TemplateName: TEMPLATE_NAME, HtmlPart: ` <h1>Hello, {{contact.firstName}}!</h1> <p> Did you know Amazon has a mascot named Peccy? </p> `, SubjectPart: "Amazon Tip", }, }); }; const run = async () => { const createTemplateCommand = createCreateTemplateCommand(); try { return await sesClient.send(createTemplateCommand); } catch (err) { console.log("Failed to create template.", err); return err; } };

Il seguente esempio di codice mostra come utilizzareDeleteIdentity.

SDKper JavaScript (v3)
Nota

C'è di più su. GitHub Trova l'esempio completo e scopri di più sulla configurazione e l'esecuzione nel Repository di esempi di codice AWS.

import { DeleteIdentityCommand } from "@aws-sdk/client-ses"; import { sesClient } from "./libs/sesClient.js"; const IDENTITY_EMAIL = "fake@example.com"; const createDeleteIdentityCommand = (identityName) => { return new DeleteIdentityCommand({ Identity: identityName, }); }; const run = async () => { const deleteIdentityCommand = createDeleteIdentityCommand(IDENTITY_EMAIL); try { return await sesClient.send(deleteIdentityCommand); } catch (err) { console.log("Failed to delete identity.", err); return err; } };

Il seguente esempio di codice mostra come utilizzareDeleteReceiptFilter.

SDKper JavaScript (v3)
Nota

C'è di più su. GitHub Trova l'esempio completo e scopri di più sulla configurazione e l'esecuzione nel Repository di esempi di codice AWS.

import { DeleteReceiptFilterCommand } from "@aws-sdk/client-ses"; import { sesClient } from "./libs/sesClient.js"; import { getUniqueName } from "@aws-doc-sdk-examples/lib/utils/util-string.js"; const RECEIPT_FILTER_NAME = getUniqueName("ReceiptFilterName"); const createDeleteReceiptFilterCommand = (filterName) => { return new DeleteReceiptFilterCommand({ FilterName: filterName }); }; const run = async () => { const deleteReceiptFilterCommand = createDeleteReceiptFilterCommand(RECEIPT_FILTER_NAME); try { return await sesClient.send(deleteReceiptFilterCommand); } catch (err) { console.log("Error deleting receipt filter.", err); return err; } };

Il seguente esempio di codice mostra come utilizzareDeleteReceiptRule.

SDKper JavaScript (v3)
Nota

C'è di più su. GitHub Trova l'esempio completo e scopri di più sulla configurazione e l'esecuzione nel Repository di esempi di codice AWS.

import { DeleteReceiptRuleCommand } from "@aws-sdk/client-ses"; import { getUniqueName } from "@aws-doc-sdk-examples/lib/utils/util-string.js"; import { sesClient } from "./libs/sesClient.js"; const RULE_NAME = getUniqueName("RuleName"); const RULE_SET_NAME = getUniqueName("RuleSetName"); const createDeleteReceiptRuleCommand = () => { return new DeleteReceiptRuleCommand({ RuleName: RULE_NAME, RuleSetName: RULE_SET_NAME, }); }; const run = async () => { const deleteReceiptRuleCommand = createDeleteReceiptRuleCommand(); try { return await sesClient.send(deleteReceiptRuleCommand); } catch (err) { console.log("Failed to delete receipt rule.", err); return err; } };

Il seguente esempio di codice mostra come utilizzareDeleteReceiptRuleSet.

SDKper JavaScript (v3)
Nota

C'è di più su. GitHub Trova l'esempio completo e scopri di più sulla configurazione e l'esecuzione nel Repository di esempi di codice AWS.

import { DeleteReceiptRuleSetCommand } from "@aws-sdk/client-ses"; import { getUniqueName } from "@aws-doc-sdk-examples/lib/utils/util-string.js"; import { sesClient } from "./libs/sesClient.js"; const RULE_SET_NAME = getUniqueName("RuleSetName"); const createDeleteReceiptRuleSetCommand = () => { return new DeleteReceiptRuleSetCommand({ RuleSetName: RULE_SET_NAME }); }; const run = async () => { const deleteReceiptRuleSetCommand = createDeleteReceiptRuleSetCommand(); try { return await sesClient.send(deleteReceiptRuleSetCommand); } catch (err) { console.log("Failed to delete receipt rule set.", err); return err; } };

Il seguente esempio di codice mostra come utilizzareDeleteTemplate.

SDKper JavaScript (v3)
Nota

C'è di più su. GitHub Trova l'esempio completo e scopri di più sulla configurazione e l'esecuzione nel Repository di esempi di codice AWS.

import { DeleteTemplateCommand } 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 createDeleteTemplateCommand = (templateName) => new DeleteTemplateCommand({ TemplateName: templateName }); const run = async () => { const deleteTemplateCommand = createDeleteTemplateCommand(TEMPLATE_NAME); try { return await sesClient.send(deleteTemplateCommand); } catch (err) { console.log("Failed to delete template.", err); return err; } };

Il seguente esempio di codice mostra come utilizzareGetTemplate.

SDKper JavaScript (v3)
Nota

C'è di più su. GitHub Trova l'esempio completo e scopri di più sulla configurazione e l'esecuzione nel Repository di esempi di codice AWS.

import { GetTemplateCommand } 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 createGetTemplateCommand = (templateName) => new GetTemplateCommand({ TemplateName: templateName }); const run = async () => { const getTemplateCommand = createGetTemplateCommand(TEMPLATE_NAME); try { return await sesClient.send(getTemplateCommand); } catch (caught) { if (caught instanceof Error && caught.name === "MessageRejected") { /** @type { import('@aws-sdk/client-ses').MessageRejected} */ const messageRejectedError = caught; return messageRejectedError; } throw caught; } };

Il seguente esempio di codice mostra come utilizzareListIdentities.

SDKper JavaScript (v3)
Nota

C'è di più su. GitHub Trova l'esempio completo e scopri di più sulla configurazione e l'esecuzione nel Repository di esempi di codice AWS.

import { ListIdentitiesCommand } from "@aws-sdk/client-ses"; import { sesClient } from "./libs/sesClient.js"; const createListIdentitiesCommand = () => new ListIdentitiesCommand({ IdentityType: "EmailAddress", MaxItems: 10 }); const run = async () => { const listIdentitiesCommand = createListIdentitiesCommand(); try { return await sesClient.send(listIdentitiesCommand); } catch (err) { console.log("Failed to list identities.", err); return err; } };

Il seguente esempio di codice mostra come utilizzareListReceiptFilters.

SDKper JavaScript (v3)
Nota

C'è di più su. GitHub Trova l'esempio completo e scopri di più sulla configurazione e l'esecuzione nel Repository di esempi di codice AWS.

import { ListReceiptFiltersCommand } from "@aws-sdk/client-ses"; import { sesClient } from "./libs/sesClient.js"; const createListReceiptFiltersCommand = () => new ListReceiptFiltersCommand({}); const run = async () => { const listReceiptFiltersCommand = createListReceiptFiltersCommand(); return await sesClient.send(listReceiptFiltersCommand); };

Il seguente esempio di codice mostra come utilizzareListTemplates.

SDKper JavaScript (v3)
Nota

C'è di più su. GitHub Trova l'esempio completo e scopri di più sulla configurazione e l'esecuzione nel Repository di esempi di codice AWS.

import { ListTemplatesCommand } from "@aws-sdk/client-ses"; import { sesClient } from "./libs/sesClient.js"; const createListTemplatesCommand = (maxItems) => new ListTemplatesCommand({ MaxItems: maxItems }); const run = async () => { const listTemplatesCommand = createListTemplatesCommand(10); try { return await sesClient.send(listTemplatesCommand); } catch (err) { console.log("Failed to list templates.", err); return err; } };

Il seguente esempio di codice mostra come utilizzareSendBulkTemplatedEmail.

SDKper JavaScript (v3)
Nota

C'è di più su. GitHub Trova l'esempio completo e scopri di più sulla configurazione e l'esecuzione nel Repository di esempi di codice AWS.

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; } };

Il seguente esempio di codice mostra come utilizzareSendEmail.

SDKper JavaScript (v3)
Nota

C'è di più su. GitHub Trova l'esempio completo e scopri di più sulla configurazione e l'esecuzione nel Repository di esempi di codice AWS.

import { SendEmailCommand } from "@aws-sdk/client-ses"; import { sesClient } from "./libs/sesClient.js"; const createSendEmailCommand = (toAddress, fromAddress) => { return new SendEmailCommand({ Destination: { /* required */ CcAddresses: [ /* more items */ ], ToAddresses: [ toAddress, /* more To-email addresses */ ], }, Message: { /* required */ Body: { /* required */ Html: { Charset: "UTF-8", Data: "HTML_FORMAT_BODY", }, Text: { Charset: "UTF-8", Data: "TEXT_FORMAT_BODY", }, }, Subject: { Charset: "UTF-8", Data: "EMAIL_SUBJECT", }, }, Source: fromAddress, ReplyToAddresses: [ /* more items */ ], }); }; const run = async () => { const sendEmailCommand = createSendEmailCommand( "recipient@example.com", "sender@example.com", ); try { return await sesClient.send(sendEmailCommand); } catch (caught) { if (caught instanceof Error && caught.name === "MessageRejected") { /** @type { import('@aws-sdk/client-ses').MessageRejected} */ const messageRejectedError = caught; return messageRejectedError; } throw caught; } };

Il seguente esempio di codice mostra come utilizzareSendRawEmail.

SDKper JavaScript (v3)
Nota

C'è di più su. GitHub Trova l'esempio completo e scopri di più sulla configurazione e l'esecuzione nel Repository di esempi di codice AWS.

Usare nodemailer per inviare un'e-mail con un allegato.

import sesClientModule from "@aws-sdk/client-ses"; /** * nodemailer wraps the SES SDK and calls SendRawEmail. Use this for more advanced * functionality like adding attachments to your email. * * https://nodemailer.com/transports/ses/ */ import nodemailer from "nodemailer"; /** * @param {string} from An Amazon SES verified email address. * @param {*} to An Amazon SES verified email address. */ export const sendEmailWithAttachments = ( from = "from@example.com", to = "to@example.com", ) => { const ses = new sesClientModule.SESClient({}); const transporter = nodemailer.createTransport({ SES: { ses, aws: sesClientModule }, }); return new Promise((resolve, reject) => { transporter.sendMail( { from, to, subject: "Hello World", text: "Greetings from Amazon SES!", attachments: [{ content: "Hello World!", filename: "hello.txt" }], }, (err, info) => { if (err) { reject(err); } else { resolve(info); } }, ); }); };

Il seguente esempio di codice mostra come utilizzareSendTemplatedEmail.

SDKper JavaScript (v3)
Nota

C'è di più su. GitHub Trova l'esempio completo e scopri di più sulla configurazione e l'esecuzione nel Repository di esempi di codice 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; } };

Il seguente esempio di codice mostra come utilizzareUpdateTemplate.

SDKper JavaScript (v3)
Nota

C'è di più su. GitHub Trova l'esempio completo e scopri di più sulla configurazione e l'esecuzione nel Repository di esempi di codice 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; } };

Il seguente esempio di codice mostra come utilizzareVerifyDomainIdentity.

SDKper JavaScript (v3)
Nota

C'è di più su. GitHub Trova l'esempio completo e scopri di più sulla configurazione e l'esecuzione nel Repository di esempi di codice AWS.

import { VerifyDomainIdentityCommand } from "@aws-sdk/client-ses"; import { getUniqueName, postfix, } from "@aws-doc-sdk-examples/lib/utils/util-string.js"; import { sesClient } from "./libs/sesClient.js"; /** * You must have access to the domain's DNS settings to complete the * domain verification process. */ const DOMAIN_NAME = postfix(getUniqueName("Domain"), ".example.com"); const createVerifyDomainIdentityCommand = () => { return new VerifyDomainIdentityCommand({ Domain: DOMAIN_NAME }); }; const run = async () => { const VerifyDomainIdentityCommand = createVerifyDomainIdentityCommand(); try { return await sesClient.send(VerifyDomainIdentityCommand); } catch (err) { console.log("Failed to verify domain.", err); return err; } };

Il seguente esempio di codice mostra come utilizzareVerifyEmailIdentity.

SDKper JavaScript (v3)
Nota

C'è di più su. GitHub Trova l'esempio completo e scopri di più sulla configurazione e l'esecuzione nel Repository di esempi di codice AWS.

// Import required AWS SDK clients and commands for Node.js import { VerifyEmailIdentityCommand } from "@aws-sdk/client-ses"; import { sesClient } from "./libs/sesClient.js"; const EMAIL_ADDRESS = "name@example.com"; const createVerifyEmailIdentityCommand = (emailAddress) => { return new VerifyEmailIdentityCommand({ EmailAddress: emailAddress }); }; const run = async () => { const verifyEmailIdentityCommand = createVerifyEmailIdentityCommand(EMAIL_ADDRESS); try { return await sesClient.send(verifyEmailIdentityCommand); } catch (err) { console.log("Failed to verify email identity.", err); return err; } };