AWS SDK for JavaScript SES Client for Node.js, Browser and React Native
—
Core email sending functionality with support for formatted emails, raw emails with attachments, templated emails, and bulk operations. This module provides comprehensive email delivery capabilities with full control over recipients, content, and tracking.
Send formatted email messages with text and/or HTML content.
/**
* Sends an email message with text and/or HTML content
* @param input - Email parameters including source, destination, and message
* @returns Promise with message ID and metadata
*/
interface SendEmailCommandInput {
/** The email address that is sending the email */
Source: string;
/** Email recipients (To, CC, BCC) */
Destination: Destination;
/** Email message content (subject and body) */
Message: Message;
/** Reply-to email addresses */
ReplyToAddresses?: string[];
/** Return path for bounces */
ReturnPath?: string;
/** Message tags for tracking */
Tags?: MessageTag[];
/** Configuration set name for tracking */
ConfigurationSetName?: string;
/** Source ARN for cross-account sending */
SourceArn?: string;
/** Return path ARN */
ReturnPathArn?: string;
}
interface SendEmailCommandOutput {
/** Unique message identifier */
MessageId: string;
$metadata: ResponseMetadata;
}Usage Example:
import { SESClient, SendEmailCommand } from "@aws-sdk/client-ses";
const client = new SESClient({ region: "us-east-1" });
const command = new SendEmailCommand({
Source: "sender@example.com",
Destination: {
ToAddresses: ["recipient1@example.com", "recipient2@example.com"],
CcAddresses: ["cc@example.com"],
},
Message: {
Subject: { Data: "Welcome to our service!" },
Body: {
Text: {
Data: "Thank you for signing up. Visit our website for more information."
},
Html: {
Data: "<h1>Welcome!</h1><p>Thank you for signing up. <a href='https://example.com'>Visit our website</a> for more information.</p>"
},
},
},
Tags: [
{ Name: "campaign", Value: "welcome" },
{ Name: "priority", Value: "high" },
],
});
const response = await client.send(command);
console.log("Email sent with ID:", response.MessageId);Send raw email messages including attachments and custom headers.
/**
* Sends a raw email message with full control over content and headers
* @param input - Raw email parameters including message data
* @returns Promise with message ID and metadata
*/
interface SendRawEmailCommandInput {
/** Source email address */
Source?: string;
/** List of recipient email addresses */
Destinations?: string[];
/** Raw email message data including headers and body */
RawMessage: RawMessage;
/** Configuration set name for tracking */
ConfigurationSetName?: string;
/** Message tags for tracking */
Tags?: MessageTag[];
/** Source ARN for cross-account sending */
SourceArn?: string;
/** From ARN for cross-account sending */
FromArn?: string;
/** Return path ARN */
ReturnPathArn?: string;
}
interface RawMessage {
/** Raw email message data as bytes or string */
Data: Uint8Array | string;
}
interface SendRawEmailCommandOutput {
/** Unique message identifier */
MessageId: string;
$metadata: ResponseMetadata;
}Usage Example:
import { SESClient, SendRawEmailCommand } from "@aws-sdk/client-ses";
const client = new SESClient({ region: "us-east-1" });
const rawEmailData = `From: sender@example.com
To: recipient@example.com
Subject: Email with Attachment
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="boundary123"
--boundary123
Content-Type: text/plain
This email contains an attachment.
--boundary123
Content-Type: application/pdf; name="document.pdf"
Content-Disposition: attachment; filename="document.pdf"
Content-Transfer-Encoding: base64
JVBERi0xLjQKJdPr6eEKMSAwIG9iago8PAovVHlwZSAvQ2F0YWxvZwovUGFnZXMgMiAwIFIKPj4K
--boundary123--`;
const command = new SendRawEmailCommand({
Source: "sender@example.com",
Destinations: ["recipient@example.com"],
RawMessage: {
Data: rawEmailData,
},
});
const response = await client.send(command);Send bounce notifications for received messages.
/**
* Sends a bounce message to the sender of an original message
* @param input - Bounce parameters including original message ID
* @returns Promise with message ID and metadata
*/
interface SendBounceCommandInput {
/** Original message ID that is bouncing */
OriginalMessageId: string;
/** Bounce sender email address */
BounceSender: string;
/** Explanation for the bounce */
Explanation?: string;
/** DSN message tags */
MessageDsn?: MessageDsn;
/** Information about bounced recipients */
BouncedRecipientInfoList: BouncedRecipientInfo[];
/** Bounce sender ARN for cross-account sending */
BounceSenderArn?: string;
}
interface BouncedRecipientInfo {
/** Email address of bounced recipient */
Recipient: string;
/** Reason for bounce */
RecipientArn?: string;
/** Bounce type */
BounceType?: BounceType;
/** Additional DSN fields */
RecipientDsnFields?: RecipientDsnFields;
}
interface SendBounceCommandOutput {
/** Unique message identifier */
MessageId: string;
$metadata: ResponseMetadata;
}Send custom verification emails using templates.
/**
* Sends a custom verification email using a template
* @param input - Verification email parameters
* @returns Promise with message ID and metadata
*/
interface SendCustomVerificationEmailCommandInput {
/** Email address to send verification to */
EmailAddress: string;
/** Name of the custom verification template */
TemplateName: string;
/** Configuration set name for tracking */
ConfigurationSetName?: string;
}
interface SendCustomVerificationEmailCommandOutput {
/** Unique message identifier */
MessageId: string;
$metadata: ResponseMetadata;
}interface Destination {
/** Primary recipients */
ToAddresses?: string[];
/** Carbon copy recipients */
CcAddresses?: string[];
/** Blind carbon copy recipients */
BccAddresses?: string[];
}
interface Message {
/** Email subject line */
Subject: Content;
/** Email body content */
Body: Body;
}
interface Content {
/** Content data */
Data: string;
/** Character encoding (default: UTF-8) */
Charset?: string;
}
interface Body {
/** Plain text version */
Text?: Content;
/** HTML version */
Html?: Content;
}
interface MessageTag {
/** Tag name */
Name: string;
/** Tag value */
Value: string;
}interface ResponseMetadata {
/** Unique request identifier */
requestId?: string;
/** Extended request identifier */
extendedRequestId?: string;
/** CloudFront identifier */
cfId?: string;
/** Number of retry attempts */
attempts?: number;
/** Total retry delay in milliseconds */
totalRetryDelay?: number;
}type BounceType =
| "DoesNotExist"
| "MessageTooLarge"
| "ExceededQuota"
| "ContentRejected"
| "Undefined"
| "TemporaryFailure";class MessageRejected extends SESServiceException {
name: "MessageRejected";
}
class MailFromDomainNotVerifiedException extends SESServiceException {
name: "MailFromDomainNotVerifiedException";
}
class AccountSendingPausedException extends SESServiceException {
name: "AccountSendingPausedException";
}
class ConfigurationSetSendingPausedException extends SESServiceException {
name: "ConfigurationSetSendingPausedException";
}Usage Example with Error Handling:
import {
SESClient,
SendEmailCommand,
MessageRejected,
AccountSendingPausedException
} from "@aws-sdk/client-ses";
try {
const response = await client.send(command);
console.log("Email sent successfully:", response.MessageId);
} catch (error) {
if (error instanceof MessageRejected) {
console.error("Message was rejected by SES:", error.message);
} else if (error instanceof AccountSendingPausedException) {
console.error("Account sending is disabled:", error.message);
} else {
console.error("Unexpected error:", error);
}
}Install with Tessl CLI
npx tessl i tessl/npm-aws-sdk--client-ses