@visulima/email
Version:
A comprehensive email library with multi-provider support, crypto utilities, and template engines
3 lines (2 loc) • 2.99 kB
JavaScript
var v=Object.defineProperty;var p=(e,o)=>v(e,"name",{value:o,configurable:!0});import C from"./EmailError-zm2ffVav.js";import d from"./formatEmailAddress-CHeME3Vk.js";import l from"./formatEmailAddresses-UegVOe5A.js";import w from"./generateBoundary-CZ8kJuY6.js";import x from"./headersToRecord-BKUTr40L.js";import{s as h,a as b}from"./sanitize-header-wWav-Scu.js";var B=Object.defineProperty,E=p((e,o)=>B(e,"name",{value:o,configurable:!0}),"b");const m=globalThis.Buffer!==void 0,g=E(e=>{if(typeof e=="string"){if(m)return Buffer.from(e,"utf8").toString("base64");const t=new TextEncoder().encode(e);let r="";const a=8192;for(let s=0;s<t.length;s+=a){const f=t.subarray(s,Math.min(s+a,t.length));r+=String.fromCharCode.apply(void 0,f)}return btoa(r)}if(m&&e instanceof Buffer)return e.toString("base64");const o=e instanceof Uint8Array?e:new Uint8Array(e);if(m)return Buffer.from(o).toString("base64");let n="";const c=8192;for(let t=0;t<o.length;t+=c){const r=o.subarray(t,Math.min(t+c,o.length));n+=String.fromCharCode.apply(void 0,r)}return btoa(n)},"toBase64");var M=Object.defineProperty,j=p((e,o)=>M(e,"name",{value:o,configurable:!0}),"h");const S=globalThis.Buffer!==void 0,I=j(async e=>{const o=w(),n=[`From: ${d(e.from)}`,`To: ${l(e.to)}`];if(e.cc&&n.push(`Cc: ${l(e.cc)}`),e.bcc&&n.push(`Bcc: ${l(e.bcc)}`),e.replyTo&&n.push(`Reply-To: ${d(e.replyTo)}`),n.push(`Subject: ${h(e.subject)}`,"MIME-Version: 1.0"),e.headers){const c=x(e.headers);Object.entries(c).forEach(([t,r])=>{const a=b(t),s=h(r);n.push(`${a}: ${s}`)})}if(n.push(`Content-Type: multipart/mixed; boundary="${o}"`,""),e.text&&n.push(`--${o}`,"Content-Type: text/plain; charset=UTF-8","Content-Transfer-Encoding: 7bit","",e.text,""),e.html&&n.push(`--${o}`,"Content-Type: text/html; charset=UTF-8","Content-Transfer-Encoding: 7bit","",e.html,""),e.attachments&&e.attachments.length>0){const c=await Promise.all(e.attachments.map(async t=>{let r;if(t.raw!==void 0)r=t.raw;else{if(t.content===void 0)throw new C("attachment",`Attachment '${t.filename}' must have content, raw, or be resolved from path/href before building MIME message`);r=t.content instanceof Promise?await t.content:t.content}return{...t,resolvedContent:r}}));for(const t of c){n.push(`--${o}`);const r=t.contentType||"application/octet-stream",a=h(t.filename);n.push(`Content-Type: ${r}; name="${a}"`);const s=t.contentDisposition||"attachment";n.push(`Content-Disposition: ${s}; filename="${a}"`),t.cid&&n.push(`Content-ID: <${t.cid}>`),t.headers&&Object.entries(t.headers).forEach(([u,$])=>{const y=b(u),T=h($);n.push(`${y}: ${T}`)});const f=t.encoding||"base64";n.push(`Content-Transfer-Encoding: ${f}`,"");const i=t.resolvedContent;if(f==="base64")n.push(g(i));else if(f==="7bit"||f==="8bit")if(typeof i=="string")n.push(i);else if(S&&i instanceof Buffer)n.push(i.toString("utf8"));else{const u=new TextDecoder;n.push(u.decode(i))}else n.push(g(i));n.push("")}}return n.push(`--${o}--`),n.join(`\r
`)},"buildMimeMessage");export{I as default};