order: 3
id: outlook-item-body-add-inline-base64-image
name: Add inline Base64-encoded image to message or appointment body (Compose)
description: Add an inline Base64-encoded image to the body of a message or appointment being composed.
host: OUTLOOK
api_set:
    Mailbox: '1.8'
script:
    content: |-
        $("#prepend-image").on("click", prependImage);
        $("#append-image").on("click", appendImage);

        const base64String =
          "iVBORw0KGgoAAAANSUhEUgAAAGAAAABgCAMAAADVRocKAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAnUExURQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAN0S+bUAAAAMdFJOUwAQIDBAUI+fr7/P7yEupu8AAAAJcEhZcwAADsMAAA7DAcdvqGQAAAF8SURBVGhD7dfLdoMwDEVR6Cspzf9/b20QYOthS5Zn0Z2kVdY6O2WULrFYLBaLxd5ur4mDZD14b8ogWS/dtxV+dmx9ysA2QUj9TQRWv5D7HyKwuIW9n0vc8tkpHP0W4BOg3wQ8wtlvA+PC1e8Ao8Ld7wFjQtHvAiNC2e8DdqHqKwCrUPc1gE1AfRVgEXBfB+gF0lcCWoH2tYBOYPpqQCNwfT3QF9i+AegJfN8CtAWhbwJagtS3AbIg9o2AJMh9M5C+SVGBvx6zAfmT0r+Bv8JMwP4kyFPir+cswF5KL3WLv14zAFBCLf56Tw9cparFX4upgaJUtPhrOS1QlY5W+vWTXrGgBFB/b72ev3/0igUdQPppP/nfowfKUUEFcP207y/yxKmgAYQ+PywoAFOfCH3A2MdCFzD3kdADBvq10AGG+pXQBgb7pdAEhvuF0AIc/VtoAK7+JciAs38KIuDugyAC/v4hiMCE/i7IwLRBsh68N2WQjMVisVgs9i5bln8LGScNcCrONQAAAABJRU5ErkJggg==";

        function prependImage() {
          // Insert the Base64-encoded image to the beginning of the body.
          Office.context.mailbox.item.addFileAttachmentFromBase64Async(base64String, "sample.png", { isInline: true }, (attachmentResult) => {
            if (attachmentResult.status === Office.AsyncResultStatus.Failed) {
              console.log(`Failed to attach file: ${attachmentResult.error.message}`);
              return;
            }

            Office.context.mailbox.item.body.prependAsync('<img src="cid:sample.png" />', { coercionType: Office.CoercionType.Html }, (prependResult) => {
              if (prependResult.status === Office.AsyncResultStatus.Failed) {
                console.log(`Failed to prepend image to body: ${attachmentResult.error.message}`);
                return;
              }

              console.log("Inline Base64-encoded image added to the beginning of the body.");
            })
          });
        }

        function appendImage() {
          // Get the current body of the message or appointment.
          Office.context.mailbox.item.body.getAsync(Office.CoercionType.Html, (bodyResult) => {
            if (bodyResult.status === Office.AsyncResultStatus.Failed) {
              console.log(`Failed to get body: ${bodyResult.error.message}`);
              return;
            }

            // Add the Base64-encoded image to the end of the body.
            const options = { isInline: true, asyncContext: bodyResult.value };
            Office.context.mailbox.item.addFileAttachmentFromBase64Async(base64String, "sample.png", options, (attachResult) => {
              if (attachResult.status === Office.AsyncResultStatus.Failed) {
                console.log(`Failed to attach file: ${attachResult.error.message}`);
                return;
              }

              let body = attachResult.asyncContext;
              body += '<img src="cid:sample.png" />';

              Office.context.mailbox.item.body.setAsync(body, { coercionType: Office.CoercionType.Html }, (setResult) => {
                if (setResult.status === Office.AsyncResultStatus.Failed) {
                  console.log(`Failed to set body: ${setResult.error.message}`);
                  return;
                }
                
                console.log("Inline Base64-encoded image added to the end of the body.");
              });
            });
          });
        }
    language: typescript
template:
    content: |-
        <section class="ms-Fabric ms-font-m">
          <p class="ms-font-m">This sample adds an inline Base64-encoded image to the body of the message or appointment being composed.</p>
          <p><b>Required mode</b>: Compose</p>
        </section>

        <section class="ms-Fabric samples ms-font-m">
          <h3>Try it out</h3>
          <button id="prepend-image" class="ms-Button">
            <span class="ms-Button-label">Prepend image to body</span>
          </button>
          <button id="append-image" class="ms-Button">
            <span class="ms-Button-label">Append image to body</span>
          </button>
        </section>
    language: html
style:
    content: |-
        section.samples {
            margin-top: 20px;
        }

        section.samples .ms-Button, section.setup .ms-Button {
            display: block;
            margin-bottom: 5px;
            margin-left: 20px;
            min-width: 80px;
        }
    language: css
libraries: |
    https://appsforoffice.microsoft.com/lib/1/hosted/office.js
    @types/office-js

    office-ui-fabric-core@11.1.0/dist/css/fabric.min.css
    office-ui-fabric-js@1.5.0/dist/css/fabric.components.min.css

    core-js@2.4.1/client/core.min.js
    @types/core-js

    jquery@3.1.1
    @types/jquery@3.3.1