Mail

Sending Outlook Emails using Microsoft Account with Google Apps Script

Gists

This is a sample script for sending Outlook emails using Microsoft account with Google Apps Script.

Before you use this script, please install OnedriveApp which is Google Apps Script library. Ref And, please authorize your Microsoft account for using Microsoft Graph API. Ref

Sample script

function myFunction() {
  const obj = [
    {
      to: [{ name: "### name ###", email: "### email address ###" }, , ,],
      subject: "sample subject 1",
      body: "sample text body",
      cc: [{ name: "name1", email: "emailaddress1" }, , ,],
    },
    {
      to: [{ name: "### name ###", email: "### email address ###" }, , ,],
      subject: "sample subject 2",
      htmlBody: "<u><b>sample html body</b></u>",
      attachments: [blob],
      bcc: [{ name: "name1", email: "emailaddress1" }, , ,],
    },
  ];

  const prop = PropertiesService.getScriptProperties();
  const odapp = OnedriveApp.init(prop);
  const res = odapp.sendEmails(obj);
  console.log(res);
}

In this sample script, 2 emails are sent using Microsoft Graph API with your Microsoft account. By this, both Outlook Emails and Google Emails can be used by Google Apps Script.

Send E-mail with xlsx File Converted from Spreadsheet

This is a script to send e-mail with a xlsx file converted from spreadsheet as an attachment file. Access token is necessary to use this script.

function excelSender() {
  var accesstoken = "[your accesstoken]";
  var sheetID = "[sheet id]";
  var xlsxName = "[output xlsx file name]"
  var params = {
    "headers" : {Authorization: "Bearer " + accesstoken},
    "muteHttpExceptions" : true
  };
  var dUrl = "https://www.googleapis.com/drive/v3/files/" + sheetID + "/export?mimeType=application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
  var xlsxlFile = UrlFetchApp.fetch(dUrl, params).getBlob().setName(xlsxName);
  MailApp.sendEmail({
    to: "[your mail address]",
    subject: "sample mail",
    body: "sample mail with an excel file",
    attachments: [xlsxlFile]
  });
}