【问题标题】:How to send rich text emails with GmailApp?如何使用 GmailApp 发送富文本电子邮件?
【发布时间】:2016-09-30 15:40:27
【问题描述】:

我正在尝试通过电子邮件发送一份包含所有格式的 Google 文档。

function sendGoogleDocInEmail() {

  var doc = DocumentApp.openById("example_Id");
  var body = doc.getBody();
  var text = body.getText();

  GmailApp.sendEmail("emailaddress@gmail.com", "Hi", text);

此代码工作正常,但电子邮件以纯文本形式发送。

我在 Google Doc 中有描述性的超链接文本片段,它们在转换为纯文本时会丢失超链接。

有什么方法可以在发送电子邮件时保留所有超文本格式?

我尝试将正文对象传递给该方法,但这只是发送一封正文中包含 DocumentBodySection 的电子邮件。

谢谢!

【问题讨论】:

    标签: google-apps-script


    【解决方案1】:

    尝试使用此脚本的组合:https://stackoverflow.com/a/28503601/3520117

    然后使用MailApp.sendEmail方法的htmlBody参数。

    未经测试,但应该可以工作:

    function emailGoogleDoc(){
      var id = DocumentApp.getActiveDocument().getId() ;
      var forDriveScope = DriveApp.getStorageUsed(); //needed to get Drive Scope requested
      var url = "https://docs.google.com/feeds/download/documents/export/Export?id="+id+"&exportFormat=html";
      var param = {
        method      : "get",
        headers     : {"Authorization": "Bearer " + ScriptApp.getOAuthToken()},
        muteHttpExceptions:true,
      };
      var html = UrlFetchApp.fetch(url,param).getContentText();
      Logger.log(html);
    
      var email = person@domain.tld;  
      var subject = 'Subject line';
      var body = "To view this email, please enable html in your email client.";
    
      MailApp.sendEmail(
        email,           // recipient
        subject,         // subject 
        body, {          // body
          htmlBody: html // advanced options
        }
      );
    }
    

    【讨论】:

      猜你喜欢
      • 2018-03-24
      • 1970-01-01
      • 2010-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-14
      相关资源
      最近更新 更多