Skip to content

Commit

Permalink
:shipit:
Browse files Browse the repository at this point in the history
  • Loading branch information
ahdinosaur committed Feb 29, 2016
1 parent 2d52613 commit 21c249d
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 35 deletions.
59 changes: 55 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,70 @@

[![Build Status](https://travis-ci.org/ahdinosaur/feathers-mailer.png?branch=master)](https://travis-ci.org/ahdinosaur/feathers-mailer)

> Feathers mailer service
> Feathers mailer service using [`nodemailer`](https://github.com/nodemailer/nodemailer)
## About
## Installation

```shell
npm install feathers-mailer --save
```

If using a [transport plugin](https://github.com/nodemailer/nodemailer#send-using-a-transport-plugin), install the respective module.


## API

### `import Mailer from 'feathers-mailer'`

### `mailer = Mailer(transport, defaults)`

- `transport` can be either [SMTP options](https://github.com/nodemailer/nodemailer#set-up-smtp) or a [transport plugin](https://github.com/nodemailer/nodemailer#send-using-a-transport-plugin) with associated options.
- `defaults` is an object that defines default values for mail options.

### `mailer.create(body, params)`

`mailer.create` is a thin wrapper for [`transporter.sendMail`](https://github.com/nodemailer/nodemailer#sending-mail), accepting `body` and returning `err` and `info`.

See [here](https://github.com/nodemailer/nodemailer#e-mail-message-fields) for possible fields of `body`.

## Example

```js
import Mailer from 'feathers-mailer'
import mandrill from 'nodemailer-mandrill-transport'

// Register the service, see below for an example
app.use('/mailer', Mailer(mandrill({
auth: {
apiKey: process.env.MANDRILL_API_KEY
}
})));

// Use the service
var email = {
from: 'FROM_EMAIL',
to: 'TO_EMAIL',
subject: 'Sendgrid test',
html: 'This is the email body'
};

app.service('mailer').create(email).then(function (result) {
console.log('Sent email', result);
}).catch(err => {
console.log(err);
});
```

For a more complete example, see [examples/app](./examples/app.js) which can be run with `npm run example`.

## Changelog

__0.1.0__
__1.0.0__

- Initial release

## License

Copyright (c) 2015
Copyright (c) 2016

Licensed under the [MIT license](LICENSE).
20 changes: 9 additions & 11 deletions examples/app.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import mandrillTransport from 'nodemailer-mandrill-transport'
import createMailer from '../src'
import mandrill from 'nodemailer-mandrill-transport'
import Mailer from '../src'
import feathers from 'feathers'
import rest from 'feathers-rest';
import bodyParser from 'body-parser';

const mailerService = createMailer({
Model: mandrillTransport({
auth: {
apiKey: process.env.MANDRILL_API_KEY || 'notakey'
}
})
});
const mailer = Mailer(mandrill({
auth: {
apiKey: process.env.MANDRILL_API_KEY || 'notakey'
}
}));

// Create a feathers instance.
var app = feathers()
Expand All @@ -20,7 +18,7 @@ var app = feathers()
.use(bodyParser.json())
// Turn on URL-encoded parser for REST services
.use(bodyParser.urlencoded({extended: true}))
.use('/mails', mailerService);
.use('/mailer', mailer);

// A basic error handler, just like Express
app.use(function(error, req, res, next){
Expand All @@ -30,4 +28,4 @@ app.use(function(error, req, res, next){
// Start the server
module.exports = app.listen(3030);

console.log('feathers-mailer service running on 127.0.0.1:3030/mails');
console.log('feathers-mailer service running on 127.0.0.1:3030/mailer');
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "feathers-mailer",
"description": "Feathers mailer service",
"version": "0.0.0",
"version": "1.0.0",
"homepage": "https://github.com/ahdinosaur/feathers-mailer",
"main": "lib/",
"keywords": [
Expand Down
24 changes: 8 additions & 16 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,13 @@ import Mailer from 'nodemailer';
const debug = makeDebug('feathers-mailer');

class Service {
constructor (options) {
debug('constructor', options);
if (!options) {
throw new Error('feathers-mailer: constructor `options` must be provided');
constructor (transport, defaults) {
debug('constructor', transport);
if (!transport) {
throw new Error('feathers-mailer: constructor `transport` must be provided');
}

if (!options.Model) {
throw new Error('feathers-mailer: constructor `options.Model` must be provided');
}

this.Model = Mailer.createTransport(
options.Model,
options.defaults
);
this.id = options.id || 'id';
this.transporter = Mailer.createTransport(transport, defaults);
}

extend (obj) {
Expand All @@ -33,12 +25,12 @@ class Service {
debug('create', body, params);
// TODO maybe body should be text/html field
// and params is rest of options
this.Model.sendMail(body, cb);
this.transporter.sendMail(body, cb);
}
}

export default function init(options) {
return new Service(options);
export default function init(transport, defaults) {
return new Service(transport, defaults);
}

init.Service = Service;
4 changes: 1 addition & 3 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ describe('feathers-mailer', () => {

it('basic functionality', done => {
assert.equal(typeof createMailer, 'function', 'exports function');
const mailer = createMailer({
Model: stubTransport()
});
const mailer = createMailer(stubTransport());
let mailData = {
from: '"Alice" <alice@example.com>',
to: ['bob@example.com', '"Carol" <carol@example.com>'],
Expand Down

0 comments on commit 21c249d

Please sign in to comment.