Skip to content

Commit

Permalink
fix: refactored in order to support strapi 3.15
Browse files Browse the repository at this point in the history
  • Loading branch information
Alessandro Caprarelli committed Sep 17, 2020
1 parent 2ac7723 commit 9970451
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 41 deletions.
35 changes: 29 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
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', '[email protected]'),
defaultReplyTo: env('MAILTRAP_DEFAULT_REPLY_TO', '[email protected]'),
},
}
// ...
});
```

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`.
43 changes: 9 additions & 34 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -60,14 +35,14 @@ 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();
}
});
});
},
};
},
};
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down

0 comments on commit 9970451

Please sign in to comment.