Skip to content

Instantly share code, notes, and snippets.

@chientrm
Created August 15, 2023 07:33
Show Gist options
  • Save chientrm/2b8def9c649a5374d49340b257c3264f to your computer and use it in GitHub Desktop.
Save chientrm/2b8def9c649a5374d49340b257c3264f to your computer and use it in GitHub Desktop.
mailshitpostai
import { array, object, string } from 'yup';
import { validate } from './validate';
import { Router, json, error } from 'itty-router';
export default {
async fetch(
request: Request,
env: Env,
ctx: ExecutionContext,
): Promise<Response> {
const router = Router();
router.post('/api/sendEmail', async () => {
const { to, subject, content } = await validate(request, {
to: array()
.required()
.of(
object().shape({
name: string().required(),
email: string().email().required(),
}),
),
subject: string().required(),
content: array()
.required()
.of(
object().shape({
type: string().required().oneOf(['text/plain', 'text/html']),
value: string().required(),
}),
),
});
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: env.DKIM_PRIVATE_KEY,
},
],
from: { name: 'ShitpostAI', email: '[email protected]' },
subject,
content,
}),
});
});
router.all('*', () => error(404));
return router.handle(request, env, ctx).then(json).catch(error);
},
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment