使用GmailApp在Google Apps Script中回复Gmail thread特定邮件的问题
Google Apps Script 处理Gmail线程回复解决方案
1. 回复线程中自身首条邮件的收件人
通过线程ID定位目标线程,筛选出自己发送的首封邮件,提取其收件人(To/Cc/Bcc),结合Google Sheets内容发送跟进提醒并关联原线程:
function sendFollowUpToThreadRecipients() { const threadId = "你的线程ID"; // 替换为实际线程ID const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1"); // 替换为目标工作表名 const replyContent = sheet.getRange("A1").getValue(); // 替换为存储回复内容的单元格 // 获取目标线程与邮件列表 const thread = GmailApp.getThreadById(threadId); const messages = thread.getMessages(); // 筛选自身发送的首条邮件 const myFirstMessage = messages.find(msg => msg.getFrom().includes(Session.getActiveUser().getEmail())); if (!myFirstMessage) { console.log("未找到自身发送的邮件"); return; } // 提取原始收件人信息 const toRecipients = myFirstMessage.getTo(); const ccRecipients = myFirstMessage.getCc(); const bccRecipients = myFirstMessage.getBcc(); // 发送跟进邮件并关联原线程 GmailApp.sendEmail( toRecipients, "跟进提醒:[原邮件主题]", // 替换为合适的邮件主题 replyContent, { cc: ccRecipients, bcc: bccRecipients, threadId: threadId, htmlBody: formatReplyToHtml(replyContent) // 关联问题2的格式处理函数 } ); }
2. 保留多段落与特定字号格式
必须使用htmlBody才能保留格式,纯文本会自动合并换行,两种处理方式可选:
简单手动转HTML
适用于仅需保留段落和统一字号的场景,将文本换行转为<br>,段落用<p>包裹:
function formatReplyToHtml(rawContent) { return rawContent.split("\n").map(line => { return `<p><span style="font-size:14px">${line}</span></p>`; }).join(""); }
提取Sheets富文本格式
若Sheets内容包含不同字号、颜色等富文本设置,可通过getRichTextValue()精准提取格式并生成HTML:
function getRichTextAsHtml() { const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1"); const richText = sheet.getRange("A1").getRichTextValue(); const text = richText.getText(); const runs = richText.getRuns(); let html = ""; runs.forEach(run => { const segment = text.substring(run.getStartIndex(), run.getEndIndex()); const style = run.getTextStyle(); let styleStr = ""; if (style.getFontSize()) styleStr += `font-size:${style.getFontSize()}px;`; if (style.getForegroundColor()) styleStr += `color:${style.getForegroundColor()};`; html += `<span style="${styleStr}">${segment}</span>`; }); return html.replace(/\n/g, "<br>"); }
3. 生成Gmail风格的原始邮件引用
提取目标邮件的发件人、日期和内容,格式化为<blockquote>结构添加到回复末尾:
function generateEmailQuote(message) { // 格式化日期为Gmail风格 const formattedDate = Utilities.formatDate( message.getDate(), Session.getScriptTimeZone(), "EEE, MMM d, yyyy 'at' h:mm a" ); const sender = message.getFrom().replace(/<.*>/, "").trim(); const senderEmail = message.getFrom().match(/<(.*)>/)[1]; // 提取原始邮件纯文本内容并处理换行 const originalContent = message.getBody().replace(/<br>/g, "\n").replace(/<.*?>/g, ""); // 生成引用HTML return ` <blockquote style="border-left: 2px solid #ccc; margin: 10px 0; padding-left: 10px; color: #555;"> <p>于 ${formattedDate},${sender} <${senderEmail}> 写道:</p> <p>${originalContent.split("\n").join("</p><p>")}</p> </blockquote> `; } // 拼接内容与引用发送邮件 function sendReplyWithQuote() { const threadId = "你的线程ID"; const thread = GmailApp.getThreadById(threadId); const myFirstMessage = thread.getMessages().find(msg => msg.getFrom().includes(Session.getActiveUser().getEmail())); const replyContent = getRichTextAsHtml(); const quote = generateEmailQuote(myFirstMessage); GmailApp.sendEmail( myFirstMessage.getTo(), "跟进提醒", "", { cc: myFirstMessage.getCc(), threadId: threadId, htmlBody: `${replyContent}<br><br>${quote}` } ); }
内容的提问来源于stack exchange,提问作者Hamed
相关产品推荐
相关产品推荐

