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

feat: Add example code for scheduled emails #159

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
21 changes: 21 additions & 0 deletions with-scheduling/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module.exports = {
env: {
browser: true,
es2021: true,
},
extends: ['eslint:recommended', 'plugin:@typescript-eslint/recommended'],
overrides: [],
parser: '@typescript-eslint/parser',
parserOptions: {
ecmaVersion: 'latest',
sourceType: 'module',
},
plugins: ['@typescript-eslint'],
rules: {
'linebreak-style': ['error', 'unix'],
'quotes': ['warn', 'single'],
'semi': ['error', 'always'],
'@typescript-eslint/no-explicit-any': 'off',
'@typescript-eslint/ban-ts-comment': 'off',
},
};
5 changes: 5 additions & 0 deletions with-scheduling/.prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
node_modules
.next
.vercel
.env
dist
8 changes: 8 additions & 0 deletions with-scheduling/.prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = {
quoteProps: 'consistent',
singleQuote: true,
trailingComma: 'all',
printWidth: 80,
useTabs: false,
bracketSpacing: true,
};
30 changes: 30 additions & 0 deletions with-scheduling/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name": "with-scheduling",
"description": "Resend with scheduling",
"version": "0.0.1",
"license": "MIT",
"scripts": {
"build": "next build",
"dev": "next dev",
"start": "next start",
"lint": "eslint --fix --ext .ts ./src",
"format": "prettier --write ."
},
"dependencies": {
"dotenv": "^16.0.3",
"next": "13.4.19",
"react": "18.2.0",
"react-dom": "18.2.0",
"resend": "latest"
},
"devDependencies": {
"@types/node": "18.17.5",
"@types/react": "18.2.21",
"@types/react-dom": "18.2.7",
"@typescript-eslint/eslint-plugin": "6.4.0",
"eslint": "8.47.0",
"eslint-config-next": "13.4.19",
"prettier": "3.0.2",
"typescript": "5.2.2"
}
}
23 changes: 23 additions & 0 deletions with-scheduling/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Resend with Schedule Emails

This example show how to schedule emails with the Resend API.

## How to run

### 1. Install the dependencies

```bash
yarn
```

### 2. Create a `.env` file at the root and add your Resend API

```bash
RESEND_API_KEY=re_8m9gwsVG_6n94KaJkJ42Yj6qSeVvLq9xF
```

### 3. Run command to create domain

```bash
node src/create-domain.js
```
15 changes: 15 additions & 0 deletions with-scheduling/src/cancel-scheduled-email.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { resend } from "./lib/resend";

async function cancelScheduledEmail() {
try {
const data = await resend.emails.cancel(
'49a3999c-0ce1-4ea6-ab68-afcd6dc2e794'
);

console.log(data);
} catch (error) {
console.error(error);
}
}

cancelScheduledEmail();
6 changes: 6 additions & 0 deletions with-scheduling/src/lib/resend.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import dotenv from 'dotenv';
dotenv.config();

import { Resend } from 'resend';

export const resend = new Resend(process.env.RESEND_API_KEY!);
21 changes: 21 additions & 0 deletions with-scheduling/src/schedule-email.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { resend } from "./lib/resend";

async function scheduleEmail() {
try {
const oneMinuteFromNow = new Date(Date.now() + 1000 * 60).toISOString();

const data = await resend.emails.send({
from: "Acme <[email protected]>",
to: ["[email protected]"],
subject: 'Hello from the future',
html: '<p>Happy sending</p>',
scheduledAt: oneMinuteFromNow,
});

console.log(data);
} catch (error) {
console.error(error);
}
}

scheduleEmail();
18 changes: 18 additions & 0 deletions with-scheduling/src/update-scheduled-email.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { resend } from "./lib/resend";

async function updateScheduledEmail() {
try {
const twoMinutesFromNow = new Date(Date.now() + 1000 * 120).toISOString();

const data = await resend.emails.update({
id: '49a3999c-0ce1-4ea6-ab68-afcd6dc2e794',
scheduledAt: twoMinutesFromNow,
});

console.log(data);
} catch (error) {
console.error(error);
}
}

updateScheduledEmail();
19 changes: 19 additions & 0 deletions with-scheduling/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"compilerOptions": {
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": false,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"incremental": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve"
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
"exclude": ["node_modules"]
}
Loading