/** * SendPier Send API — send a message with a file attached. * * Requirements: Node.js 18 or newer. Built-in fetch and node:fs, no packages. * * Usage: * export SENDPIER_API_KEY='sp_live_...' * export SENDPIER_DOMAIN='example.com' * export SENDPIER_FROM='invoices@example.com' * node send-with-attachment.mjs recipient@example.org ./invoice.pdf * * Message text, HTML and attachments together may be 25 MB after decoding, and * a message carries at most 20 attachments. If one attachment is refused the * whole message is refused -- half a mail never goes out. */ import { readFileSync } from 'node:fs'; import { basename } from 'node:path'; const API_BASE = 'https://api.sendpier.com'; /** One request to the Send API. */ async function sendpierRequest(method, path, body = null) { const key = process.env.SENDPIER_API_KEY; const domain = process.env.SENDPIER_DOMAIN; if (!key || !domain) { console.error('set SENDPIER_API_KEY and SENDPIER_DOMAIN first'); process.exit(78); } const headers = { Authorization: `Bearer ${key}`, 'X-Domain-Name': domain, Accept: 'application/json', }; if (body !== null) { headers['Content-Type'] = 'application/json'; } const response = await fetch(`${API_BASE}${path}`, { method, headers, body: body === null ? undefined : JSON.stringify(body), // An attachment makes the upload larger; give it room without waiting for // ever on a stalled connection. signal: AbortSignal.timeout(120_000), }); const raw = await response.text(); let parsed = null; try { parsed = JSON.parse(raw); } catch { // Keep the raw text: status code and body both belong in your log. } return { status: response.status, body: parsed, raw }; } /** * One entry of the attachments list. * * `content_type` may be left out; it is then derived from the file name. Add a * `contentId` to turn the file into an inline part, and point at it from your * HTML with ``. */ function sendpierAttachment(path, contentType = null, contentId = null) { const attachment = { // Only the file name travels; a path in front of it is cut off anyway. filename: basename(path), content: readFileSync(path).toString('base64'), }; if (contentType !== null) { attachment.content_type = contentType; } if (contentId !== null) { attachment.content_id = contentId; } return attachment; } const recipient = process.argv[2]; const file = process.argv[3]; const from = process.env.SENDPIER_FROM; if (!recipient || !file || !from) { console.error('usage: SENDPIER_FROM=invoices@example.com node send-with-attachment.mjs '); process.exit(64); } const response = await sendpierRequest('POST', '/v1/messages', { from: { email: from, name: 'Example' }, to: [{ email: recipient }], subject: 'Your invoice', text: 'Your invoice is attached.', html: '

Your invoice is attached.

', attachments: [sendpierAttachment(file, 'application/pdf')], }); console.log(response.raw); if (response.status !== 202) { // 413 message_too_large means the whole message went over 25 MB; send big // files as a download link instead. 422 attachment_type_blocked means the // file type would never reach an inbox anyway -- the last extension counts, // so invoice.pdf.exe is an .exe. console.error(`request failed with HTTP ${response.status}`); process.exit(1); } for (const message of response.body?.data ?? []) { console.error(`queued ${message.id} for ${message.to}`); }