如何使用GmailApp发送富文本电子邮件? [英] How to send rich text emails with GmailApp?

查看:641
本文介绍了如何使用GmailApp发送富文本电子邮件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 函数sendGoogleDocInEmail(())函数sendGoogleDocInEmail() ){

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

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

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



我在Google Doc中使用了描述性的超链接文本片段,并且在转换为纯文本时丢失了超链接。



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

我尝试将正文对象传递给方法,但只是发送一封包含<$ c

谢谢!

解决方案

$ c> DocumentBodySection

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

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



 函数emailGoogleDoc(){
var id = DocumentApp.getActiveDocument()。getId();
var forDriveScope = DriveApp.getStorageUsed(); //需要获取Drive Scope请求
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 ='主题行';
var body =要查看此电子邮件,请在您的电子邮件客户端中启用html。;

MailApp.sendEmail(
email,//收件人
主题,//主题
主体,{//主体
htmlBody:html //高级选项
}
);
}


I’m trying to send a Google Doc, with all of its formatting, in an email.

function sendGoogleDocInEmail() {

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

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

This code works fine, but the email is sent as plain text.

I have descriptive hyperlink text pieces in the Google Doc, and they lose their hyperlinks when converted to plain text.

Is there any way I can keep all of the hypertext formatting when sending the email?

I’ve tried passing the body object to the method instead, but that just sends an email with DocumentBodySection in the body.

Thanks!

解决方案

Trying using a combination of this script: https://stackoverflow.com/a/28503601/3520117

And then using the htmlBody parameter of the MailApp.sendEmail method.

Untested, but should work:

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

这篇关于如何使用GmailApp发送富文本电子邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆