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.
Sends a search request to the MailHog API.
Returns a list of emails objects.
mailhog.search(query, kind, start, limit)
.then(function (result) {})
.catch(function (error) {})
query
is the search query stringkind
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
)
mailhog.search('example.org').then(function (result) {
console.log(result)
}).catch(function (error) {
console.error(error)
})
Returns the text content part of the given email object.
mailhog.getText(mail)
mail
is an object returned by MailHog for an email message
mailhog.search('example.org').then(function (result) {
for (let mail of result.items) {
console.log(mailhog.getText(mail))
}
}).catch(function (error) {
console.error(error)
})
Returns the HTML content part of the given email object.
mailhog.getHTML(mail)
mail
is an object returned by MailHog for an email message
mailhog.search('example.org').then(function (result) {
for (let mail of result.items) {
console.log(mailhog.getHTML(mail))
}
}).catch(function (error) {
console.error(error)
})
Retrieves the latest message content for the given query.
Returns a promise that resolves with the email content as result.
mailhog.getLatest(query, plainText, kind)
.then(function (result) {})
.catch(function (error) {})
query
is the search query stringplainText
(boolean) defines if text (true
) or HTML (false
) is returnedkind
can befrom|to|containing
, defaults to"to"
Returns HTML unless plainText
is true
or there is no HTML content.
mailhog.getLatest('nihon@example.org').then(function (result) {
console.log(result)
}).catch(function (error) {
console.error(error)
})
Released under the MIT license.