使用发送功能删除HTML电子邮件中的嵌入式图像

时间:2019-12-12 05:16:52

标签: office-js office-addins outlook-web-addins

要删除HTML电子邮件中的嵌入式图像,请使用removeAttachmentAsync()方法(在Mailbox API中)。 removeAttachmentAsync()方法成功完成,但是其行为如下:

  • Web上的Outlook(OWA):
    • 发送的电子邮件没有嵌入的图像。这按我预期的那样工作。
  • Windows上的Outlook:
    • 发送的电子邮件仍然具有嵌入的图像。

在Windows版Outlook中删除嵌入式图像的正确方法是什么?

第一个代码:

const test = async (event) => {
  let timeout = null;
  const item = Office.context.mailbox.item;
  item.getAttachmentsAsync((result) => {
    for (let i = 0; i < result.value.length; i++) {
      item.removeAttachmentAsync(result.value[i].id, (result2) => {
        clearTimeout(timeout);
        timeout = setTimeout(() => {
          event.completed({ allowEvent: true });
        }, 500);
      });
    }
  });
}

更新

我使用setAsync()方法重写了代码。行为已更改:

  • Windows上的Outlook:
    • 发送的电子邮件中嵌入了空白图像。

修改后的代码:

const test = async (event) => {
  let timeout = null;
  const item = Office.context.mailbox.item;
  item.getAttachmentsAsync((result) => {
    for (let i = 0; i < result.value.length; i++) {
      item.removeAttachmentAsync(result.value[i].id, (result2) => {
        clearTimeout(timeout);
        timeout = setTimeout(() => {
          item.body.getAsync(Office.CoercionType.Html, (result3) => {
            item.body.setAsync(result3.value, { coercionType: Office.CoercionType.Html }, () => {
              event.completed({ allowEvent: true });
            });
          });
        }, 500);
      });
    }
  });
}

1 个答案:

答案 0 :(得分:0)

解决问题的代码:

const removeImagesFromBody = (event) => {
  const item = Office.context.mailbox.item;
  const type = Office.CoercionType.Html;
  item.body.getAsync(type, (result) => {
    let body = result.value;

    let match;
    const regex1 = new RegExp('v:shapes="([^"]+)"', 'gi');
    while ((match = regex1.exec(result.value)) !== null) {
      const regex2 = new RegExp(`<v:shape id="${match[1]}"[^]*?</v:shape>`, 'gi');
      body = body.replace(regex2, '');
    }

    body = body.replace(/<img [^>]*>/gi, '');
    item.body.setAsync(body, { coercionType: type }, () => {
      event.completed({ allowEvent: true });
    });
  });
}