简体   繁体   中英

Email body modification (Outlook plugin)

I'm making an add-in that reads email bodies for an acronyms, it then modifies the html body so that when you hover over the found acronym, it gives you its definition. I've had no issue with the logic and even found a way to pull the body's html source code using Office.context.mailbox.item.body.getAsync("html", function callback(result) {..} . I then tested it on a new window to make sure it was being modified correctly.

My next step is to place the html code back into the original window so that for all emails I get, all acronyms in my database, when hovered over, will have a definition. I tried using the .setAsync() method provided on this page here to place the the html code back, but I can't seem to get it to work or I don't properly understand it.

Here is the basics of my code

'use strict';
funciton(){
  function readJSONFile(file, callback) {
    //read my database of acronyms
  }

  function replace(body, acronyms) {
    //replace acronyms in body with elements that have definitions
  }

  Office.onReady(function () {
    // window is ready
    $(document).ready(function () {
        var emailBody;
        Office.context.mailbox.item.body.getAsync("html", function callback(result) {
            //Contains the email body
            emailBody = result.value;
            readJSONFile("data.json", function (acronyms) {
                //Replaces acronyms in email body based on the JSON file
                emailBody = replace(emailBody, JSON.parse(acronyms));
                //I tried this, didn't seem to do anything
                Office.context.mailbox.item.body.setAsync(emailBody, { coercionType: "html" }, function callback(result));
                //I also tried this, just in case I read the syntax wrong
                Office.context.mailbox.item.body.setAsync(emailBody, "html", function callback(result));
                });
            });
        });
    });
}()

Note: emailBody contains the html structure so when I push it to another window that I created to test the functionality of the code, if reads it as html. I'm trying to do the same on the original email body window.

Here is a sample code which you can use as a base for making modifications to the message body:

function appendText() {
    Office.context.mailbox.item.body.getAsync(Office.CoercionType.Html,function (result) {
        // make any modifications to the message body
        var newHtml = result.value.replace("</body>", "<br/ >Best regards!</body>")
        // set the messasge body back
        Office.context.mailbox.item.body.setAsync(newHtml, { coercionType: Office.CoercionType.Html });
    });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM