UNPKG

@visulima/email

Version:

A comprehensive email library with multi-provider support, crypto utilities, and template engines

2 lines (1 loc) 4.3 kB
var P=Object.defineProperty;var h=(s,a)=>P(s,"name",{value:a,configurable:!0});import{createRequire as A}from"node:module";import c from"./EmailError-zm2ffVav.js";import S from"./RequiredOptionError-CevW3u2K.js";import E from"./generateMessageId-11Ls5JsR.js";import K from"./headersToRecord-BKUTr40L.js";import{makeRequest as y}from"./makeRequest-DwxHX0xo.js";import b from"./retry-D1MBqS49.js";import k from"./validateEmailOptions-BzlJECG5.js";import{defineProvider as q}from"./defineProvider-B9rSklAJ.js";import{c as z,P as C,h as w}from"./provider-base-_hbWXBdK.js";import{f,a as M}from"./address-formatter-Cm_E_ZMa.js";const j=A(import.meta.url),u=typeof globalThis<"u"&&typeof globalThis.process<"u"?globalThis.process:process,T=h(s=>{if(typeof u<"u"&&u.versions&&u.versions.node){const[a,l]=u.versions.node.split(".").map(Number);if(a>22||a===22&&l>=3||a===20&&l>=16)return u.getBuiltinModule(s)}return j(s)},"__cjs_getBuiltinModule"),{Buffer:_}=T("node:buffer"),i="mailomat",x="https://api.mailomat.com",R=3e4,B=3,Q=q((s={})=>{if(!s.apiKey)throw new S(i,"apiKey");const a={apiKey:s.apiKey,debug:s.debug||!1,endpoint:s.endpoint||x,retries:s.retries||B,timeout:s.timeout||R,...s.logger&&{logger:s.logger}},l=new C,n=z(i,s.logger);return{features:{attachments:!0,batchSending:!0,customHeaders:!0,html:!0,replyTo:!0,scheduling:!1,tagging:!0,templates:!0,tracking:!0},async getEmail(e){try{if(!e)return{error:new c(i,"Email ID is required to retrieve email details"),success:!1};await l.ensureInitialized(()=>this.initialize(),i);const o={"Content-Type":"application/json","X-API-Key":a.apiKey};n.debug("Retrieving email details",{id:e});const t=await b(async()=>y(`${a.endpoint}/emails/${e}`,{headers:o,method:"GET",timeout:a.timeout}),a.retries);return t.success?(n.debug("Email details retrieved successfully"),{data:t.data?.body,success:!0}):(n.debug("API request failed when retrieving email",t.error),{error:new c(i,`Failed to retrieve email: ${t.error instanceof Error?t.error.message:"Unknown error"}`,{cause:t.error}),success:!1})}catch(o){return{error:w(i,"retrieve email",o,n),success:!1}}},async initialize(){await l.ensureInitialized(async()=>{if(!await this.isAvailable())throw new c(i,"Mailomat API not available or invalid API key");n.debug("Provider initialized successfully")},i)},async isAvailable(){try{return n.debug("Checking Mailomat API availability"),!0}catch(e){return n.debug("Error checking availability:",e),!1}},name:i,options:a,async sendEmail(e){try{const o=k(e);if(o.length>0)return{error:new c(i,`Invalid email options: ${o.join(", ")}`),success:!1};await l.ensureInitialized(()=>this.initialize(),i);const t={from:{email:e.from.email,name:e.from.name},subject:e.subject,to:f(e.to)};if(e.html&&(t.html=e.html),e.text&&(t.text=e.text),e.cc&&(t.cc=f(e.cc)),e.bcc&&(t.bcc=f(e.bcc)),e.replyTo&&(t.replyTo=M(e.replyTo)),e.templateId&&(t.templateId=e.templateId,e.templateVariables&&(t.templateVariables=e.templateVariables)),e.tags&&e.tags.length>0&&(t.tags=e.tags),e.headers){const r=K(e.headers);t.headers=r}e.attachments&&e.attachments.length>0&&(t.attachments=await Promise.all(e.attachments.map(async r=>{let d;if(r.content)if(typeof r.content=="string")d=r.content;else if(r.content instanceof Promise){const I=await r.content;d=_.from(I).toString("base64")}else d=r.content.toString("base64");else if(r.raw)d=typeof r.raw=="string"?r.raw:r.raw.toString("base64");else throw new c(i,`Attachment ${r.filename} has no content`);return{content:d,contentType:r.contentType||"application/octet-stream",filename:r.filename,...r.cid&&{cid:r.cid}}}))),n.debug("Sending email via Mailomat API",{subject:t.subject,to:t.to});const v={"Content-Type":"application/json","X-API-Key":a.apiKey},m=await b(async()=>y(`${a.endpoint}/send`,{headers:v,method:"POST",timeout:a.timeout},JSON.stringify(t)),a.retries);if(!m.success)return n.debug("API request failed when sending email",m.error),{error:m.error||new c(i,"Failed to send email"),success:!1};const p=m.data?.body,g=p?.messageId||p?.id||E();return n.debug("Email sent successfully",{messageId:g}),{data:{messageId:g,provider:i,response:m.data,sent:!0,timestamp:new Date},success:!0}}catch(o){return{error:w(i,"send email",o,n),success:!1}}},async validateCredentials(){return this.isAvailable()}}});export{Q as default};