Reputation: 3852
I'm using node imap :
i have this method to save message as draft
imap.once('ready', () => {
imap.openBox('Drafts', false, (err, box) => {
if (err) throw err;
let msg, htmlEntity, plainEntity;
msg = mimemessage.factory({
contentType: 'multipart/alternate',
body: []
});
htmlEntity = mimemessage.factory({
contentType: 'text/html;charset=utf-8',
body: 'Draft Mail'
});
plainEntity = mimemessage.factory({
body: 'Draft Mail'
});
msg.header('Message-ID', '<msgId>');
msg.header('To','[email protected]');
msg.header('Subject', 'Mail');
msg.body.push(plainEntity);
msg.body.push(htmlEntity);
imap.append(msg.toString());
})
});
I want to add attachment also in this mail and save this as a draft, any solution thanks
Upvotes: 1
Views: 86
Reputation: 1790
The attachments are just another part in the multipart message payload. The mimemessage node module allows you to add attachments in the same way as other message body parts (like the htmlEntity and plainEntity in your example). You'd additionally need to add a Content-Disposition
header with value as attachment; filename=<name of file>
. Take a look at the example in the Usage Example
section of the module readme.
Please note that the body
field in the attachment 'part' would need to be base64 encoded string. In NodeJS you can read a file as base64 string using something like this:
const { readFile } = require('fs/promise');
// ...
const imageBuf = await readFile(filename);
const attachmentEntity = mimemessage. factory({
contentType: 'image/png',
contentTransferEncoding: 'base64',
body: imageBuf.toString('base64')
});
attachmentEntity.header('Content-Disposition', 'attachment ;filename="image.png"');
Upvotes: 0