aws
The aws output does up to three things for each email, in this order, each when its option is set:
storage: store the email, or a part of it, in an Amazon S3 bucket;pubsub: send a CloudEvent describing the email to an Amazon SQS queue;ses: send the email with Amazon SESSendRawEmail.
When to use it
Section titled “When to use it”- An SMTP endpoint for applications that should send through SES without the SES API or SMTP credentials: the relay uses an IAM role. See replacing aws-smtp-relay.
- Receive emails for your domain into S3 and process them from a queue, similar to SES receiving rules.
Credentials and region
Section titled “Credentials and region”The output creates one AWS SDK client per service. Credentials and region come from the SDK default chain:
environment variables (AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_REGION…), shared config files, or the
role of the container or instance. s3client, sqsclient and sesClient pass options to each client, such as
the region of the SES identity or an endpoint for a local emulator.
With "ses": true, the email is sent as received (with the server’s header changes)
to the recipients the flow accepted (RCPT TO), blind copies included. SES needs the sender to be a verified
identity, and the account to be out of the SES sandbox to send to any address.
sendRawEmailOptions adds parameters to the SendRawEmail call, such as a configuration set or the ARNs used to
send through an identity of another account. They cannot change the message or the recipients.
storage.type selects what is stored:
type |
Stored object |
|---|---|
raw (default) |
the whole email; with addRawHeaders (default true), X-smtp-relay-* headers are added at the top: RCPT_TO (one per recipient of the flow), MAIL_FROM, USER (if authenticated), CLIENT_HOSTNAME and HELO |
attachments |
one object per attachment; put ${attachment} in path so they do not overwrite each other |
text |
the text body, if any |
html |
the HTML body, if any |
storage.path is the object key and accepts the path variables. putOptions adds
parameters to every PutObject call: storage class, encryption, ACL, metadata, tags.
pubsub.topic is the queue URL, for example https://sqs.us-east-1.amazonaws.com/123456789012/emails. The message
body is the CloudEvent, in JSON. It does not contain the S3 object key: use a key the consumer can rebuild from
the CloudEvent, such as ${iso8601}_${id} (from its time and data.server.id). sendMessageOptions adds parameters to the SendMessage call,
such as the MessageGroupId a FIFO queue requires.
Options
Section titled “Options”Examples
Section titled “Examples”Relay every email to SES, the setup of the former aws-smtp-relay:
// Replace my previous project aws-smtp-relay{ "$schema": "https://raw.githubusercontent.com/loopingz/smtp-relay/main/config.schema.json", "flows": { "localhost": { "filters": [ // Allow any ip to use the SMTP { "type": "whitelist", "ips": ["regexp:.*"] } ], "outputs": [ { "type": "aws", // Send it to SES "ses": true } ] } }, // Listen on every interface so the server is reachable from outside its container "bind": "0.0.0.0", "options": { "disableReverseLookup": false, "hideSTARTTLS": true, // Do not require auth "authOptional": true }}Send through an SES identity owned by another AWS account, with sending authorization:
// Sending Authorization setup can be found here: https://docs.aws.amazon.com/ses/latest/dg/sending-authorization.html//// This is useful if you have multiple SES identities in different accounts, regions or both that you want to re-use, or if you want to send e-mail using an identity that a 3rd party controls (customer, vendor, etc.).//// The account that owns the verified identity (e.g. 111111111111) attaches an identity policy allowing the account running the relay (e.g. 222222222222) to send:// {// "Version": "2012-10-17",// "Statement": [{// "Effect": "Allow",// "Principal": { "AWS": "arn:aws:iam::222222222222:root" }, // This can be the account root or a more specific identity used by the AWS SDK. More information about available identities can be found here: https://docs.aws.amazon.com/sdkref/latest/guide/standardized-credentials.html// "Action": ["ses:SendRawEmail"],// "Resource": "arn:aws:ses:us-east-1:111111111111:identity/example.com"// }]// }//// The account running the relay (e.g. 222222222222) must have an IAM policy allowing it to send using the identity in the owning account (e.g. 111111111111):// {// "Version": "2012-10-17",// "Statement": [{// "Effect": "Allow",// "Action": ["ses:SendRawEmail"],// "Resource": "arn:aws:ses:us-east-1:111111111111:identity/example.com"// }]// }//// The relay then passes the identity ARN as SourceArn/FromArn/ReturnPathArn.//// IMPORTANT: The account running the relay MUST be removed from the sandbox in the same region that the SES identity exists in.// In this case BOTH account 111111111111 and 222222222222 must be removed from the SES sandbox in us-east-1.
{ "$schema": "https://raw.githubusercontent.com/loopingz/smtp-relay/main/config.schema.json", "flows": { "cross-account-example": { "filters": [ { "type": "whitelist", "from": ["regexp:.*@example.com"] // Recommended filter when multiple identities configured and need to be routed to different accounts // This is a simple example and as-is creates an open relay, so additional filtering or authenticationis probably a good idea. } ], "outputs": [ { "type": "aws", "ses": true, // Region of the SES identity in the owning account. // Not required if it is the same region as the smtp-relay "sesClient": { "region": "us-east-1" }, // ARN of the identity in the owning account "sendRawEmailOptions": { "SourceArn": "arn:aws:ses:us-east-1:111111111111:identity/example.com", "FromArn": "arn:aws:ses:us-east-1:111111111111:identity/example.com", "ReturnPathArn": "arn:aws:ses:us-east-1:111111111111:identity/example.com" // In testing this was not always necessary } } ] } }, // Listen on every interface so the server is reachable from outside its container "bind": "0.0.0.0", "options": { "hideSTARTTLS": true, "authOptional": true }}Receive emails for a domain: store them in S3, then notify a queue:
$schema: https://raw.githubusercontent.com/loopingz/smtp-relay/main/config.schema.jsonflows: inbound: filters: - type: whitelist to: ["regexp:.*@mydomain.com"] outputs: - type: aws storage: bucket: mydomain-inbound path: inbound/${iso8601}_${id}.eml putOptions: ServerSideEncryption: AES256 pubsub: topic: https://sqs.eu-west-1.amazonaws.com/123456789012/inbound-emails s3client: region: eu-west-1 sqsclient: region: eu-west-1bind: 0.0.0.0options: authOptional: true