Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added SMTP support and removed resend dependency #313

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 5 additions & 3 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ NODE_ENV=development
# AUTH_GITHUB_CLIENT_ID=
# AUTH_GITHUB_CLIENT_SECRET=

# Resend is an email service used for signing in to Trigger.dev via a Magic Link.
# EMAIL VARIABLES.
# Emails will print to the console if you leave these commented out
### Visit https://resend.com, create an account and get your API key. Then insert it below along with your From and Reply To email addresses. Visit https://resend.com/docs for more information.
# RESEND_API_KEY=<api_key>
# FROM_EMAIL=
# REPLY_TO_EMAIL=
# SMTP_HOST=
# SMTP_PORT=
# SMTP_USER=
# SMTP_PASSWORD=

# CLOUD VARIABLES
POSTHOG_PROJECT_KEY=
Expand Down
5 changes: 4 additions & 1 deletion apps/webapp/app/env.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ const EnvironmentSchema = z.object({
AUTH_GITHUB_CLIENT_SECRET: z.string().optional(),
FROM_EMAIL: z.string().optional(),
REPLY_TO_EMAIL: z.string().optional(),
RESEND_API_KEY: z.string().optional(),
SMTP_HOST: z.string().optional(),
SMTP_PORT: z.number().optional(),
matt-aitken marked this conversation as resolved.
Show resolved Hide resolved
SMTP_USER: z.string().optional(),
SMTP_PASSWORD: z.string().optional(),
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 ?? "team@email.trigger.dev",
replyTo: env.REPLY_TO_EMAIL ?? "help@email.trigger.dev",
Expand Down
9 changes: 6 additions & 3 deletions docs/documentation/guides/self-hosting/flyio.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ Both of these secrets should be set to the base URL of your fly application. For

![github copy secrets](/images/github-secrets.png)

`RESEND_API_KEY`, `FROM_EMAIL` and `REPLY_TO_EMAIL`
`FROM_EMAIL`,`REPLY_TO_EMAIL`, `SMTP_HOST`, `SMTP_PORT`, `SMTP_USER` and `SMTP_PASSWORD`

We use [Resend.com](https://resend.com) for email sending (including the magic-link signup/login system). They have a generous free tier of 100 emails a day that should be sufficient. Signup for Resend.com and enter the environment vars below
We use SMTP for email sending (including the magic-link signup/login system).

## Set the secrets

Expand All @@ -99,7 +99,10 @@ fly secrets set \
APP_ORIGIN="https://<fly app name>.fly.dev" \
FROM_EMAIL="Acme Inc. <hello@yourdomain.com>" \
REPLY_TO_EMAIL="Acme Inc. <reply@yourdomain.com>" \
RESEND_API_KEY=<your API Key> \
SMTP_HOST= < your SMTP host > \
SMTP_PORT= < your SMTP port > \
SMTP_USER= < your SMTP user > \
SMTP_PASSWORD= < your SMTP password > \
AUTH_GITHUB_CLIENT_ID=<your GitHub OAuth Client ID> \
AUTH_GITHUB_CLIENT_SECRET=<your GitHUb OAuth Client Secret>

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 = config.smtpConfig ? nodemailer.createTransport(config.smtpConfig) : undefined;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this smtpConfig option is always going to be "truthy" so the nodemailer.createTransport thing is always going to be called:

CleanShot 2023-08-21 at 16 14 58

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm sorry but I don't quite understand this

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ericallam Should I put back the resend api key and dependency as a fallback to when the smtp config doesn't exist?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think Eric just meant the config values should be checked instead of the entire object. Otherwise, this will never return undefined. Checking for truthy smtpConfig.host should probably be enough.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ohh, makes sense. Thanks

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