Created
August 15, 2023 07:33
-
-
Save chientrm/2b8def9c649a5374d49340b257c3264f to your computer and use it in GitHub Desktop.
mailshitpostai
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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