Skip to content

enniel/adonis-mail-notification-channel

Repository files navigation

Adonis Mail Notification Channel

Mail Notification Channel for adonis-notifications.

Build Status Coverage Status

Installation

  1. Add package:
$ npm i adonis-mail-notification-channel --save

or

$ yarn add adonis-mail-notification-channel
  1. Configure mail package.

See mail doc for more information

  1. Register provider inside the your start/app.js file.
const providers = [
  ...
  'adonis-mail-notification-channel/providers/MailNotificationChannelProvider',
  ...
]

Usage example

// app/Model/User.js
const Lucid = use('Lucid')

class User extends Lucid {
  static get traits () {
    return [
      '@provider:Notifiable'
    ]
  }

  /**
   *  // email
   *  [email protected]
   *
   *  // email + name
   *  { address: [email protected]', name: 'Foo' }
   *
   *  // Array
   *  [{ address: '[email protected]', name: 'Foo' }]
   */
  routeNotificationForEmail () {
    return this.email
  }
}

module.exports = User
// app/Notifications/MyNotification.js
const MailMessage = use('MailMessage')

class MyNotification {
  constructor (text) {
    this.text = text
  }

  via () {
    return ['mail']
  }

  toMail () {
    const message = new MailMessage()
    // You can set up configuration in message
    message
      .configure({
        connection: 'smtp',
        smtp: {
          driver: 'smtp',
          host: process.env.SMTP_HOST,
          port: process.env.SMTP_PORT,
          auth: {
            user: process.env.MAIL_USERNAME,
            pass: process.env.MAIL_PASSWORD
          }
        }
      })
    message
      .text(this.text)
      .subject('Message Notification Channel Test')
    return message
  }
}

module.exports = MyNotification
const Notifications = use('Notifications')

...
const users = await User.all()

await Notifications.send(users, new MyNotification(`It's works!!!`))
...

Credits

Support

Having trouble? Open an issue!

License

The MIT License (MIT). Please see License File for more information.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published