A lightweight, promise-based IMAP email client for Node.js
npm install easy-imapA lightweight, promise-based IMAP email client for Node.js that simplifies email retrieval and mailbox management.
- TLS/SSL secure connections
- Comprehensive IMAP operations
- Promise-based async/await syntax
- Detailed response parsing
- Optional debug logging
- Mailbox listing and selection
- Email metadata and body retrieval
- Node.js 14+
- An email account with IMAP access
``bash`
npm install easy-imap
`javascript
import IMAPClient from 'easy-imap';
async function fetchEmails() {
const client = new IMAPClient({
host: 'imap.example.com',
port: 993,
secure: true
});
try {
await client.connect();
await client.login('email@example.com', 'password');
// List mailboxes
const mailboxes = await client.listMailboxes();
// Select INBOX
await client.selectMailbox('INBOX');
// Get email count
const count = await client.fetchEmailCount();
// Fetch latest email
const latestEmail = await client.fetchEmail(count);
const latestEmailBody = await client.fetchEmailBody(count);
} finally {
await client.close();
}
}
`
| Option | Type | Default | Description |
|----------|---------|---------|----------------------------------|
| host | string | Required| IMAP server hostname |
| port | number | Required| IMAP server port |
| secure | boolean | true | Use TLS/SSL connection |
| debug | boolean | false | Enable detailed logging |
- connect(): Establish server connectionlogin(username, password)
- : AuthenticatelistMailboxes()
- : Get available mailboxesselectMailbox(name)
- : Choose a mailboxfetchEmailCount()
- : Count total emailsfetchEmail(id)
- : Get email metadatafetchEmailBody(id)
- : Retrieve email contentclose()
- : End server connection
All methods return Promises and should be used with try/catch:
`javascript`
try {
// IMAP operations
} catch (error) {
console.error('IMAP Error:', error.message);
}
- Always use environment variables for credentials
- Enable debug only during development
- Use secure connections (TLS)
- mailparser: Email parsingnet
- Node.js built-in and tls`