Add email worker and use it to send email from pages function
Some checks failed
release / Publish to Cloudflare Pages (push) Failing after 1m17s
Some checks failed
release / Publish to Cloudflare Pages (push) Failing after 1m17s
This commit is contained in:
2286
contact-email-worker/package-lock.json
generated
Normal file
2286
contact-email-worker/package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
17
contact-email-worker/package.json
Normal file
17
contact-email-worker/package.json
Normal file
@@ -0,0 +1,17 @@
|
||||
{
|
||||
"name": "royal-leaf-c03c",
|
||||
"version": "0.0.0",
|
||||
"private": true,
|
||||
"scripts": {
|
||||
"deploy": "wrangler deploy",
|
||||
"dev": "wrangler dev",
|
||||
"start": "wrangler dev",
|
||||
"cf-typegen": "wrangler types"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@cloudflare/vitest-pool-workers": "^0.6.4",
|
||||
"@cloudflare/workers-types": "^4.20250129.0",
|
||||
"typescript": "^5.5.2",
|
||||
"wrangler": "^3.107.2"
|
||||
}
|
||||
}
|
||||
18
contact-email-worker/src/index.ts
Normal file
18
contact-email-worker/src/index.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import { EmailMessage } from "cloudflare:email";
|
||||
import { WorkerEntrypoint } from "cloudflare:workers";
|
||||
|
||||
export default class SendEmailWorker extends WorkerEntrypoint<Env> {
|
||||
async sendEmail(rawMessage: string): Promise<Response> {
|
||||
try {
|
||||
const cfMessage = new EmailMessage(
|
||||
"contact@michaelpivato.dev",
|
||||
"contact@michaelpivato.dev",
|
||||
rawMessage
|
||||
);
|
||||
await this.env.SEB.send(cfMessage);
|
||||
} catch (e) {
|
||||
return new Response((e as Error).message);
|
||||
}
|
||||
return new Response();
|
||||
}
|
||||
}
|
||||
46
contact-email-worker/tsconfig.json
Normal file
46
contact-email-worker/tsconfig.json
Normal file
@@ -0,0 +1,46 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
/* Visit https://aka.ms/tsconfig.json to read more about this file */
|
||||
|
||||
/* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
|
||||
"target": "es2021",
|
||||
/* Specify a set of bundled library declaration files that describe the target runtime environment. */
|
||||
"lib": ["es2021"],
|
||||
/* Specify what JSX code is generated. */
|
||||
"jsx": "react-jsx",
|
||||
|
||||
/* Specify what module code is generated. */
|
||||
"module": "es2022",
|
||||
/* Specify how TypeScript looks up a file from a given module specifier. */
|
||||
"moduleResolution": "Bundler",
|
||||
/* Specify type package names to be included without being referenced in a source file. */
|
||||
"types": [
|
||||
"@cloudflare/workers-types/2023-07-01"
|
||||
],
|
||||
/* Enable importing .json files */
|
||||
"resolveJsonModule": true,
|
||||
|
||||
/* Allow JavaScript files to be a part of your program. Use the `checkJS` option to get errors from these files. */
|
||||
"allowJs": true,
|
||||
/* Enable error reporting in type-checked JavaScript files. */
|
||||
"checkJs": false,
|
||||
|
||||
/* Disable emitting files from a compilation. */
|
||||
"noEmit": true,
|
||||
|
||||
/* Ensure that each file can be safely transpiled without relying on other imports. */
|
||||
"isolatedModules": true,
|
||||
/* Allow 'import x from y' when a module doesn't have a default export. */
|
||||
"allowSyntheticDefaultImports": true,
|
||||
/* Ensure that casing is correct in imports. */
|
||||
"forceConsistentCasingInFileNames": true,
|
||||
|
||||
/* Enable all strict type-checking options. */
|
||||
"strict": true,
|
||||
|
||||
/* Skip type checking all .d.ts files. */
|
||||
"skipLibCheck": true
|
||||
},
|
||||
"exclude": ["test"],
|
||||
"include": ["worker-configuration.d.ts", "src/**/*.ts"]
|
||||
}
|
||||
5
contact-email-worker/worker-configuration.d.ts
vendored
Normal file
5
contact-email-worker/worker-configuration.d.ts
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
// Generated by Wrangler
|
||||
// After adding bindings to `wrangler.json`, regenerate this interface via `npm run cf-typegen`
|
||||
interface Env {
|
||||
SEB: SendEmail;
|
||||
}
|
||||
13
contact-email-worker/wrangler.json
Normal file
13
contact-email-worker/wrangler.json
Normal file
@@ -0,0 +1,13 @@
|
||||
{
|
||||
"$schema": "node_modules/wrangler/config-schema.json",
|
||||
"name": "contact-email",
|
||||
"main": "src/index.ts",
|
||||
"compatibility_date": "2025-01-29",
|
||||
"observability": {
|
||||
"enabled": true
|
||||
},
|
||||
"send_email": [
|
||||
{ "name": "SEB", "destination_address": "contact@michaelpivato.dev" }
|
||||
],
|
||||
"workers_dev": false
|
||||
}
|
||||
Reference in New Issue
Block a user