UNPKG

@piksail/strapi-provider-email-mailpit

Version:
135 lines (123 loc) 4.09 kB
module.exports = { init(providerOptions, settings) { settings.verbose = settings.verbose || false; return { send: async (options) => { const { from, to, subject, html, text, cc, bcc, tags, replyTo, ...rest } = options; function checkArray(arr) { if (arr === undefined || arr === null) { return []; } if (typeof arr === "string") { if (arr.includes(",")) { return arr.split(","); } return [arr]; } return arr; } function checkEmailArray(arr) { if (arr === undefined || arr === null) { return []; } if (typeof arr === "string") { if (arr.includes(",")) { return arr.split(",").map((email) => ({ email: email.trim(), name: "", })); } return [{ email: arr, name: "" }]; } if (arr.length > 0 && typeof arr[0] === "string") { throw new Error( "Must be an array of objects with email and name: { email: '', name: '' } or a comma separated string 'test@example.com,test2@example.com'" ); } return arr; } if (settings.verbose) { // mailpit settings without headers object const { headers, ...restSettings } = settings || {}; console.log("Mailpit settings without headers:", restSettings); // mailpit headers settings without Authorization const { Authorization, ...restHeadersWithoutAuth } = headers || {}; console.log( "Mailpit headers settings without Authorization:", restHeadersWithoutAuth ); console.log("Mailpit options:", options); } let toArr = checkEmailArray(to); let replyToArr = checkEmailArray(replyTo); let ccArr = checkEmailArray(cc); let bccArr = checkEmailArray(bcc); let tagsArr = checkArray(tags); const mailpitPayload = { from: from || { email: settings.defaultFrom, name: "" }, replyTo: replyToArr, to: toArr, cc: ccArr, bcc: bccArr, subject: subject || "", text: text || "", html: html || "", tags: tagsArr, ...rest, }; if (settings.verbose) { console.log("Mailpit Payload:", mailpitPayload); } let mailpitUrl = "http://localhost:8025/api/v1/send"; if (providerOptions.baseUrl) { if (providerOptions.baseUrl.endsWith("/")) { providerOptions.baseUrl = providerOptions.baseUrl.slice(0, -1); } mailpitUrl = `${providerOptions.baseUrl}/api/v1/send`; } if (settings.verbose) { console.log("Mailpit Base Url:", mailpitUrl); } // Merge default Content-Type with any custom headers from settings.headers const customHeaders = settings.headers && typeof settings.headers === "object" ? settings.headers : {}; const headers = { "Content-Type": "application/json", ...customHeaders, }; if (settings.verbose) { const { Authorization, ...restHeaders } = headers || {}; console.log("Mailpit Headers without Authorization:", restHeaders); } const response = await fetch(mailpitUrl, { method: "POST", headers, body: JSON.stringify(mailpitPayload), }); if (!response.ok) { const error = await response.text(); throw new Error(`Mailpit send failed: ${error}`); } else { const responseBody = await response.json(); if (settings.verbose) { console.log("Mailpit Response:", responseBody); } return responseBody; } }, }; }, };