简体   繁体   English

使用 GmailApp 从 Google App 脚本创建 HTML 草稿

[英]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.我想创建一个谷歌应用程序脚本,通过从谷歌电子表格收集数据来获取带有签名的 Gmail 草案。 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:使用 htmlBody:

If you just want to add an HTML body to the draft, you can use the following method:如果只想在草稿中添加一个 HTML 主体,可以使用以下方法:

Where options refers to an object that can contain a series of advanced parameters, including:其中options是指一个 object 可以包含一系列高级参数,包括:

  • htmlBody : if set, devices capable of rendering HTML will use it instead of the required body argument; htmlBody :如果设置,能够呈现 HTML 的设备将使用它而不是所需的 body 参数; you can add an optional inlineImages field in HTML body if you have inlined images for your email如果您的 email 有内联图像,您可以在 HTML 正文中添加一个可选的 inlineImages 字段
  • inlineImages : a JavaScript object containing a mapping from image key ( String ) to image data ( BlobSource ); inlineImages :一个 JavaScript object 包含从图像键( String )到图像数据( BlobSource )的映射; this assumes that the htmlBody parameter is used and contains references to these images in the format <img src="cid:imageKey" />这假定使用了htmlBody参数并包含对这些图像的引用,格式为<img src="cid:imageKey" />

That is, you define the image you want in inlineImages and add them to your HTML using the corresponding imageKey .也就是说,您在inlineImages中定义所需的图像,并使用相应的 imageKey 将它们添加到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 ).您实际上可以通过 Gmail API 管理签名,因此可以使用 Apps 脚本(使用高级 Gmail 服务)。

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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Google应用脚本创建草稿邮件 - Create draft mail using Google apps script 来自网页的 Google Apps 脚本 GmailApp - Google Apps Script GmailApp from Web Page 如何使用电子表格中的 Google App 脚本生成 Gmail 草案包含正文中的表格 - How to Generate Gmail draft using Google App Script from Spreadsheet contains Table in the Body 使用doGet()发送参数时,Google Apps脚本Web App MailApp / GmailApp.sendMail()不起作用 - Google Apps Script Web App MailApp/GmailApp.sendMail() not working when parameter is sent using doGet() 从现有草稿 [GmailApp] [Apps 脚本] 的 htmlBody 嵌入内嵌图像时会中断 - Inline images break when embedded from the htmlBody of an existing draft [GmailApp] [Apps Script] 使用 Postman 的 GmailApp 功能调用 Google Apps Script API [不使用 AuthO2] - Call Google Apps Script API with GmailApp function from Postman [without using AuthO2] Google App脚本GMailApp发送电子邮件并将其分组为“对话” - Google App Script GMailApp send email and group them in Conversation Google脚本GmailApp-真的很困惑 - Google Script GmailApp - Really Confused 使用Google脚本将电话号码字符串从Google表单格式化为电子表格,再转换为电子邮件草稿 - Format phone number string from google form to spreadsheets into an email draft using google script 使用HTML输入中的日期在Google Apps脚本中创建事件 - Using a Date from HTML Input to Create Event in Google Apps Script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM