-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
151 additions
and
4,606 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -63,3 +63,6 @@ profile* | |
|
||
# package-lock | ||
package-lock.json | ||
|
||
# node-tap | ||
.tap |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,19 @@ | ||
## fastify-mail | ||
# fastify-mail | ||
|
||
A [Fastify](https://www.fastify.io/) plugin that uses [point-of-view](https://github.com/fastify/point-of-view#readme) and [fastify-nodemailer](https://github.com/lependu/fastify-nodemailer#readme) to template and send email messages. | ||
|
||
### Usage | ||
## Usage | ||
|
||
```sh | ||
npm i @autotelic/fastify-mail | ||
``` | ||
|
||
### Setup | ||
## Setup | ||
|
||
`fastify-mail` decorates the reply interface with the `mail` method and takes two options to get started: `pov` and `transporter` | ||
|
||
##### Point-of-View | ||
### Point-of-View | ||
|
||
- `pov.engine` should be a template engine object used to configure point-of-view | ||
- to see a list of engines currently supported by point-of-view with options, [view docs here](https://github.com/fastify/point-of-view/blob/master/index.d.ts) | ||
- For quick start, `fastify-mail` only requires the engine. For example, using `nunjucks`: | ||
|
@@ -23,14 +24,17 @@ npm i @autotelic/fastify-mail | |
|
||
- If you'd prefer to register `point-of-view` on your own, omit the `engine` option and `fastify-mail` will not register it. | ||
- If you configure `point-of-view` with a different decorator name, add this to the options of `fastify-mail` | ||
|
||
```js | ||
fastify.register(mail, { pov: { propertyName: 'POV_DECORATOR' }, transporter: ... }) | ||
``` | ||
|
||
##### Nodemailer | ||
### Nodemailer | ||
|
||
- `transporter` should be an object defining connection data to be used as a `nodemailer` SMTP transport. [View nodemailer's docs here](https://nodemailer.com/smtp/) | ||
- `fastify-mail` decorates `fastify` with `nodemailer` so a transporter must be defined | ||
- For example, using `maildev`: | ||
|
||
```js | ||
const transporter = { | ||
host: 'localhost', | ||
|
@@ -41,7 +45,7 @@ npm i @autotelic/fastify-mail | |
fastify.register(mail, { pov: { engine: ... }, transporter }) | ||
``` | ||
|
||
### Example | ||
## Example | ||
|
||
```js | ||
// index.js | ||
|
@@ -78,69 +82,73 @@ fastify.get("/sendmail", async (req, reply) => { | |
}) | ||
``` | ||
|
||
#### Templates | ||
## Templates | ||
|
||
Each message must have the following templates with the file extension set in point-of-view config: | ||
- 'html': Contains the html template for the email. | ||
- 'text': Contains the text template for the email. | ||
|
||
``` | ||
- 'html': Contains the html template for the email. | ||
- 'text': Contains the text template for the email. | ||
|
||
```sh | ||
. | ||
|--index.js | ||
|--templates | ||
|-- html.njk | ||
|-- text.njk | ||
``` | ||
|
||
#### Example Apps | ||
## Example Apps | ||
|
||
See [/examples/mailgun](./examples/mailgun) for a working example app using [nodemailer-mailgun-transport](https://github.com/xr0master/mailgun-nodemailer-transport#readme). | ||
|
||
See [/examples/maildev](./examples/maildev) for a working example app using [MailDev](https://maildev.github.io/maildev/) | ||
|
||
### API | ||
## API | ||
|
||
#### Decorator | ||
### Decorator | ||
|
||
This plugin decorates fastify with a `mail` object containing the following methods: | ||
|
||
- sendMail: 'function' - Calls `createMessage` to generate an message and uses [fastify-nodemailer](https://github.com/lependu/fastify-nodemailer) to send the generated email. | ||
- Accepts the following arguments: | ||
- message: 'object' | ||
*This is a valid 'message' object as per the Nodemailer API* | ||
- from: 'string' - The email address the email is to be sent from. | ||
- to: 'array' - Comma separated list or an array of recipients email addresses (`string`) that will appear on the To: field | ||
- cc: 'array' - Comma separated list or an array of recipients email addresses (`string`) that will appear on the Cc: field | ||
- bcc: 'array' - Comma separated list or an array of recipients email addresses (`string`) that will appear on the Bcc: field | ||
- replyTo : 'string' - An email address that will appear on the Reply-To: field | ||
- subject: 'string' - The subject of the email with context injected. | ||
- html: 'string' - The HTML version of the message as an Unicode string, with context injected. | ||
- text : 'string' - The plaintext version of the message as an Unicode string, with context injected | ||
|
||
- opts: 'object' - Object containing options: | ||
- templatePath: 'string' - the relative path to the message's templates. | ||
- context: 'object' - Object containing context for the message (such as - variables that will be used in copy) | ||
|
||
- Returns: 'object' with following properties: | ||
- accepted : 'array' of email addresses accepted - eg. [ '[email protected]' ] | ||
- rejected : 'array' of email addresses rejected - eg. [], | ||
- envelopeTime | ||
- messageTime | ||
- messageSize | ||
- response | ||
- envelope | ||
- messageId | ||
- sendMail: 'function' - Calls `createMessage` to generate an message and uses [fastify-nodemailer](https://github.com/lependu/fastify-nodemailer) to send the generated email. | ||
- Accepts the following arguments: | ||
- message: 'object' | ||
*This is a valid 'message' object as per the Nodemailer API* | ||
- from: 'string' - The email address the email is to be sent from. | ||
- to: 'array' - Comma separated list or an array of recipients email addresses (`string`) that will appear on the To: field | ||
- cc: 'array' - Comma separated list or an array of recipients email addresses (`string`) that will appear on the Cc: field | ||
- bcc: 'array' - Comma separated list or an array of recipients email addresses (`string`) that will appear on the Bcc: field | ||
- replyTo : 'string' - An email address that will appear on the Reply-To: field | ||
- subject: 'string' - The subject of the email with context injected. | ||
- html: 'string' - The HTML version of the message as an Unicode string, with context injected. | ||
- text : 'string' - The plaintext version of the message as an Unicode string, with context injected | ||
|
||
- opts: 'object' - Object containing options: | ||
- templatePath: 'string' - the relative path to the message's templates. | ||
- context: 'object' - Object containing context for the message (such as - variables that will be used in copy) | ||
|
||
- Returns: 'object' with following properties: | ||
- accepted : 'array' of email addresses accepted - eg. [ '[email protected]' ] | ||
- rejected : 'array' of email addresses rejected - eg. [], | ||
- envelopeTime | ||
- messageTime | ||
- messageSize | ||
- response | ||
- envelope | ||
- messageId | ||
|
||
- createMessage: 'function' - Generates a message object where the data provided is updated to use templates where available with context variables injected | ||
- Accepts the following arguments: | ||
- Accepts the following arguments: | ||
- message: 'object' | ||
- fields as above | ||
- templatePath: 'string' - the relative path to the message's templates. | ||
- context: 'object' - Object containing context for the message (such as - variables that will be used in copy) | ||
|
||
For more details on this response see the Nodemail documentation [View nodemailer's docs here](https://nodemailer.com/smtp/) | ||
For more details on this response see the nodemailer documentation [View nodemailer's docs here](https://nodemailer.com/smtp/) | ||
|
||
### Testing | ||
|
||
[Tap](https://node-tap.org/) is used for testing. To run tests: | ||
|
||
```sh | ||
npm test | ||
``` | ||
$ npm test | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,7 +45,7 @@ module.exports = async function (fastify, options) { | |
'</html>\n' | ||
|
||
const message = { | ||
to: ['[email protected]'], | ||
to: '[email protected]', | ||
from: '[email protected]', | ||
subject: 'Test Subject', | ||
html: testHtml, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
export MAILGUN_API_KEY=1234 | ||
export MAILGUN_DOMAIN=sandbox...mailgun.org | ||
export RECIPIENTS=[email protected] | ||
export SENDER=[email protected] | ||
export RECIPIENT=[email protected] | ||
export SENDER=[email protected] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.