如何使用office-js在Outlook中插入嵌入的图像

时间:2016-03-15 09:19:18

标签: javascript office-js

可以在Outlook电子邮件中嵌入图像吗?使用此代码,outlook将插入一个损坏的图像mailBoxItem.body.setSelectedDataAsync

var linkToImage = '<img src=\"data:image/'+getTemplateExtension(template.templateType).replace('.','')+";base64," + sasLink + '\"/>';

                         //Add an image as a link
                        if (mailBoxItem.body.setSelectedDataAsync) {
                            mailBoxItem.body.setSelectedDataAsync(linkToImage, {
                                        asyncContext: null,
                                        coercionType: Office.CoercionType.Html
                                    },
                                    function(asyncResult) {
                                        if (asyncResult.status == "failed") {
                                            showMessage("Action failed with error: " + asyncResult.error.message);
                                        } else {
                                            showMessage("You successfully wrote in the email body. Click Next to learn more.");
                                        }
                                    }
                                )
                        }

1 个答案:

答案 0 :(得分:1)

看起来当前的Outlook不支持在<img>元素中插入具有base64编码图像的src标记,而是要求您拥有图像的完整网址。

所以我写了一个服务器端脚本,我正在POST该base64图像字符串。该脚本将图像保存在服务器上,然后返回URL.Now最后,您可以创建一个<img>标记,返回的URL为src,并且可以成功嵌入到邮件正文中。

使用以下代码使其正常工作

 var imageBase64Data = 'data:image/png;base64,iVBORw...';//truncated the actual base64 data as its too  long
 $.ajax({
     type: 'post',
     url: 'https://metalop.com/Word-Cloud-Generator/image-url-generator.php',
     data: {
         image: imageBase64Data
     },
     error: function(e) {
         console.error(e);
     },
     success: function(response) {
         console.log(response);
         var imageHTML = "<img " +
             "src='" + response + "' img/>";
         console.log(imageHTML);

         //Add an image as a link
         Office.cast.item.toItemCompose(Office.context.mailbox.item).body.setSelectedDataAsync(imageHTML, {
                 coercionType: Office.CoercionType.Html,
             },
             function(asyncResult) {
                 if (asyncResult.status === Office.AsyncResultStatus.Failed) {
                     app.showNotification("Action failed with error: " + asyncResult.error.message);
                 }
             });
     }
 });