Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 4 additions & 0 deletions apps/webapp/app/env.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ const EnvironmentSchema = z.object({
FROM_EMAIL: z.string().optional(),
REPLY_TO_EMAIL: z.string().optional(),
RESEND_API_KEY: z.string().optional(),
SMTP_HOST: z.string(),
SMTP_PORT: z.number(),
SMTP_USER: z.string(),
SMTP_PASSWORD: z.string(),
PLAIN_API_KEY: z.string().optional(),
RUNTIME_PLATFORM: z.enum(["docker-compose", "ecs", "local"]).default("local"),
});
Expand Down
10 changes: 9 additions & 1 deletion apps/webapp/app/services/email.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,15 @@ import type { AuthUser } from "./authUser";
import { workerQueue } from "./worker.server";

const client = new EmailClient({
apikey: env.RESEND_API_KEY,
smtpConfig: {
host:env.SMTP_HOST,
secure: true,
port: env.SMTP_PORT,
auth: {
user: env.SMTP_USER,
pass: env.SMTP_PASSWORD,
},
},
imagesBaseUrl: env.APP_ORIGIN,
from: env.FROM_EMAIL ?? "[email protected]",
replyTo: env.REPLY_TO_EMAIL ?? "[email protected]",
Expand Down
5 changes: 3 additions & 2 deletions packages/emails/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,17 @@
"@react-email/render": "^0.0.7",
"@react-email/section": "^0.0.1",
"@react-email/text": "^0.0.2",
"nodemailer": "^6.9.4",
"react": "^18.2.0",
"react-email": "^1.6.1",
"resend": "^0.9.1",
"tiny-invariant": "^1.2.0",
"zod": "3.21.4"
},
"devDependencies": {
"@types/react": "18.2.17",
"@trigger.dev/tsconfig": "workspace:*",
"@types/node": "16",
"@types/nodemailer": "^6.4.9",
"@types/react": "18.2.17",
"typescript": "^4.9.4"
},
"engines": {
Expand Down
28 changes: 21 additions & 7 deletions packages/emails/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import WorkflowIntegration from "../emails/workflow-integration";
import InviteEmail, { InviteEmailSchema } from "../emails/invite";
import { render } from "@react-email/render";

import { Resend } from "resend";
import nodemailer from "nodemailer";
import { z } from "zod";
import React from "react";

Expand Down Expand Up @@ -42,15 +42,29 @@ export const DeliverEmailSchema = z

export type DeliverEmail = z.infer<typeof DeliverEmailSchema>;

export type NodeMailerTransportOptions = {
host: string;
port: number;
secure: boolean;
auth: {
user: string;
pass: string;
};
};

export class EmailClient {
#client?: Resend;
#client?: nodemailer.Transporter;
#imagesBaseUrl: string;
#from: string;
#replyTo: string;

constructor(config: { apikey?: string; imagesBaseUrl: string; from: string; replyTo: string }) {
this.#client =
config.apikey && config.apikey.startsWith("re_") ? new Resend(config.apikey) : undefined;
constructor(config: {
smtpConfig: NodeMailerTransportOptions;
imagesBaseUrl: string;
from: string;
replyTo: string;
}) {
this.#client = nodemailer.createTransport(config.smtpConfig);
this.#imagesBaseUrl = config.imagesBaseUrl;
this.#from = config.from;
this.#replyTo = config.replyTo;
Expand Down Expand Up @@ -110,12 +124,12 @@ export class EmailClient {

async #sendEmail({ to, subject, react }: { to: string; subject: string; react: ReactElement }) {
if (this.#client) {
await this.#client.sendEmail({
await this.#client.sendMail({
from: this.#from,
to,
replyTo: this.#replyTo,
subject,
react,
html: render(react),
});

return;
Expand Down