-
Notifications
You must be signed in to change notification settings - Fork 125
feat: add send-email-with-keplars Node.js template #351
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
debojyoti452
wants to merge
2
commits into
appwrite:main
Choose a base branch
from
debojyoti452:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,2 @@ | ||
| node_modules/ | ||
| .env |
This file contains hidden or 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,69 @@ | ||
| # Send Email with Keplars | ||
|
|
||
| Send transactional emails from your Appwrite Function using the [Keplars](https://keplars.com) priority-queue API with instant, high, async, or bulk delivery. | ||
|
|
||
| ## Usage | ||
|
|
||
| ### POST / | ||
|
|
||
| Send an email. | ||
|
|
||
| **Request body:** | ||
|
|
||
| | Field | Type | Required | Description | | ||
| | --- | --- | --- | --- | | ||
| | `to` | string \| string[] | Yes | Recipient email address(es) | | ||
| | `from` | string | Yes | Sender address (must be verified in Keplars) | | ||
| | `subject` | string | Yes | Email subject line | | ||
| | `body` | string | No | Email body (HTML or plain text). Required if `template_id` is not set. | | ||
| | `from_name` | string | No | Sender display name | | ||
| | `template_id` | string | No | Keplars template ID. Required if `body` is not set. | | ||
| | `params` | object | No | Template variables | | ||
|
|
||
| **Success response:** | ||
|
|
||
| ```json | ||
| { | ||
| "ok": true, | ||
| "data": { | ||
| "id": "msg_...", | ||
| "status": "queued" | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| **Error response:** | ||
|
|
||
| ```json | ||
| { | ||
| "ok": false, | ||
| "error": "Missing required fields: to, from, subject" | ||
| } | ||
| ``` | ||
|
|
||
| ## Configuration | ||
|
|
||
| | Variable | Description | Required | | ||
| | --- | --- | --- | | ||
| | `KEPLARS_API_KEY` | Your Keplars API key (`kms_...`) | Yes | | ||
|
|
||
| ## Deployment | ||
|
|
||
| 1. Create a new Appwrite Function | ||
| 2. Add the environment variables above | ||
| 3. Deploy the function | ||
|
|
||
| **Example request:** | ||
|
|
||
| ```bash | ||
| curl -X POST https://[REGION].appwrite.io/v1/functions/[FUNCTION_ID]/executions \ | ||
| -H "X-Appwrite-Project: [PROJECT_ID]" \ | ||
| -H "Content-Type: application/json" \ | ||
| -d '{ | ||
| "to": "user@example.com", | ||
| "from": "hello@yourdomain.com", | ||
| "subject": "Welcome!", | ||
| "body": "<h1>Welcome!</h1><p>Thanks for signing up.</p>" | ||
| }' | ||
| ``` | ||
|
|
This file contains hidden or 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,9 @@ | ||
| declare global { | ||
| namespace NodeJS { | ||
| interface ProcessEnv { | ||
| KEPLARS_API_KEY: string; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| export {}; |
This file contains hidden or 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,13 @@ | ||
| { | ||
| "name": "send-email-with-keplars", | ||
| "version": "1.0.0", | ||
| "description": "Send transactional emails using the Keplars priority-queue API.", | ||
| "main": "src/main.js", | ||
| "type": "module", | ||
| "scripts": { | ||
| "format": "prettier --write ." | ||
| }, | ||
| "devDependencies": { | ||
| "prettier": "^3.2.5" | ||
| } | ||
| } |
This file contains hidden or 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,71 @@ | ||
| import { throwIfMissing } from './utils.js'; | ||
|
|
||
| const ENDPOINT = 'https://api.keplars.com/api/v1/send-email/async'; | ||
|
|
||
| export default async ({ req, res, log, error }) => { | ||
| throwIfMissing(process.env, ['KEPLARS_API_KEY']); | ||
|
|
||
| if (req.method !== 'POST') { | ||
| return res.json({ ok: false, error: 'Method not allowed' }, 405); | ||
| } | ||
|
|
||
| const { to, from, from_name, subject, body, template_id, params } = | ||
| req.body ?? {}; | ||
|
|
||
| if (!to || !from || !subject) { | ||
| return res.json( | ||
| { ok: false, error: 'Missing required fields: to, from, subject' }, | ||
| 400 | ||
| ); | ||
| } | ||
|
|
||
| if (!body && !template_id) { | ||
| return res.json( | ||
| { | ||
| ok: false, | ||
| error: 'Either body or template_id must be provided', | ||
| }, | ||
| 400 | ||
| ); | ||
| } | ||
|
|
||
| const payload = { | ||
| to: Array.isArray(to) ? to : [to], | ||
| from, | ||
| subject, | ||
| }; | ||
|
|
||
| if (from_name) payload.from_name = from_name; | ||
| if (body) payload.body = body; | ||
| if (template_id) payload.template_id = template_id; | ||
| if (params && typeof params === 'object') payload.params = params; | ||
|
|
||
| try { | ||
| const response = await fetch(ENDPOINT, { | ||
| method: 'POST', | ||
| headers: { | ||
| Authorization: `Bearer ${process.env.KEPLARS_API_KEY}`, | ||
| 'Content-Type': 'application/json', | ||
| 'User-Agent': 'keplars-appwrite/1.0.0', | ||
| }, | ||
| body: JSON.stringify(payload), | ||
| }); | ||
|
|
||
| const data = await response.json(); | ||
|
|
||
| if (!response.ok) { | ||
| const message = | ||
| typeof data?.message === 'string' | ||
| ? data.message | ||
| : `Keplars API error: ${response.status}`; | ||
| error(message); | ||
| return res.json({ ok: false, error: message }, response.status); | ||
| } | ||
|
|
||
| log(`Email sent to ${Array.isArray(to) ? to.join(', ') : to}`); | ||
| return res.json({ ok: true, data }); | ||
| } catch (err) { | ||
| error(err.message); | ||
| return res.json({ ok: false, error: 'Failed to send email' }, 500); | ||
| } | ||
| }; | ||
This file contains hidden or 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,11 @@ | ||
| /** | ||
| * @param {Record<string, string>} obj | ||
| * @param {string[]} keys | ||
| * @throws {Error} | ||
| */ | ||
| export function throwIfMissing(obj, keys) { | ||
| const missing = keys.filter((key) => !obj[key]); | ||
| if (missing.length > 0) { | ||
| throw new Error(`Missing required environment variables: ${missing.join(', ')}`); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.