Hi there, I have a Gmail IMAP parser. I download messages from inbox and I want to insert them to the Mongo database. I am getting this error message: Error: Meteor code must always run within a Fiber. Try wrapping callbacks that you pass to non-Meteor libraries with Meteor.bindEnvironment. This is my code.
import { Meteor } from 'meteor/meteor';
import { insertTaskFromMessage } from '../../api/tasks/methods';
var Imap = require('imap');
var MailParser = require("mailparser").MailParser;
const gmail = Meteor.settings.private.gmail;
const imap = new Imap({
user: gmail.user,
password: gmail.password,
host: 'imap.gmail.com',
port: 993,
tls: {
secureProtocol: "TLSv1_method"
}
});
const fetchMessage = (msg, seqno) => {
const parser = new MailParser();
parser.on('end', insertMessage);
msg.on("body", (stream) => {
stream.on("data", (chunk) => {
parser.write(chunk);
});
});
msg.on('end', () => parser.end());
};
const insertMessage = (mail) => {
insertTaskFromMessage.call({
title: mail.subject,
description: mail.text,
userEmail: mail.from[ 0 ].address
}, (error) => {
if (error) {
console.log(error);
}
});
};
export default function () {
imap.once('ready', () => {
imap.openBox('INBOX', true, (err, box) => {
if (err) throw err;
const f = imap.seq.fetch('1:*', {
bodies: [ 'HEADER.FIELDS (FROM TO SUBJECT DATE)', '' ]
});
f.on('message', fetchMessage);
f.once('error', (err) => console.log('Fetch error: ' + err));
f.once('end', () => imap.end());
});
});
imap.on('error', (err) => console.log(err));
imap.once('end', () => console.log('Connection ended'));
imap.connect();
}