Skip to content

Instantly share code, notes, and snippets.

@chientrm
Created August 20, 2023 17:43
Show Gist options
  • Save chientrm/cdeaca44c1f8952525a3ae3170c1925f to your computer and use it in GitHub Desktop.
Save chientrm/cdeaca44c1f8952525a3ae3170c1925f to your computer and use it in GitHub Desktop.
Worker MailChannels
import { zValidator } from '@hono/zod-validator';
import { Hono } from 'hono';
import { z } from 'zod';
const app = new Hono<{ Bindings: { DKIM_PRIVATE_KEY: string } }>();
app.get('/', (c) => c.text('Hello Hono!'));
app.post(
'/send_email',
zValidator(
'json',
z.object({
to: z.array(z.object({ name: z.string(), email: z.string().email() })),
subject: z.string(),
content: z.array(z.object({ type: z.string(), value: z.string() })),
}),
),
async (c) => {
const { to, subject, content } = c.req.valid('json');
return fetch('https://api.mailchannels.net/tx/v1/send', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
personalizations: [
{
to,
dkim_domain: 'shitpostai.com',
dkim_selector: 'mailchannels',
dkim_private_key: c.env.DKIM_PRIVATE_KEY,
},
],
from: { name: 'ShitpostAI', email: '[email protected]' },
subject,
content,
}),
});
},
);
export default app;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment