Skip to content

Commit

Permalink
Drop AWS SDK v3 for SES to make it work on edge.
Browse files Browse the repository at this point in the history
  • Loading branch information
oscartbeaumont committed Feb 5, 2024
1 parent 5cc351d commit ead6e8f
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 38 deletions.
4 changes: 3 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ ENTRA_CLIENT_SECRET=
INTUNE_CLIENT_ID=
INTUNE_CLIENT_SECRET=
INTUNE_TENANT=
FROM_ADDRESS=console
FROM_ADDRESS=console
AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
2 changes: 1 addition & 1 deletion api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
}
},
"dependencies": {
"@aws-sdk/client-ses": "^3.504.0",
"@mattrax/email": "workspace:^",
"@mattrax/policy": "workspace:^",
"@microsoft/microsoft-graph-client": "^3.0.7",
Expand All @@ -34,6 +33,7 @@
"@react-email/render": "^0.0.12",
"@t3-oss/env-core": "^0.8.0",
"@trpc/server": "^10.45.0",
"aws4fetch": "^1.0.17",
"base58": "^2.0.1",
"drizzle-orm": "^0.29.3",
"hono": "^3.12.10",
Expand Down
84 changes: 48 additions & 36 deletions api/src/emails.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
import type { JSX as ReactJSX } from "react";
import { render } from "@react-email/render";
import { SES, SendEmailCommand } from "@aws-sdk/client-ses";
import { AwsClient } from "aws4fetch";
import { env } from "./env";

const ses = new SES({
region: "us-east-1",
...(env.AWS_ACCESS_KEY_ID && env.AWS_SECRET_ACCESS_KEY
? {
credentials: {
accessKeyId: env.AWS_ACCESS_KEY_ID,
secretAccessKey: env.AWS_SECRET_ACCESS_KEY,
},
}
: {}),
});
const aws =
env.AWS_ACCESS_KEY_ID && env.AWS_SECRET_ACCESS_KEY
? new AwsClient({
region: "us-east-1",
accessKeyId: env.AWS_ACCESS_KEY_ID,
secretAccessKey: env.AWS_SECRET_ACCESS_KEY,
})
: undefined;

type SendEmailArgs = {
to: string;
Expand All @@ -27,31 +24,46 @@ export async function sendEmail(args: SendEmailArgs) {
return;
}

if (!aws) {
const msg = "AWS client not setup but 'FROM_ADDRESS' provided!";
console.error(msg);
throw new Error(msg);
}

const emailHtml = render(args.component);
const cmd = new SendEmailCommand({
Destination: {
ToAddresses: [args.to],
},
Message: {
Body: {
Html: {
Charset: "UTF-8",
Data: emailHtml,
const resp = await aws.fetch(
"https://email.us-east-1.amazonaws.com/v2/email/outbound-emails",
{
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
FromEmailAddress: env.FROM_ADDRESS,
Destination: {
ToAddresses: [args.to],
},
},
Subject: {
Charset: "UTF-8",
Data: args.subject,
},
},
Source: env.FROM_ADDRESS,
ReplyToAddresses: [],
});

try {
return await ses.send(cmd);
} catch (err) {
console.error("Failed to send email.", err);
return err;
Content: {
Simple: {
Body: {
Html: {
Charset: "UTF-8",
Data: emailHtml,
},
},
Subject: {
Charset: "UTF-8",
Data: args.subject,
},
},
},
}),
}
);
if (!resp.ok) {
console.error(
`Error sending email with SES status '${
resp.status
}': ${await resp.text()}`
);
throw new Error("Failed to send email.");
}
}
10 changes: 10 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ead6e8f

Please sign in to comment.