Skip to content

Commit

Permalink
added email worker
Browse files Browse the repository at this point in the history
  • Loading branch information
RafidMuhymin committed Oct 22, 2023
1 parent aadf804 commit a26df25
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/workers/dilmahtea-me-email/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"private": true,
"name": "dilmahtea-me-email",
"version": "1.0.0",
"description": "A template for kick starting a Cloudflare Workers project",
"main": "src/index.ts",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"license": "MIT"
}
47 changes: 47 additions & 0 deletions src/workers/dilmahtea-me-email/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import type { ENV } from "./types";

import createModuleWorker, { reply } from "../../../utils/createModuleWorker";

declare interface Body {
to: { email: string; name?: string }[];
subject: string;
content: { type: string; value: string }[];
}

async function handlePOST(request: Request, env: ENV) {
const { to, subject, content } = await request.json<Body>();

const response = await fetch("https://api.mailchannels.net/tx/v1/send", {
method: "POST",
headers: {
"content-type": "application/json",
},
body: JSON.stringify({
personalizations: [
{
to,
dkim_domain: "dilmahtea.me",
dkim_selector: "mailchannels",
dkim_private_key: env.DKIM_PRIVATE_KEY,
},
],
from: {
name: env.FROM_NAME,
email: env.FROM_EMAIL,
},
subject,
content,
}),
}).then((res) => res.json());

console.log(response);

return reply({ success: true }, 200);
}

handlePOST.retry = true;

export default createModuleWorker({
pathname: "*",
methods: { POST: handlePOST },
});
9 changes: 9 additions & 0 deletions src/workers/dilmahtea-me-email/src/types/env.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export interface ENV {
// ENVIRONMENTAL VARIABLES
FROM_NAME: string;
FROM_EMAIL: string;
DKIM_PRIVATE_KEY: string;

// KV NAMESPACES
MAILS: KVNamespace;
}
1 change: 1 addition & 0 deletions src/workers/dilmahtea-me-email/src/types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type { ENV } from "./env";
10 changes: 10 additions & 0 deletions src/workers/dilmahtea-me-email/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"compilerOptions": {
"target": "esnext",
"module": "esnext",
"lib": ["esnext"],
"moduleResolution": "node",
"allowSyntheticDefaultImports": true,
"types": ["@cloudflare/workers-types/2022-11-30"]
}
}
12 changes: 12 additions & 0 deletions src/workers/dilmahtea-me-email/wrangler.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
name = "dilmahtea-me-email"
account_id = "f0f636c746c8673c3905e9104b385251"
compatibility_date = "2022-11-30"
main = "./src/index.ts"
workers_dev = false
logpush = true
placement = { mode = "smart" }
vars = { FROM_NAME = "Dilmah Europe", FROM_EMAIL = "hello@dilmahtea.me" }
services = [{ binding = "RETRY_WORKERS", service = "dilmahtea-me-retry-workers" }]

# secrets
# DKIM_PRIVATE_KEY

0 comments on commit a26df25

Please sign in to comment.