I have started a new email project. It's very challenging to me because first time I am working with own custom email project. I am using node.js for creating REST full API. Using imap-simple module for accessing Internet message access. Everything is working like send email, receive email, add flag etc from our own server. But problem is when trying to move an email from Inbox to Drafts folder it's giving an error. Can you please see my code snippet below and guide me for the right way.
const imaps = require('imap-simple');
module.exports.moveEmail = async (req, res)=>{
let user = req.body;
let UID = req.params.uid
try{
const connection = await imaps.connect({
imap: {
user: user.user, // User email
password: user.password, // User email password
host: 'server.XXXX.com',
port: XXXX,
authTimeout: 10000,
tls: true,
tlsOptions: { rejectUnauthorized: false },
},
});
await connection.openBox('INBOX')
const searchCriteria = [['UID', UID], ['TO', user.user]]
const fetchOptions = {
bodies: ['HEADER', ''], // Empty string means full body
markSeen: false
}
const messages = await connection.search(searchCriteria, fetchOptions)
if (messages.length === 0) {
return res.send({message:`ID ${UID} Email not found`})
}
// console.log(messages)
connection.moveMessage(UID, 'Drafts', (err) => {
if (err){
console.log(err)
console.log({status:500, message:'Problem marking email move to Drafts'});
rej(err);
}else{
connection.end();
return res.send({status:200, message:`Email flag removed successfully!`})
}
})
}catch (error){
console.log(error)
}
}
Hope fully some one have a nice solution.