Introduction to Mail-Listener2
Mail-Listener2 is a robust Node.js library designed to fetch and manage emails using IMAP. It’s highly configurable and provides a wide range of API methods to seamlessly integrate email functionalities into your application. Whether you want to monitor an email inbox, parse incoming emails, or perform complex operations based on email content, Mail-Listener2 makes it straightforward and efficient.
Setting Up Mail-Listener2
First, let’s start with the installation:
npm install mail-listener2
Basic Usage
Below, we’ll look at some basic usage scenarios with code snippets.
Initialize Mail Listener
const MailListener = require("mail-listener2");
const mailListener = new MailListener({
username: "your-email@gmail.com",
password: "your-password",
host: "imap.gmail.com",
port: 993,
tls: true,
tlsOptions: { rejectUnauthorized: false },
mailbox: "INBOX",
searchFilter: ["UNSEEN"],
markSeen: true,
fetchUnreadOnStart: true,
mailParserOptions: { streamAttachments: true },
attachments: true,
attachmentOptions: { directory: "attachments/" }
});
Start Listening to Emails
mailListener.start();
Stop Listening to Emails
mailListener.stop();
Handle Email Events
mailListener.on("server:connected", ()=> {
console.log("Connected to IMAP server");
});
mailListener.on("server:disconnected", ()=> {
console.log("Disconnected from IMAP server");
});
mailListener.on("error", (err)=> {
console.error(err);
});
mailListener.on("mail", (mail, seqno, attributes)=> {
console.log("New email received");
console.log(mail);
});
mailListener.on("attachment", (attachment, path, seqno)=> {
console.log("Attachment stored at: " + path);
});
Advanced Features
Mail-Listener2 also provides various options and methods to handle different scenarios. For example:
Filtering Emails
const options = {
username: "your-email@gmail.com",
password: "your-password",
host: "imap.gmail.com",
port: 993,
tls: true,
tlsOptions: { rejectUnauthorized: false },
mailbox: "INBOX",
searchFilter: ["UNSEEN", "FLAGGED"],
markSeen: false,
fetchUnreadOnStart: false,
mailParserOptions: { streamAttachments: true },
attachments: true,
attachmentOptions: { directory: "attachments/" }
};
const advancedMailListener = new MailListener(options);
advancedMailListener.start();
Parsing Complex Emails
mailListener.on("mail", (mail)=> {
console.log("New email received:");
// Access various parts of the mail object
const from = mail.from[0];
const subject = mail.subject;
const htmlBody = mail.html;
console.log("From: ", from.address);
console.log("Subject: ", subject);
console.log("HTML Body: ", htmlBody);
});
Building an Email-based Application
Let’s build a simple Node.js application that uses Mail-Listener2 to monitor incoming emails and store them in a database.
Complete App Example
const express = require("express");
const mongoose = require("mongoose");
const MailListener = require("mail-listener2");
// Initialize Express app
const app = express();
// Connect to MongoDB
mongoose.connect("mongodb://localhost:27017/emails", {
useNewUrlParser: true,
useUnifiedTopology: true
});
// Define a schema and model for storing emails
const emailSchema = new mongoose.Schema({
from: String,
subject: String,
html: String,
receivedAt: Date
});
const Email = mongoose.model("Email", emailSchema);
// Initialize MailListener
const mailListener = new MailListener({
username: "your-email@gmail.com",
password: "your-password",
host: "imap.gmail.com",
port: 993,
tls: true,
tlsOptions: { rejectUnauthorized: false },
mailbox: "INBOX",
searchFilter: ["UNSEEN"],
markSeen: true,
fetchUnreadOnStart: true,
mailParserOptions: { streamAttachments: true },
attachments: true,
attachmentOptions: { directory: "attachments/" }
});
// Event for new email
mailListener.on("mail", (mail)=> {
const newEmail = new Email({
from: mail.from[0].address,
subject: mail.subject,
html: mail.html,
receivedAt: new Date()
});
newEmail.save()
.then(() => console.log("Email saved to database"))
.catch(err => console.error(err));
});
// Start listening to emails
mailListener.start();
// Start the Express server
app.listen(3000, ()=> {
console.log("Server running on port 3000");
});
With the above application, you can monitor your email inbox and store all the received emails’ details in a MongoDB database.
Conclusion
Mail-Listener2 is a powerful tool for integrating email functionalities into your Node.js applications. Whether you need to monitor incoming emails, filter them, or parse complex email contents, Mail-Listener2 provides a comprehensive set of features to handle these tasks efficiently.
Get started with Mail-Listener2 today and simplify email management in your next project.
Hash: 866ad2f03fa899b076ec25a8e77538a21c4f202f85701bb75f90b6e7a4f2541b