I am currently working on a Thunderbird extension that has the ability to upload attached files in emails. The process flow of this extension is outlined below:
- Clicking on the extension icon will display a popup with options to select from: "Read All", "Read Selected", and "Read Unread".
- Upon selecting an email with an attachment and choosing the "Read Selected" option, the listener for the "Read Selected"
onclick
event is activated. - The
onclick
listener then sends a message to the background script to handle the uploading process.
Below is the code snippet I have developed so far:
popup.js
async function readSelected() {
// This function is triggered by the listener
const msgList = await browser.mailTabs.getSelectedMessages();
if (msgList.messages) {
await browser.runtime.sendMessage({
caller: 'readSelected',
messages: msgList.messages
});
}
}
background.js
browser.runtime.onMessage.addListener((req, sender, res) => {
// 'messages' is an Array of MessageHeader objects
const { caller, accounts, all, messages } = req;
// ... Other code for handling different scenarios
console.log('Reading selected');
console.log(messages);
const ids = [];
for (const msg of messages) {
ids.push(msg.id);
}
// Maps all IDs to promises that resolve to MessagePart objects
Promise.all(ids.map(id => browser.messages.getFull(id)))
.then(messages => {
console.log(messages);
}).catch(e => console.error(e));
});
While monitoring the console in background.js
, I noticed that each MessagePart
object contains a nested array called parts
, which also consists of more MessagePart
objects. Within these objects, I can see the names of the attachments (such as a DOCX file in my case). However, the question arises - how do I access the actual attachment file? I require the binary file data in order to convert it into a Base64 string before proceeding with the upload to the server. I reviewed similar questions on Stack Overflow such as post1 and post2, but I remain uncertain about using the nsIFile
interface since it necessitates a URI, which is not provided for the parts.
If additional information is required, please feel free to ask in the comments section and I will update the query accordingly (the remaining code primarily handles calls for the other options mentioned in point (1) above). Any guidance or assistance would be greatly appreciated. Thank you.