简体   繁体   中英

Create HTML Draft from Google App script using GmailApp

I want to create a Google App Script to get Gmail Draft with a signature, by collecting data from a google spreadsheet. by default,

 function Loop_Email_Drafts() { var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getActiveSheet(); var index = 2; var lastRow = sheet.getLastRow(); for (;index <= lastRow; index++){ var emailAddress = sheet.getRange(index, 1, 1, 1).getValue(); var ccmail = sheet.getRange(index, 2, 1, 1).getValue(); var subject = sheet.getRange(index, 3, 1, 1).getValue(); var message = sheet.getRange(index, 4, 1, 1).getValue(); GmailApp.createDraft(emailAddress, subject, message, {cc: ccmail}); }// End of For Loop }// End of Function

google app script is creating a plain text draft from which I cant add my signature(my signature also contains an image).

Currently, I m using the below code to generate a draft but without a signature is worthless.

Use htmlBody:

If you just want to add an HTML body to the draft, you can use the following method:

Where options refers to an object that can contain a series of advanced parameters, including:

  • htmlBody : if set, devices capable of rendering HTML will use it instead of the required body argument; you can add an optional inlineImages field in HTML body if you have inlined images for your email
  • inlineImages : a JavaScript object containing a mapping from image key ( String ) to image data ( BlobSource ); this assumes that the htmlBody parameter is used and contains references to these images in the format <img src="cid:imageKey" />

That is, you define the image you want in inlineImages and add them to your HTML using the corresponding imageKey . Here's an example of how this could be done:

const html = "<p>Whatever</p><img src=\"cid:yourImageKey\" />";
const options = {
  htmlBody: html,
  inlineImages: {
    yourImageKey: YOUR_IMAGE_BLOBSOURCE
  }
}
GmailApp.createDraft(recipient, subject, '', options);

Update: add signature:

You can actually manage signatures via Gmail API, and therefore with Apps Script (using the Advanced Gmail Service ).

In order to add your actual signature to your draft, you can do the following:

const signature = Gmail.Users.Settings.SendAs
                      .get("me", "email_address_or_alias")
                      .signature;
const html = "<div>YOUR_HTML_BODY</div><br>" + signature;
const options = {
  htmlBody: html,
  cc: ccmail
}
GmailApp.createDraft(recipient, subject, '', options);

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