Search code examples
thunderbirdthunderbird-webextensions

How do I determine the attachment for messages with manifest v3?


I used this example of the stable documentation

let attachments = await browser.messages.listAttachments(messageId);
for (let att of attachments) {
    let file = await browser.messages.getAttachmentFile(
        messageId,
        att.partName
    );
    let content = await file.text();
}

It doesn't work.

https://webextension-api.thunderbird.net/en/stable/messages.html

My other source code in the background.js of my extension

async function mail_display(tab, message) {
  console.log(`Message displayed in tab ${message.messageId}: ${message.subject}`);
  console.log(message.id); 
  try {
 

    let attachments = await browser.messages.listAttachments(message.id);
    console.log(attachments);
    for (let att of attachments) {
      let file = await browser.messages.getAttachmentFile(messageId, att.partName);
      console.log(file);
      let content = await file.text();
    }
  } catch(error) {
    console.log(`Fehler beim Abrufen der Attachments: ${error}`);
  }
}

browser.messageDisplay.onMessageDisplayed.addListener(mail_display);

It returns an empty array with no attachment.


Solution

  • okay i found a solution that is working:

    // background.js in Thunderbird extension
    async function mail_display(tab, message) {
      // Log message: "Message displayed in tab {message.id}: {message.subject}"
      console.log(`Message displayed in tab ${message.id}: ${message.subject}`);
    
      // Try to load the full message
      try {
        // Save the full message in the message_full variable
        message_full = await browser.messages.getFull(message.id);
    
        // Log the message (optional)
        //console.log(message);
    
        // Call the listAttachments() method to get the list of attachments
        let attachments = await browser.messages.listAttachments(message.id);
    
        // Log the attachments
        console.log(attachments);
    
        // Iterate over the attachments
        for (let att of attachments) {
          // Call the getAttachmentFile() method to get the file for the attachment
          let file = await browser.messages.getAttachmentFile(message.id, att.partName);
    
          // Call the text() method to get the attachment content as text
          let content = await file.text();
    
          // Log the attachment content
          console.log(content);
        }
      } catch (error) {
        // Log the error message
        console.log(`Error with loading the Attachments: ${error}`);
      }
    }
    
    browser.messageDisplay.onMessageDisplayed.addListener(mail_display);
    
    

    Note:

    1. It doesn't seem to work if the email is openPGP encrypted (!)
    2. Restart Thunderbird