能否用Google Scripts实现带正文文本与附件的会议邀请发送?
如何用Google Scripts发送带正文文本和附件的会议邀请
完全可以实现这个需求,Google Scripts结合Calendar API和Gmail API就能达成你要的效果,以下是两种可行方案:
方案1:通过Calendar API的createEvent添加正文和附件链接
createEvent方法的配置参数支持添加会议描述(对应额外正文),附件可以通过Google Drive共享链接嵌入描述中:
function createEventWithDetails() { const calendar = CalendarApp.getDefaultCalendar(); const eventTitle = "项目评审会议"; const startTime = new Date("2024-06-10T10:00:00"); const endTime = new Date("2024-06-10T11:30:00"); const attendees = ["attendee1@example.com", "attendee2@example.com"]; // 准备额外正文文本 const bodyContent = "这是会议的补充说明:\n1. 汇报Q2项目进度\n2. 讨论下一阶段资源分配\n\n相关资料:"; // 获取Drive文件的共享链接 const targetFile = DriveApp.getFileById("替换为你的文件ID"); const fileShareUrl = targetFile.getUrl(); // 拼接描述内容 const eventDescription = `${bodyContent}\n- [会议资料PDF](${fileShareUrl})`; // 创建会议并发送邀请 const event = calendar.createEvent(eventTitle, startTime, endTime, { attendees: attendees, description: eventDescription, sendInvites: true }); Logger.log("会议创建完成,ID:" + event.getId()); }
这种方式发送的邀请邮件中,正文内容会显示在会议详情区域,附件以可点击的链接形式呈现,收件人可以直接跳转下载。
方案2:结合Gmail API发送带ICS邀请、自定义正文和本地附件的邮件
如果需要完全复刻VBA工具的效果(自定义正文+ICS会议邀请+任意附件整合在一封邮件),可以用Gmail API构建多部分邮件:
function sendCustomMeetingInvite() { // 1. 创建临时Calendar事件,导出ICS内容 const calendar = CalendarApp.getDefaultCalendar(); const tempEvent = calendar.createEvent("客户沟通会", new Date("2024-06-12T14:00:00"), new Date("2024-06-12T15:00:00"), { attendees: ["client@example.com"], sendInvites: false // 关闭自动发送,后续用Gmail发送 }); const icsContent = Calendar.Events.get(calendar.getId(), tempEvent.getId(), {alt: "ics"}); // 2. 准备自定义HTML正文和附件 const customHtmlBody = ` <p>您好,这是本次客户沟通会的正式邀请:</p> <ul> <li>会议主题:客户需求同步</li> <li>时间:2024年6月12日14:00-15:00</li> <li>参会人员:您和我方项目组</li> </ul> <p>请查看附件中的需求文档,提前熟悉内容。</p> `; // 获取要附加的文件(支持任意类型) const attachmentBlob = DriveApp.getFileById("替换为你的附件ID").getBlob(); // 3. 构建多部分邮件内容 const boundary = "custom-boundary-123"; const emailParts = [ `From: ${Session.getActiveUser().getEmail()}`, "To: client@example.com", "Subject: 客户沟通会邀请", `Content-Type: multipart/mixed; boundary=${boundary}`, "", `--${boundary}`, "Content-Type: text/html; charset=UTF-8", "", customHtmlBody, "", `--${boundary}`, "Content-Type: text/calendar; charset=UTF-8; method=REQUEST", 'Content-Disposition: attachment; filename="meeting-invite.ics"', "", icsContent, "", `--${boundary}`, `Content-Type: ${attachmentBlob.getContentType()}; name="${attachmentBlob.getName()}"`, `Content-Disposition: attachment; filename="${attachmentBlob.getName()}"`, "Content-Transfer-Encoding: base64", "", Utilities.base64Encode(attachmentBlob.getBytes()), "", `--${boundary}--` ].join("\r\n"); // 4. 发送邮件 Gmail.Users.Messages.send({raw: Utilities.base64EncodeWebSafe(emailParts)}, "me"); // 删除临时创建的Calendar事件(可选) tempEvent.deleteEvent(); }
注意:使用此方案需要在Google Apps Script编辑器的「服务」菜单中启用Gmail API,发送的邮件会同时包含自定义正文、ICS会议邀请附件和你指定的任意类型附件,和VBA工具的效果完全一致。
内容的提问来源于stack exchange,提问作者PFi
相关产品推荐
相关产品推荐

