diff --git a/README.md b/README.md index bc4538b..6025acc 100644 --- a/README.md +++ b/README.md @@ -8,11 +8,34 @@ Using npm: `npm install strapi-provider-email-mailtrap --save` Using yarn: `yarn add strapi-provider-email-mailtrap` +## Prerequisites + +- You need a Mailtrap account: https://mailtrap.io +- From the inbox page get your `username` and `password`. + + ## How to use it -1. You need a Mailtrap account: https://mailtrap.io -2. From the inbox page get your `username` and `password`. -3. From your strapi admin panel go to the `plugins` page. -4. Click on the settings on the email row. -5. Chose `Mailtrap` as provider for your development/staging env. -6. Fill the form with your info. \ No newline at end of file +Instruct the email plugin to use the mailtrap provider. + +```javascript +module.exports = ({ env }) => ({ + // ... + email: { + provider: 'mailtrap', + providerOptions: { + user: env('MAILTRAP_USER', 'default_user'), + password: env('MAILTRAP_PASSWORD', 'default_pass') + }, + settings: { + defaultFrom: env('MAILTRAP_DEFAULT_FROM', 'default@value.com'), + defaultReplyTo: env('MAILTRAP_DEFAULT_REPLY_TO', 'default@value.com'), + }, + } + // ... +}); +``` + +If you want to use it in every environment, you should add the above code to `config/plugins.js`. + +If you want to use it in a certain environment, you should add the above code to `config/env/plugins.js`. diff --git a/lib/index.js b/lib/index.js index cdddea1..8380a0d 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,56 +1,31 @@ 'use strict'; -/** - * Module dependencies - */ - // Public node modules. const nodemailer = require('nodemailer'); const { removeUndefined } = require('strapi-utils'); -/* eslint-disable no-unused-vars */ module.exports = { - provider: 'mailtrap', - name: 'Mailtrap', - auth: { - mailtrapDefaultFrom: { - label: 'Mailtrap Default From', - type: 'text', - }, - mailtrapReplyTo: { - label: 'Mailtrap Reply To', - type: 'text', - }, - mailtrapUser: { - label: 'Mailtrap user', - type: 'text' - }, - mailtrapPassword: { - label: 'Mailtrap password', - type: 'text' - } - }, - init: (providerOptions= {}, settings= {}) => { + init: (providerOptions = {}, settings = {}) => { const transport = nodemailer.createTransport({ host: "smtp.mailtrap.io", port: 2525, auth: { - user: providerOptions.mailtrapUser || "", - pass: providerOptions.mailtrapPassword || "", + user: providerOptions.user || "", + pass: providerOptions.password || "", } }); return { - send: async options => { + send: options => { return new Promise((resolve, reject) => { const { from, to, cc, bcc, replyTo, subject, text, html, ...rest } = options; let msg = { - from: from || settings.mailtrapDefaultFrom, + from: from || settings.defaultFrom, to, cc, bcc, - replyTo: replyTo || settings.mailtrapReplyTo, + replyTo: replyTo || settings.defaultReplyTo, subject, text, html, @@ -60,9 +35,9 @@ module.exports = { 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' }] }]); + reject(err); } else { - strapi.log.debug('[strapi-provider-email-mailtrap] Mail sent. Id: ', info.messageId) + strapi.log.trace('[strapi-provider-email-mailtrap] Mail sent. Id: ', info.messageId) resolve(); } }); @@ -70,4 +45,4 @@ module.exports = { }, }; }, -}; +}; \ No newline at end of file diff --git a/package.json b/package.json index 4b66a1b..26e06e9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "strapi-provider-email-mailtrap", - "version": "3.0.1", + "version": "3.1.5", "description": "Mailtrap provider for strapi email", "keywords": [ "email",