繁体   English   中英

尝试自动发送通过应用程序脚本创建的 Google 文档的电子邮件

[英]Trying to send email automatically of a Google Docs created through apps script

使用脚本,我在复制的文档中替换了 body txt。 我想获取该文档 ID 并在创建文档后自动通过电子邮件发送该 ID。 不确定是否可能,但我当前的代码如下。 请帮忙。

var templateFile = DriveApp.getFileById("1baIoahNT9YJ84mnUcp1pyWR0U5v235z29vPCkiv4rIc");
var templateResponseFolder = DriveApp.getFolderById("1mmFKjNnbTy2k8ZWobtcvdtIcwyse3NVd");
var copy = templateFile.makeCopy(deptName + " " + "Cost Recovery Agreement", templateResponseFolder);
var doc = DocumentApp.openById(copy.getId());
var body = doc.getBody();
body.replaceText("{{DeptName}}", deptName);
body.replaceText("{{DeptCo}}", deptCo);
body.replaceText("{{FirstName}}", firstName);
body.replaceText("{{LastName}}", lastName);
doc.saveAndClose();
var id = doc.getId();
var subject = "Welcome to ResponseMaster!";
var message = title + " " + lastName + "," + "\n\n" + "Thank you for choosing ResponseMaster as your dedicated software for your fire department.";
var costRecoveryForm = DriveApp.getFileById();
MailApp.sendEmail(userEmail, subject, message, { attachments: [costRecoveryForm] });

我已经在脚本上方正确设置了 e.values,但这就是处理该问题的所有脚本。

通过将 Google Doc URL 放在电子邮件正文中,Gmail 会将其显示为附件(如果收件人有权访问该文件)。

如下图所示。 通过将 URL 添加到邮件正文来附加 Google Drive 文件

所以这是我的方法:

let templateFile = DriveApp.getFileById("1hNJ8SUybJn8nnJg87Pm22dzI0_3L0N17ZbYhV1HeUrM");
let templateResponseFolder = DriveApp.getFolderById("1Xzs1k-e8q2NYCTHg-4yit7zFWTF7L33h");
let copy = templateFile.makeCopy(`${deptName} Cost Recovery Agreement`, templateResponseFolder);
let copyId = copy.getId();

// Here I give the user permission to view the file I'm sending him
DriveApp.getFileById(copyId).addViewer(userEmail); 

let doc = DocumentApp.openById(copyId);
let copyUrl = doc.getUrl();
let body = doc.getBody();
body.replaceText("{{DeptName}}", deptName);
body.replaceText("{{DeptCo}}", deptCo);
body.replaceText("{{FirstName}}", firstName);
body.replaceText("{{LastName}}", lastName);
doc.saveAndClose();

let subject = "Welcome to ResponseMaster!";
let message = `${title} ${lastName},\n\nThank you for choosing ResponseMaster as your dedicated software for your fire department.\n\n${copyUrl}`;
MailApp.sendEmail(userEmail, subject, message);

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM