diff --git a/lib/index.js b/lib/index.js index 598de64..cdddea1 100644 --- a/lib/index.js +++ b/lib/index.js @@ -5,58 +5,69 @@ */ // Public node modules. -const nodemailer = require('nodemailer') +const nodemailer = require('nodemailer'); +const { removeUndefined } = require('strapi-utils'); /* eslint-disable no-unused-vars */ module.exports = { provider: 'mailtrap', name: 'Mailtrap', auth: { - mailtrap_default_from: { + mailtrapDefaultFrom: { label: 'Mailtrap Default From', type: 'text', }, - mailtrap_user: { + mailtrapReplyTo: { + label: 'Mailtrap Reply To', + type: 'text', + }, + mailtrapUser: { label: 'Mailtrap user', type: 'text' }, - mailtrap_password: { + mailtrapPassword: { label: 'Mailtrap password', type: 'text' } }, - init: config => { + init: (providerOptions= {}, settings= {}) => { const transport = nodemailer.createTransport({ host: "smtp.mailtrap.io", port: 2525, auth: { - user: config.mailtrap_user || "", - pass: config.mailtrap_password || "" + user: providerOptions.mailtrapUser || "", + pass: providerOptions.mailtrapPassword || "", } - }) + }); return { - send: options => { + send: async options => { return new Promise((resolve, reject) => { - // Default values. - options = (typeof options === 'object' && options !== null) ? options : {}; - options.from = options.from || config.mailtrap_default_from; - options.text = options.text || options.html; - options.html = options.html || options.text; + const { from, to, cc, bcc, replyTo, subject, text, html, ...rest } = options; + + let msg = { + from: from || settings.mailtrapDefaultFrom, + to, + cc, + bcc, + replyTo: replyTo || settings.mailtrapReplyTo, + subject, + text, + html, + ...rest, + }; - transport.sendMail(options, - function(err, info) { - if (err) { - strapi.log.error('[strapi-provider-email-mailtrap] err', err) - reject([{ messages: [{ id: 'Auth.form.error.email.invalid' }] }]); - } else { - strapi.log.debug('[strapi-provider-email-mailtrap] Mail sent. Id: ', info.messageId) - resolve(); - } + transport.sendMail(removeUndefined(msg), (err, info) => { + if (err) { + strapi.log.error('[strapi-provider-email-mailtrap] err', err) + reject([{ messages: [{ id: 'Auth.form.error.email.invalid' }] }]); + } else { + strapi.log.debug('[strapi-provider-email-mailtrap] Mail sent. Id: ', info.messageId) + resolve(); } - ); + }); }); }, }; }, -}; \ No newline at end of file +}; diff --git a/package.json b/package.json index 6596906..4b66a1b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "strapi-provider-email-mailtrap", - "version": "1.0.1", + "version": "3.0.1", "description": "Mailtrap provider for strapi email", "keywords": [ "email", @@ -20,7 +20,7 @@ }, "main": "./lib", "dependencies": { - "nodemailer": "^6.4.6" + "nodemailer": "^6.4.8" }, "author": { "name": "Alessandro Caprarelli",