I am developing a Thunderbird extension that can upload attached files in mail. The flow of the extension is as follows:
- Clicking the extension icon shows a popup for selecting one of "Read All", "Read Selected" and "Read Unread" options
- When I select an email that contains an attachment and choose "Read Selected" option, the listener for the "Read Selected"
onclick
event is fired. - The
onclick
listener sends a message to the background script to handle the upload
Here's the code I have so far:
popup.js
async function readSelected() {
// this function is invoked 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;
// ... code for handling other cases
console.log('Reading selected');
console.log(messages);
const ids = [];
for(const msg of messages) {
ids.push(msg.id);
}
// maps all ids to promises that resolves to MessagePart objects
Promise.all(ids.map(id => browser.messages.getFull(id)))
.then(messages => {
console.log(messages);
}).catch(e => console.error(e));
});
In the console for background.js
, I see that each MessagePart
object has a parts
array which in turn is an array of MessagePart
objects. I can see the name of the attachment (in my case, the selected message has a DOCX file as attachment). Question is, how do I get the file? I need the binary file data because I need to convert it to Base64 string before I can upload to the remote server. I looked at the following SO posts: post1 and post2 but I am not sure how that works since, both posts suggest using nsIFile
interface which, requires a URI. There are no such URI for the parts provided.
If more information is needed please ask me in comment and I will update the question (the rest of the code is mostly handling calls for the other options as discussed in (1) above). Some guidelines and help will be highly appreciated. Thanks.