OfficeScripts to send Email with html table in email body

Here’s an example of OfficeScripts to send an email using an Outlook account with an HTML body that includes a table:

function sendEmailWithTable() {
  // Create the email parameters
  var email = {
    subject: "Email with Table",
    to: "[email protected]",
    body: {
      contentType: Office.MailboxEnums.BodyType.Html,
      content: `
        <html>
        <head>
          <style>
            table {
              border-collapse: collapse;
            }
            th, td {
              border: 1px solid black;
              padding: 8px;
            }
          </style>
        </head>
        <body>
          <h1>Table Example</h1>
          <table>
            <tr>
              <th>Column 1</th>
              <th>Column 2</th>
            </tr>
            <tr>
              <td>Row 1, Cell 1</td>
              <td>Row 1, Cell 2</td>
            </tr>
            <tr>
              <td>Row 2, Cell 1</td>
              <td>Row 2, Cell 2</td>
            </tr>
          </table>
        </body>
        </html>
      `
    }
  };

  // Send the email
  Office.context.mailbox.makeEwsRequestAsync(createEmailEwsRequest(email), function(result) {
    if (result.status === Office.AsyncResultStatus.Succeeded) {
      console.log("Email sent successfully");
    } else {
      console.log("Failed to send email");
    }
  });
}

function createEmailEwsRequest(email) {
  var soapEnvelope =
    '<?xml version="1.0" encoding="utf-8"?>' +
    '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages" xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">' +
      '<soap:Header>' +
        '<t:RequestServerVersion Version="Exchange2013" />' +
      '</soap:Header>' +
      '<soap:Body>' +
        '<m:CreateItem MessageDisposition="SendAndSaveCopy">' +
          '<m:SavedItemFolderId>' +
            '<t:DistinguishedFolderId Id="sentitems" />' +
          '</m:SavedItemFolderId>' +
          '<m:Items>' +
            '<t:Message>' +
              '<t:Subject>' + email.subject + '</t:Subject>' +
              '<t:Body BodyType="' + email.body.contentType + '">' + email.body.content + '</t:Body>' +
              '<t:ToRecipients>' +
                '<t:Mailbox>' +
                  '<t:EmailAddress>' + email.to + '</t:EmailAddress>' +
                '</t:Mailbox>' +
              '</t:ToRecipients>' +
            '</t:Message>' +
          '</m:Items>' +
        '</m:CreateItem>' +
      '</soap:Body>' +
    '</soap:Envelope>';

  return soapEnvelope;
}

The createEmailEwsRequest function generates the EWS (Exchange Web Services) request body for creating and sending the email. It creates a SOAP envelope with the necessary XML elements and incorporates the email subject, body, and recipient’s email address.

Please note that this code assumes you are running the OfficeScript within an Office application that has Outlook integration. Additionally, make sure to have the necessary permissions and configuration to send emails via Outlook.

Pamai Tech
Turning ideas into Reality

Products

Office Add-in

Enterprise Solutions

Cloud Consulting

UI UX Design

Data Transformation

Services

FAQ's

Privacy Policy

Terms & Condition

Team

Contact Us

Company

About Us

Services

Features

Our Pricing

Latest News

© 2023 Pamai Tech