Docs

Email API reference

The Email API is the binding, the permission sets, and the events your app uses with an Email resource. The binding is the connection details Alien injects into each workload linked to the resource: here, which SES infrastructure to use.

Your app talks to SES directly with the AWS SDK (@aws-sdk/client-sesv2 in TypeScript, or any AWS SDK).

There is no Alien send API. Every backend is SES, so a wrapper would add a layer without adding portability.

The binding

Linking an Email resource injects ALIEN_<ID>_BINDING: the resource id uppercased, with hyphens replaced by underscores.

const binding = JSON.parse(process.env.ALIEN_MAILER_BINDING!)
FieldTypeNotes
service"ses"
regionstringAWS region of the SES resources.
configurationSetstringPass as ConfigurationSetName on every send so events reach the linked queue.
eventTopicArnstring, optionalThe SNS topic for events. Present only with .events().

The binding has no domain list: identities come and go at runtime, so a list fixed at initial setup would go stale. List the current identities with ses:ListEmailIdentities, granted by email/manage-identities.

Permission sets

Grant these through permission profiles:

.permissions({
  profiles: {
    execution: { mailer: ["email/send", "email/manage-identities"] },
  },
})
SetAWS actionsScope
email/sendses:SendEmail, ses:SendRawEmailEvery identity, and this resource's configuration set
email/manage-identitiesses:CreateEmailIdentity, ses:GetEmailIdentity, ses:DeleteEmailIdentity, ses:PutEmailIdentityMailFromAttributes, ses:PutEmailIdentityDkimAttributes, ses:PutEmailIdentityFeedbackAttributes, ses:PutEmailIdentityConfigurationSetAttributes, ses:ListEmailIdentitiesEvery identity in the account and region
email/managementses:GetEmailIdentity, ses:GetConfigurationSet, ses:GetConfigurationSetEventDestinations, ses:DescribeReceiptRuleSet, ses:DescribeReceiptRule, ses:DescribeActiveReceiptRuleSetRead-only
email/heartbeatses:DescribeActiveReceiptRuleSetRead-only
email/provisionCreating and deleting identities, the configuration set, event destinations, and receipt rulesUsed at initial setup, not at runtime

SES identities are named after mail domains, which aren't known at initial setup. So identity permissions cover every identity in the account and region, while configuration set permissions stay scoped to the stack.

ses:ListEmailIdentities and the receipt-rule reads don't support resource-level scoping in AWS IAM.

Sending

import { SESv2Client, SendEmailCommand } from "@aws-sdk/client-sesv2"

const binding = JSON.parse(process.env.ALIEN_MAILER_BINDING!)
const ses = new SESv2Client({ region: binding.region })

await ses.send(new SendEmailCommand({
  ConfigurationSetName: binding.configurationSet,
  FromEmailAddress: "hello@mail.example.com",
  Destination: { ToAddresses: ["someone@example.org"] },
  Content: {
    Simple: {
      Subject: { Data: "Hello" },
      Body: { Text: { Data: "Sent through SES in the customer's account." } },
    },
  },
}))

Managing domains at runtime

With email/manage-identities, your app owns the domain lifecycle:

  1. Create an identity when a customer adds a domain.
  2. Hand them the DKIM records.
  3. Poll verification.
  4. Delete the identity when they remove the domain.
import {
  SESv2Client,
  CreateEmailIdentityCommand,
  GetEmailIdentityCommand,
  DeleteEmailIdentityCommand,
} from "@aws-sdk/client-sesv2"

const ses = new SESv2Client({ region: binding.region })

// Customer adds a domain
const created = await ses.send(new CreateEmailIdentityCommand({
  EmailIdentity: "mail.customer-1.com",
  ConfigurationSetName: binding.configurationSet,
}))
// created.DkimAttributes.Tokens holds three tokens. The customer creates
// <token>._domainkey.mail.customer-1.com CNAME <token>.dkim.amazonses.com

// Poll verification
const identity = await ses.send(new GetEmailIdentityCommand({
  EmailIdentity: "mail.customer-1.com",
}))
// identity.VerifiedForSendingStatus

// Customer removes the domain
await ses.send(new DeleteEmailIdentityCommand({
  EmailIdentity: "mail.customer-1.com",
}))

Identities created this way are app data. The deployment doesn't track them, and they survive stack deletion. Deleting them is up to your app.

Inbound mail

With .inbound(storage), each incoming message is written to the linked bucket as a raw MIME object. Read the objects with the Storage binding and parse them with a MIME library.

The receipt rule is a catch-all. Mail for any identity the account receives mail for lands in the bucket, including identities your app created at runtime.

With .events(queue) as well, each received message also produces an SES Received notification on the queue. It carries the envelope recipients, the spam and virus verdicts, and the object key of the message in the bucket.

Use the notification to find new mail instead of listing the bucket. The envelope recipients can't be recovered reliably from message headers.

Sending events

With .events(queue), SES publishes Send, Delivery, Bounce, Complaint, DeliveryDelay, and Reject events for mail sent through the configuration set.

Events go through an SNS topic to the queue with raw message delivery, so each queue message body is the SES event JSON. Consume them with a Worker queue trigger.

Outputs

FieldNotes
domainsPer seed domain, dkimTokens: three { name, value } CNAME records to create in DNS.
configurationSetThe configuration set name.
ruleSetNameThe inbound receipt rule set. Present only with .inbound().

Verification status isn't an output, since SES verifies asynchronously after the records exist. Read it with GetEmailIdentity.

On this page