Cloudflare Workers
Overview
Prerequisites
Send emails using Cloudflare Workers and Mailtrap
export default {
async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> {
try {
const response = await fetch("https://send.api.mailtrap.io/api/send", {
method: "POST",
headers: {
"Authorization": "Bearer YOUR-MAILTRAP-API-KEY-HERE",
"Content-Type": "application/json",
},
body: JSON.stringify({
from: { name: 'Mailtrap Test', email: 'YOUR-EMAIL-HERE' },
to: [{ email: 'RECIPIENT-EMAIL-HERE' }],
subject: 'Hello World',
html: '<strong>it works!</strong>',
}),
});
const data = await response.json();
return new Response(JSON.stringify(data), {
headers: {
'Content-Type': 'application/json',
},
});
} catch (error) {
return new Response(JSON.stringify({
error: error instanceof Error ? error.message : 'Unknown error'
}), {
status: 400,
headers: { 'Content-Type': 'application/json' },
});
}
},
};Configuration
Learn more
Last updated
Was this helpful?

