Code Monkey home page Code Monkey logo

feathers-mailer's Introduction

feathers-mailer

Node.js CI Dependency Status Download Status

Feathers mailer service using nodemailer

Installation

npm install feathers-mailer --save

If using a transport plugin, install the respective module.

API

const mailer = require('feathers-mailer');

app.use('/emails', mailer(transport, defaults))

  • transport can be either SMTP options or a transport plugin with associated options.
  • defaults is an object that defines default values for mail options.

service.create(body, params)

service.create is a thin wrapper for transporter.sendMail, accepting body and returning a promise.

See here for possible fields of body.

Example with SMTP (ethereal)

const mailer = require('feathers-mailer');
const nodemailer = require('nodemailer');

(async function (app) {
  const account = await nodemailer.createTestAccount(); // internet required

  const transporter = {
    host: account.smtp.host,
    port: account.smtp.port,
    secure: account.smtp.secure, // 487 only
    requireTLS: true,
    auth: {
      user: account.user, // generated ethereal user
      pass: account.pass // generated ethereal password
    }
  };

  // Register service and setting default From Email
  app.use('mailer', mailer(transporter, { from: account.user }));

  // Use the service
  const email = {
     to: '[email protected]',
     subject: 'SMTP test',
     html: 'This is the email body'
  };

  await app.service('mailer').create(email)
  console.log(`Preview URL: ${nodemailer.getTestMessageUrl(info)}`)
})(app)

Example with Mandrill

const mailer = require('feathers-mailer');
const mandrill = require('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
const email = {
   from: 'FROM_EMAIL',
   to: 'TO_EMAIL',
   subject: 'Mandrill 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);
});

License

Copyright (c) 2018

Licensed under the MIT license.

feathers-mailer's People

Contributors

ahdinosaur avatar bertho-zero avatar corymsmith avatar daffl avatar fratzinger avatar greenkeeper[bot] avatar j2l4e avatar rainum avatar robbiedhickey avatar shakir-abdo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

feathers-mailer's Issues

find hook for mailer service is missing

To reproduce:

  • Install feathers
  • generate app
  • upgrade to dove
  • install this mailer package
  • user this package as a service like below:
// Initializes the `mailer` service on path `/mailer`
const hooks = require('./mailer.hooks')
const mailer = require('feathers-mailer')

module.exports = function (app) {
  const transporter = app.get('mailer')
  if (transporter.dkim.domainName === 'SITE_DOMAIN') delete transporter.dkim

  // Initialize our service with any options it requires
  app.use('/api/mailer', mailer(transporter, { from: transporter.auth.user }))

  // Get our initialized service so that we can register hooks
  const service = app.service('api/mailer')

  service.hooks(hooks)
}

Causes the following error with 5.0.0-pre.15 and up:

[nodemon] starting `node server/ --trace-warnings --abort-on-uncaught-exception`
/home/marcgodard/Documents/Github/template/node_modules/@feathersjs/feathers/lib/hooks/regular.js:122
                throw new Error(`'${method}' is not a valid hook method`);
                ^

Error: 'find' is not a valid hook method
    at /home/marcgodard/Documents/Github/template/node_modules/@feathersjs/feathers/lib/hooks/regular.js:122:23
    at Array.forEach (<anonymous>)
    at /home/marcgodard/Documents/Github/template/node_modules/@feathersjs/feathers/lib/hooks/regular.js:120:27
    at Array.forEach (<anonymous>)
    at createMap (/home/marcgodard/Documents/Github/template/node_modules/@feathersjs/feathers/lib/hooks/regular.js:115:24)
    at Service.regularHooks (/home/marcgodard/Documents/Github/template/node_modules/@feathersjs/feathers/lib/hooks/regular.js:163:21)
    at Service.service.hooks (/home/marcgodard/Documents/Github/template/node_modules/@feathersjs/feathers/lib/hooks/index.js:79:39)
    at Function.module.exports (/home/marcgodard/Documents/Github/template/server/services/mailer/mailer.service.js:27:11)
    at Function.configure (/home/marcgodard/Documents/Github/template/node_modules/@feathersjs/feathers/lib/application.js:43:18)
    at Function.module.exports (/home/marcgodard/Documents/Github/template/server/services/index.js:109:7)
    at Function.configure (/home/marcgodard/Documents/Github/template/node_modules/@feathersjs/feathers/lib/application.js:43:18)
    at Object.<anonymous> (/home/marcgodard/Documents/Github/template/server/app.js:81:5)
    at Module._compile (node:internal/modules/cjs/loader:1103:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1155:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
[nodemon] app crashed - waiting for file changes before starting...

Uberproto missing in package.json

I'm not sure if this is a mistake or not, but uberproto is being used here but is not required in package.json.

This only causes issues when I'm deploying my app, running locally for dev is fine - but I assume that's because of other feathers packages I'm using, or potentially a dev dependency I have loaded that isn't installed on deploy.

How to handle uncatchable promises error.

Thanks for this really simple to use Mailer. It is really nice to use and works great with SMTP, mandrill or Mailjet (this are the ones, tested so far.)

I have an issue that I do not know how to properly handle without rewriting the service.

When mandrill is not reachable or you misconfigured the service, the Promise after the create will be returned as is and so will never resolve. Instead it fail and throw an error.

As the service only return the promise without catching anything, I do not understand how we are supposed to handle errors at the application level.

Thanks,

Steps to reproduce

To reproduce:

  1. Develop locally, cut your network connection and try to send to mandrill
  2. Misconfigured your smtp value
  3. Wait for mandrill to be down (happened a lot recently) :)

Expected behavior

The error should be catched and the route should response with the correct error code :

500, 403, what ever

Actual behavior

The response is never send and client timeout

System configuration

Simple example app in README over REST POST.

Module versions : latest

NodeJS version: 8.9.1

Operating System: macOS High Sierra

Browser Version: Postman / cURL

I created typings for this package - where to put them?

Hi, I created types definition for this package. Should I upload them to @types or create a PR for the types to be put inside the lib?

For reference, I copy the typings here:

declare module 'feathers-mailer' {
  import SMTPTransport from 'nodemailer/lib/smtp-transport'
  import {SendMailOptions, SentMessageInfo} from 'nodemailer'
  import JSONTransport from 'nodemailer/lib/json-transport'
  import SMTPPool from 'nodemailer/lib/smtp-pool'
  import SESTransport from 'nodemailer/lib/ses-transport'
  import StreamTransport from 'nodemailer/lib/stream-transport'
  import SendmailTransport from 'nodemailer/lib/sendmail-transport'

  export class Service {
    constructor(transport?: SMTPTransport | SMTPTransport.Options | string, defaults?: SMTPTransport.Options)
    constructor(transport?: SMTPPool | SMTPPool.Options, defaults?: SMTPPool.Options)
    constructor(transport?: SendmailTransport | SendmailTransport.Options, defaults?: SendmailTransport.Options)
    constructor(transport?: StreamTransport | StreamTransport.Options, defaults?: StreamTransport.Options)
    constructor(transport?: JSONTransport | JSONTransport.Options, defaults?: JSONTransport.Options)
    constructor(transport?: SESTransport | SESTransport.Options, defaults?: SESTransport.Options)

    extend(obj: Record<string, unknown>): Record<string, unknown>

    create(body: SendMailOptions, params?: never): Promise<SentMessageInfo>
  }

  export default function init(transport?: SMTPTransport | SMTPTransport.Options | string, defaults?: SMTPTransport.Options): Service
  export default function init(transport?: SMTPPool | SMTPPool.Options, defaults?: SMTPPool.Options): Service
  export default function init(transport?: SendmailTransport | SendmailTransport.Options, defaults?: SendmailTransport.Options): Service
  export default function init(transport?: StreamTransport | StreamTransport.Options, defaults?: StreamTransport.Options): Service
  export default function init(transport?: JSONTransport | JSONTransport.Options, defaults?: JSONTransport.Options): Service
  export default function init(transport?: SESTransport | SESTransport.Options, defaults?: SESTransport.Options): Service
}

out-of-date dependencies in npm package

Expected behavior

npm i feathers-mailer should fetch the latest dependencies (as defined in package):

"dependencies": {
  "debug": "^4.3.1",
  "nodemailer": "^6.6.0",
  "uberproto": "^2.0.6"
}

Actual behavior

The last update to the npm package was 3 years ago (npm version, git commit 5da0ebd) and fetches the following dependencies:

"dependencies": {
  "debug": "^3.1.0",
  "nodemailer": "^4.4.2",
  "uberproto": "^2.0.0"
}

The main reason for opening this issue is that npm is showing a command injection advisory for an old version of nodemailer when installing the package:

> npm install feathers-mailer
> npm audit
# npm audit report

nodemailer  <6.4.16
Severity: critical
Command Injection - https://npmjs.com/advisories/1708
No fix available
node_modules/nodemailer
  feathers-mailer  *
  Depends on vulnerable versions of nodemailer
  node_modules/feathers-mailer

2 critical severity vulnerabilities

Some issues need review, and may require choosing
a different dependency.

System configuration

Module versions: v3.0.1

NodeJS version: 14.14.0

Operating System: Ubuntu 20

Browser Version: N/A

[Proposal] Extended mailer

This mailer is very simple, I think that many of us need more than that to send their emails, I think of a template management by MJML and EJS combined.

MJML makes it easy to create compatible emails in different clients and EJS makes them dynamic. Here is an example (few lines) of what I started to do:

https://github.com/Oagenda/mails

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.