-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix some email issues and start working on sending emails via mailcha…
…nnels
- Loading branch information
Showing
8 changed files
with
139 additions
and
25 deletions.
There are no files selected for viewing
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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { OpenAPIRoute } from "@cloudflare/itty-router-openapi"; | ||
import { Context } from "../../interfaces"; | ||
import { OpenAPIRouteSchema } from "@cloudflare/itty-router-openapi/dist/src/types"; | ||
|
||
export class SendEmail extends OpenAPIRoute { | ||
static schema: OpenAPIRouteSchema = { | ||
operationId: "post-email-send", | ||
tags: ["Emails"], | ||
summary: "Send Email", | ||
requestBody: { | ||
subject: "Look! No servers", | ||
from: { | ||
email: "[email protected]", | ||
name: "Workers - MailChannels integration" | ||
}, | ||
to: [{ email: "[email protected]", name: "Test Recipient" }], | ||
content: { | ||
"text/plain": "And no email service accounts and all for free too!" | ||
} | ||
} | ||
}; | ||
|
||
async handle( | ||
request: Request, | ||
env: any, | ||
context: Context, | ||
data: any | ||
) { | ||
if (context.config.readonly === true) return Response.json({ msg: "unauthorized" }, { status: 401 }); | ||
|
||
const emailResp = await fetch("https://api.mailchannels.net/tx/v1/send", { | ||
method: "POST", | ||
headers: { | ||
"content-type": "application/json" | ||
}, | ||
body: JSON.stringify({ | ||
personalizations: [ | ||
{ | ||
to: data.body.to | ||
} | ||
], | ||
from: data.body.from, | ||
subject: data.body.subject, | ||
content: [ | ||
{ | ||
type: "text/plain", | ||
value: data.body.content["text/plain"] | ||
} | ||
] | ||
}) | ||
}); | ||
|
||
return { msg: await emailResp.text() }; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { config } from "../settings"; | ||
import { OpenAPIRouter } from "@cloudflare/itty-router-openapi"; | ||
import { SendEmail } from "./api/sendEmail"; | ||
|
||
|
||
export const emailsRouter = OpenAPIRouter({ | ||
base: "/api/emails", | ||
raiseUnknownParameters: config.raiseUnknownParameters, | ||
generateOperationIds: config.generateOperationIds | ||
}); | ||
|
||
emailsRouter.post("/send", SendEmail); |
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