-
Notifications
You must be signed in to change notification settings - Fork 379
[Resend] Update the resend component #796
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
Open
jordanhunt22
wants to merge
2
commits into
main
Choose a base branch
from
jordan/cleanup-resend-component
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+15
−55
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,6 @@ | ||
export const resendComponentReadmePrompt = ` | ||
# Resend Convex Component (Beta) | ||
|
||
[](https://badge.fury.io/js/@convex-dev%2Fresend) | ||
|
||
; | ||
This component is the official way to integrate the Resend email service | ||
with your Convex project. | ||
|
||
|
@@ -14,9 +12,6 @@ Features: | |
- Idempotency: Manages Resend idempotency keys to guarantee emails are delivered exactly once, preventing accidental spamming from retries. | ||
- Rate limiting: Honors API rate limits established by Resend. | ||
|
||
See [example](./example) for a demo of how to incorporate this hook into your | ||
application. | ||
|
||
## Installation | ||
|
||
\`\`\`bash | ||
|
@@ -28,7 +23,7 @@ First, you'll need to get a Resend account [here](https://resend.com). | |
You'll need a registered domain to send emails from. | ||
Set one up in the Resend dashboard [here](https://resend.com/domains). | ||
Grab an API key [here](https://resend.com/api-keys) | ||
Use the addEnvironmentVariables tool to add \`RESEND_API_KEY\` and \`RESEND_DOMAIN\` to your deployment. | ||
Use the addEnvironmentVariables tool to add \`RESEND_API_KEY\` to your deployment. | ||
|
||
Next, add the component to your Convex app via \`convex/convex.config.ts\`: | ||
|
||
|
@@ -49,40 +44,36 @@ import { components } from "./_generated/api"; | |
import { Resend } from "@convex-dev/resend"; | ||
import { internalMutation } from "./_generated/server"; | ||
|
||
export const resend: Resend = new Resend(components.resend, {}); | ||
export const resend: Resend = new Resend(components.resend, { testMode: false }); | ||
|
||
export const sendTestEmail = internalMutation({ | ||
export const sendEmail = internalMutation({ | ||
handler: async (ctx) => { | ||
await resend.sendEmail( | ||
ctx, | ||
"Me <test@\${process.env.RESEND_DOMAIN}>", | ||
"Resend <[email protected]>", | ||
"Resend <[email protected]>", | ||
"Me <[email protected]>", | ||
"Hi there", | ||
"This is a test email" | ||
); | ||
}, | ||
}); | ||
\`\`\` | ||
|
||
Then, calling \`sendTestEmail\` from anywhere in your app will send this test email. | ||
You must configure it to send emails from the \`RESEND_DOMAIN\` environment variable, otherwise you will see an unverified domain error. | ||
|
||
If you want to send emails to real addresses, you need to disable \`testMode\`. | ||
You can do this in \`ResendOptions\`, [as detailed below](#resend-component-options-and-going-into-production). | ||
Then, calling \`sendEmail\` from anywhere in your app will send this email. | ||
You should alwaysuse the [email protected] email address as the from email unless the user requests otherwise. | ||
|
||
While the setup we have so far will reliably send emails, you don't have any feedback | ||
on anything delivering, bouncing, or triggering spam complaints. For that, we need | ||
to set up a webhook! | ||
## Advanced: Setting up a webhook | ||
Only do this if the user specifically asks for it. This will allow you to get email status updates. | ||
|
||
On the Convex side, we need to mount an http endpoint to our project to route it to | ||
the Resend component in \`convex/http.ts\`: | ||
the Resend component in \`convex/router.ts\`: | ||
|
||
\`\`\`ts | ||
import { httpRouter } from "convex/server"; | ||
import { httpAction } from "./_generated/server"; | ||
import { resend } from "./sendEmails"; | ||
|
||
const http = httpRouter(); | ||
... existing code ... | ||
|
||
http.route({ | ||
path: "/resend-webhook", | ||
|
@@ -144,30 +135,6 @@ export const handleEmailEvent = internalMutation({ | |
/* ... existing email sending code ... */ | ||
\`\`\` | ||
|
||
Check out the \`example/\` project in this repo for a full demo. | ||
|
||
### Resend component options, and going into production | ||
|
||
There is a \`ResendOptions\` argument to the component constructor to help customize | ||
it's behavior. | ||
|
||
Check out the [docstrings](./src/client/index.ts), but notable options include: | ||
|
||
- \`apiKey\`: Provide the Resend API key instead of having it read from the environment | ||
variable. | ||
- \`webhookSecret\`: Same thing, but for the webhook secret. | ||
- \`testMode\`: Only allow delivery to test addresses. To | ||
your project, \`testMode\` is default **true**. You need to explicitly set this to | ||
\`false\` for the component to allow you to enqueue emails to artibrary addresses. | ||
- \`onEmailEvent\`: Your email event callback, as outlined above! | ||
Check out the [docstrings](./src/client/index.ts) for details on the events that | ||
are emitted. | ||
|
||
### Optional email sending parameters | ||
|
||
In addition to basic from/to/subject and html/plain text bodies, the \`sendEmail\` method | ||
allows you to provide a list of \`replyTo\` addresses, and other email headers. | ||
|
||
### Tracking, getting status, and cancelling emails | ||
|
||
The \`sendEmail\` method returns a branded type, \`EmailId\`. You can use this | ||
|
@@ -178,11 +145,4 @@ for a few things: | |
- To cancel the email using \`resend.cancelEmail(ctx, emailId)\`. | ||
|
||
If the email has already been sent to the Resend API, it cannot be cancelled. Cancellations | ||
do not trigger an email event. | ||
|
||
### Data retention | ||
|
||
This component retains "finalized" (delivered, cancelled, bounced) emails for seven days | ||
so you can check on the status of them. Then, a background job clears out those emails | ||
and their bodies to reclaim database space. | ||
`; | ||
do not trigger an email event.`; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good idea