To receive email in Node.js, we use the mailin
module.
To install it, we run
npm install --save mailin
Then we write
const mailin = require("mailin");
mailin.start({
port: 25,
disableWebhook: true,
});
mailin.on("authorizeUser", (connection, username, password, done) => {
if (username === "johnsmith" && password === "mysecret") {
done(null, true);
} else {
done(new Error("Unauthorized!"), false);
}
});
mailin.on("startMessage", (connection) => {
console.log(connection);
});
mailin.on("message", (connection, data, content) => {
console.log(data);
});
to call mailin.start
to start watching for emails.
And then we call on
to listen for the 'authorizeUser'
, 'startMessage'
and 'message'
events.
The authorizeUser
event is emitted when a user logs in.
The message
event handler has the messages stored in data
.