UNPKG

react-email-sender-adapters

Version:

A unified interface for sending react-email emails across multiple email services.

64 lines (63 loc) 3.67 kB
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; import { AwsClient } from 'aws4fetch'; import { createMimeMessage, Mailbox } from 'mimetext/browser'; import { encodeAttachmentToBase64, parseMail, quotedPrintableEncode } from './tools.js'; export function sendEmail(email, options) { return __awaiter(this, void 0, void 0, function* () { var _a, _b, _c; const mail = yield parseMail(email, options); if (!mail) return; const { from, to, cc, bcc, reply_to, text, html } = mail; const region = (_a = options.region) !== null && _a !== void 0 ? _a : process.env.AWS_SES_REGION; const accessKeyId = (_b = options.accessKeyId) !== null && _b !== void 0 ? _b : process.env.AWS_SES_ACCESS_KEY_ID; const secretAccessKey = (_c = options.secretAccessKey) !== null && _c !== void 0 ? _c : process.env.AWS_SES_SECRET_ACCESS_KEY; if (!region) throw new Error('No region provided for AWS SES. You can set the environment variable `AWS_SES_REGION`.'); if (!accessKeyId) throw new Error('No access key ID provided for AWS SES. You can set the environment variable `AWS_SES_ACCESS_KEY_ID`.'); if (!secretAccessKey) throw new Error('No secret access key provided for AWS SES. You can set the environment variable `AWS_SES_SECRET_ACCESS_KEY`.'); const aws = new AwsClient({ region, accessKeyId, secretAccessKey }); const msg = createMimeMessage(); msg.setSender({ addr: from.email, name: from.name }); msg.setRecipients(to.map(({ email, name }) => ({ addr: email, name }))); if (cc) msg.setCc(cc.map(({ email, name }) => ({ addr: email, name }))); if (bcc) msg.setBcc(bcc.map(({ email, name }) => ({ addr: email, name }))); if (reply_to) msg.setHeader('Reply-To', new Mailbox({ addr: reply_to.email, name: reply_to.name })); msg.setSubject(options.subject); msg.addMessage({ contentType: 'text/plain', data: quotedPrintableEncode(text), encoding: 'quoted-printable' }); msg.addMessage({ contentType: 'text/html', data: quotedPrintableEncode(html), encoding: 'quoted-printable' }); for (const attachment of options.attachments || []) { msg.addAttachment({ contentType: attachment.contentType, filename: attachment.filename, data: encodeAttachmentToBase64(attachment.content), }); } const result = yield aws.fetch(`https://email.${region}.amazonaws.com`, { method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded', }, body: new URLSearchParams({ Action: 'SendRawEmail', "RawMessage.Data": btoa(msg.asRaw()), }).toString(), }); if (result.status !== 200) { throw new Error(`Failed to send email: ${result.status} ${yield result.text()}`); } }); }