Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions apps/blade/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@
"@forge/auth": "workspace:*",
"@forge/consts": "workspace:*",
"@forge/db": "workspace:*",
"@forge/emails": "workspace:^",
"@forge/transactional": "workspace:^",
"@forge/ui": "workspace:*",
"@forge/validators": "workspace:*",
"@react-email/render": "1.1.0",
"@stripe/react-stripe-js": "^3.0.0",
"@stripe/stripe-js": "^5.2.0",
"@t3-oss/env-nextjs": "^0.11.1",
Expand Down
5 changes: 0 additions & 5 deletions apps/blade/src/app/emailTest/page.tsx

This file was deleted.

40 changes: 29 additions & 11 deletions packages/api/src/routers/email.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { TRPCRouterRecord } from "@trpc/server";
import type { GaxiosError } from "googleapis-common";
import { z } from "zod";

import { publicProcedure } from "../trpc";
Expand All @@ -11,43 +12,60 @@ export const emailRouter = {
to: z.string().email(),
subject: z.string().min(1),
body: z.string().min(1),
from: z.string().min(1),
}),
)
.mutation(async ({ input }) => {
const alias = input.from;

try {
await gmail.users.settings.sendAs.create({
userId: "me",
requestBody: {
sendAsEmail: alias,
displayName: "Knight Hacks",
treatAsAlias: true,
isDefault: false,
},
});
} catch (err: unknown) {
if ((err as GaxiosError).code !== "409") {
console.error("Error creating sendAs alias:", err);
}
}

try {
// write email
const rawMessage = [
`From: ${alias}`,
`To: ${input.to}`,
"MIME-Version: 1.0", // needed for proper email formatting
"Content-Type: text/plain; charset=utf-8", // specify content type
"MIME-Version: 1.0",
"Content-Type: text/html; charset=utf-8",
`Subject: ${input.subject}`,
"", // empty line between headers and body
"",
input.body,
].join("\n");

// encode message properly for Gmail API
const encodedMessage = Buffer.from(rawMessage)
.toString("base64")
.replace(/\+/g, "-")
.replace(/\//g, "_")
.replace(/=+$/, "");

// send email
const response = await gmail.users.messages.send({
userId: "me",
requestBody: {
raw: encodedMessage,
},
requestBody: { raw: encodedMessage },
});

return { success: true, messageId: response.data.id };
} catch (error) {
console.error("Error sending email:", {
error: error instanceof Error ? error.message : error,
input: input,
input,
});
throw new Error(
`Failed to send email: ${error instanceof Error ? error.message : "Unknown error"}`,
`Failed to send email: ${
error instanceof Error ? error.message : "Unknown error"
}`,
);
}
}),
Expand Down
6 changes: 4 additions & 2 deletions packages/api/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,15 @@ const GOOGLE_PRIVATE_KEY = Buffer.from(env.GOOGLE_PRIVATE_KEY_B64, "base64")
.replace(/\\n/g, "\n");

const gapiCalendar = "https://www.googleapis.com/auth/calendar";
const gapiGmail = "https://www.googleapis.com/auth/gmail.send";
const gapiGmailSend = "https://www.googleapis.com/auth/gmail.send";
const gapiGmailSettingsSharing =
"https://www.googleapis.com/auth/gmail.settings.sharing";

const auth = new google.auth.JWT(
env.GOOGLE_CLIENT_EMAIL,
undefined,
GOOGLE_PRIVATE_KEY,
[gapiCalendar, gapiGmail],
[gapiCalendar, gapiGmailSend, gapiGmailSettingsSharing],
GOOGLE_PERSONIFY_EMAIL as string,
);

Expand Down
Loading
Loading