Code Monkey home page Code Monkey logo

mailhog-node's Introduction

MailHog NodeJS library

Initialization

The mailhog module returns an initialization function.
This function accepts an options object that must include the apiURL.
Currently this library requires the MailHog API v2:

const mailhog = require('./mailhog')({
  apiURL: 'http://mailhog:8025/api/v2'
})

Replace mailhog in the apiURL with the hostname to your MailHog instance.

The object returned by the initialization function is the mailhog API object that is used in the following examples.

Methods

search

Sends a search request to the MailHog API.
Returns a list of emails objects.

Usage

mailhog.search(query, kind, start, limit)
  .then(function (result) {})
  .catch(function (error) {})
  • query is the search query string
  • kind can be from|to|containing, defaults to "containing"
  • start defines the start index of the search (default: 0)
  • limit defines the max number of results (default: 50)

Example

mailhog.search('example.org').then(function (result) {
  console.log(result)
}).catch(function (error) {
  console.error(error)
})

getText

Returns the text content part of the given email object.

Usage

mailhog.getText(mail)
  • mail is an object returned by MailHog for an email message

Example

mailhog.search('example.org').then(function (result) {
  for (let mail of result.items) {
    console.log(mailhog.getText(mail))
  }
}).catch(function (error) {
  console.error(error)
})

getHTML

Returns the HTML content part of the given email object.

Usage

mailhog.getHTML(mail)
  • mail is an object returned by MailHog for an email message

Example

mailhog.search('example.org').then(function (result) {
  for (let mail of result.items) {
    console.log(mailhog.getHTML(mail))
  }
}).catch(function (error) {
  console.error(error)
})

getLatest

Retrieves the latest message content for the given query.
Returns a promise that resolves with the email content as result.

Usage

mailhog.getLatest(query, plainText, kind)
  .then(function (result) {})
  .catch(function (error) {})
  • query is the search query string
  • plainText (boolean) defines if text (true) or HTML (false) is returned
  • kind can be from|to|containing, defaults to "to"

Returns HTML unless plainText is true or there is no HTML content.

Example

mailhog.getLatest('[email protected]').then(function (result) {
  console.log(result)
}).catch(function (error) {
  console.error(error)
})

License

Released under the MIT license.

Author

Sebastian Tschan

mailhog-node's People

Contributors

blueimp avatar

Watchers

 avatar  avatar

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.