Thunderbird自动发件插件如何设置邮件附件?
解决Thunderbird插件中邮件附件添加失败的问题
你当前代码直接将File对象传入setComposeDetails的attachments数组无法成功添加附件,因为Thunderbird的compose.setComposeDetails方法不支持这种传递方式。以下是两种可行的解决办法:
方案一:使用compose.addAttachment()单独添加附件
这是最直接的方式,先设置邮件基础信息,再调用专门的添加附件接口:
const fileContent = "this is a file!"; const blob = new Blob([fileContent], { type: "text/plain" }); // 修正文件名后缀与内容类型匹配,避免类型混淆 const fileAttachment = new File( [blob], `LocationData_${new Date().toISOString().slice(0, 10)}.txt`, { type: "text/plain" } ); const tab = await browser.compose.beginNew(); // 先设置收件人、主题、正文 await browser.compose.setComposeDetails(tab.id, { to: "me@email.com", subject: "Subject!!!", body: "Append file Data", }); // 单独添加附件 await browser.compose.addAttachment(tab.id, fileAttachment);
方案二:通过Data URL传入附件内容
将Blob转换为Data URL后,直接在setComposeDetails中配置附件:
const fileContent = "this is a file!"; const blob = new Blob([fileContent], { type: "text/plain" }); // 将Blob转为Data URL const dataUrl = await new Promise(resolve => { const reader = new FileReader(); reader.onload = () => resolve(reader.result); reader.readAsDataURL(blob); }); const tab = await browser.compose.beginNew(); await browser.compose.setComposeDetails(tab.id, { to: "me@email.com", subject: "Subject!!!", body: "Append file Data", attachments: [ { name: `LocationData_${new Date().toISOString().slice(0, 10)}.txt`, url: dataUrl, type: "text/plain" } ] });
额外注意事项
你之前的代码中文件名后缀为.xlsx但内容类型是text/plain,这会导致附件类型不匹配,可能引发后续解析问题,建议根据实际内容调整文件名后缀或内容类型。
内容的提问来源于stack exchange,提问作者JHH
相关产品推荐
相关产品推荐

