IMAP client
OpenSubmitter provides a wrapper for imap-simple + Mailparser modules, making the task to download & parse emails a bit easier.
This template provides a fully working example of downloading emails from IMAP server and displaying it in OpenSubmitter's results table. Here are some key points of this template:
1. Provide IMAP configuration
const imapConfig = {
imap: {
user: "user@mailserver.com",
password: "the_password",
host: "imap.gmail.com",
port: 993, // Usually it is this port
// Other options from imap-simple
tls: true,
tlsOptions: { rejectUnauthorized: false },
authTimeout: 10000
}
}
2. Use getIMAPMessages method for messages retrieval.
const messages = await this.getIMAPMessages(imapConfig);
for (const message of messages) {
let messageUID = message.UID;
let fromMail = message.from;
let subject = message.subject;
let body = message.body
}
3. Use deleteIMAPMessage method to remove messages.
await this.deleteIMAPMessage(message.UID);
4. Use imap-simple directly if needed.
const imapsimple = require('imap-simple');
console.log('opening an IMAP connection');
let IMAPConnection = null;
try {
IMAPConnection = await imapsimple.connect(imapConfig);
} catch (e) {
console.log('could not open connection '+(e as String).toString());
return null;
}
console.log('opening mail INBOX')
await IMAPConnection.openBox('INBOX');
console.log('getting list of messages from INBOX')
const messages = await this.IMAPConnection.search(['ALL'], { bodies: [''], struct: true });