使用Google Apps Script中的GmailApp回复Gmail线程中特定消息并引用原始消息。

huangapple go评论80阅读模式
英文:

Sending a reply to a specific message in a Gmail thread using GmailApp in Google Apps Script and quoting the original message

问题

我正在进行一个Google Apps Script项目,需要使用GmailApp服务在Gmail邮件线程中回复特定的消息。然而,我遇到了一些挑战,需要寻求帮助。

首先,我想将回复发送给邮件线程中第一封消息的收件人,这封消息最初是由我发送的。我尝试使用GmailApp服务,但默认情况下它只将回复发送给发件人(我)。我想知道是否有一种方法编写代码片段,以将回复发送给收件人,从而使我能够发送跟进提醒。我有邮件线程的ID,并将从Google Sheets的另一个单元格中提取回复电子邮件正文。

其次,我需要保留回复电子邮件正文的格式,它存储在另一个工作表的单元格中。正文包含多个段落,我希望避免合并电子邮件正文的行并保留字体大小。我不确定使用GmailApp应该采取哪种最佳方法。我应该考虑使用HTML正文还是探索替代方法?

此外,我想在回复电子邮件中引用原始消息,类似于Gmail UI在回复中引用原始消息的方式。具体来说,我希望在回复的电子邮件正文下面包括原始消息,显示发件人、日期和原始消息内容。例如,类似以下的形式:

"On Fri, Jun 23, 2023 at 4:32 PM Hamed XXXXXX@gmail.com wrote:

Dear--------,

I hope this email finds you well."

我会感激任何指导,以解决这些挑战,并在我的Google Apps Script项目中实现所需的功能。

提前感谢您的帮助!

英文:

I am working on a Google Apps Script project where I need to send a reply to a specific message within a Gmail thread using the GmailApp service. However, I have encountered a couple of challenges that I'm seeking assistance with.

Firstly, I want to send the reply to the recipient of the first message in the thread, which was originally sent by me. I have tried using the GmailApp service, but it only sends the reply to the sender (me) by default. I am wondering if there is a way to write a snippet code to send the reply to the recipient instead, allowing me to send a follow-up reminder.
I have the thread ID and will recall the reply email body from another sheet cell in Google Sheets.
Secondly, I need to preserve the formatting of the reply email body, which is stored in a cell of another sheet. The body contains multiple paragraphs, and I want to avoid merging the lines of the email body and preserve the font size. I am unsure of the best approach to achieve this using GmailApp. Should I consider using HTML body or explore alternative methods?

Furthermore, I would like to quote the original message within the reply email, similar to how Gmail UI quotes the original message in a reply. Specifically, I want to include the original message below the email body of the reply, showing the sender, date, and the original message content. For example, something like:

> "On Fri, Jun 23, 2023 at 4:32 PM Hamed XXXXXX@gmail.com wrote:
>
> Dear--------,
>
> I hope this email finds you well."

I would appreciate any guidance to address these challenges and achieve the desired functionality within my Google Apps Script project.

Thank you in advance for your assistance!

答案1

得分: 1

> 注意:这可能无法完全解决您的问题,因为您没有包括您的数据,但它将作为一个起点和指导。

以下脚本的工作原理:

  1. 使用 GmailApp 服务检索线程中第一条消息的收件人。
  2. 检索您发送的线程中的第一条消息正文。
  3. 使用 SpreadSheetApp 服务从我创建的一个包含 示例段落样本表 中提取回复电子邮件正文。
  4. 引用第一条消息正文,类似于 Gmail UI 在回复中引用原始消息的方式。
  5. 将电子邮件发送给收件人。

脚本:

function sendReplyToThread() {
  
  var firstThread = GmailApp.getInboxThreads(0, 11)[0]; //获取收件箱中的第一个线程
  var threadById = GmailApp.getThreadById(firstThread.getId()); //获取线程的ID
  var messages = threadById.getMessages();
  var firstMessage = messages[0]
  var firstEmail = firstMessage.getBody(); //您发送的线程的第一条消息
  var recipient = firstMessage.getTo() // 第一个收件人的电子邮件地址

  // 从表格获取回复电子邮件正文
  var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  var data = ss.getRange(1, 1).getValue();
  var data = data.replace(/\n/g, "<br>");   // 将换行符替换为HTML换行标签
  var emailBody = "<html><body>" + data + "</body></html>"

  // 获取并引用原始消息
  var originalMessage = '<div style="border-left: 1px solid #ccc; padding-left: 10px; margin-left: 10px;">';
  originalMessage += '<p style="color: rgb(80, 0, 80);">On ' + formatDate(firstMessage.getDate()) + ' <a href="mailto:' + firstMessage.getFrom() + '">' + firstMessage.getFrom() + '</a> wrote:</p>';
  originalMessage += '<blockquote style="margin: 0 0 0 10px; border-left: 1px solid rgb(80, 0, 80); padding-left: 10px; color: rgb(80, 0, 80);">' + firstEmail + '</blockquote>';
  originalMessage += '</div>';

  // 发送回复电子邮件
  var replyDraft = firstThread.reply("", {
    htmlBody: emailBody + "<br><br>" + originalMessage
    , replyTo: recipient});
}

//日期格式化功能
function formatDate(date) {
  var formattedDate = Utilities.formatDate(date, Session.getScriptTimeZone(), "EEE, MMM d, yyyy 'at' h:mm a");
  return formattedDate;
}

示例线程:

使用Google Apps Script中的GmailApp回复Gmail线程中特定消息并引用原始消息。

使用脚本的回复电子邮件:

使用Google Apps Script中的GmailApp回复Gmail线程中特定消息并引用原始消息。

参考:

Class GmailApp

GetTo method

CreateDraft method

reply method

英文:

Suggestion:

> Note: This may not totally resolve your problem because you haven't included your data, but it will serve as a starting point and guidance in doing so.

The script below works as follows

  1. Retrieves the recipient of the first message on the thread using the GmailApp service
  2. Retrieve the first message body on the thread sent by you
  3. Extract the reply email body from a sample sheet I made with a sample paragraph using SpreadSheetApp Service
  4. Quoting the first message body similar to how Gmail UI quotes the original message in a reply
  5. Send the email to the recipient

Script:

function sendReplyToThread() {
var firstThread = GmailApp.getInboxThreads(0, 11)[0]; //Get the first thread on your inbox
var threadById = GmailApp.getThreadById(firstThread.getId()); //Get the ID of the thread
var messages = threadById.getMessages();
var firstMessage = messages[0]
var firstEmail = firstMessage.getBody(); //The first message of the thread sent by you
var recipient = firstMessage.getTo() // Email of the first recipient
// Getting the reply email body from sheet
var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var data = ss.getRange(1, 1).getValue();
var data = data.replace(/\n/g, &quot;&lt;br&gt;&quot;);   // Replace line breaks with HTML line break tags
var emailBody = &quot;&lt;html&gt;&lt;body&gt;&quot; + data + &quot;&lt;/body&gt;&lt;/html&gt;&quot;
// Obtaining and quoting the original message
var originalMessage = &#39;&lt;div style=&quot;border-left: 1px solid #ccc; padding-left: 10px; margin-left: 10px;&quot;&gt;&#39;;
originalMessage += &#39;&lt;p style=&quot;color: rgb(80, 0, 80);&quot;&gt;On &#39; + formatDate(firstMessage.getDate()) + &#39; &lt;a href=&quot;mailto:&#39; + firstMessage.getFrom() + &#39;&quot;&gt;&#39; + firstMessage.getFrom() + &#39;&lt;/a&gt; wrote:&lt;/p&gt;&#39;;
originalMessage += &#39;&lt;blockquote style=&quot;margin: 0 0 0 10px; border-left: 1px solid rgb(80, 0, 80); padding-left: 10px; color: rgb(80, 0, 80);&quot;&gt;&#39; + firstEmail + &#39;&lt;/blockquote&gt;&#39;;
originalMessage += &#39;&lt;/div&gt;&#39;;
//Sending the reply email
var replyDraft = firstThread.reply(&quot;&quot;, {
htmlBody: emailBody + &quot;&lt;br&gt;&lt;br&gt;&quot; + originalMessage
, replyTo: recipient});
}
//Function for date format
function formatDate(date) {
var formattedDate = Utilities.formatDate(date, Session.getScriptTimeZone(), &quot;EEE, MMM d, yyyy &#39;at&#39; h:mm a&quot;);
return formattedDate;
}

Sample Thread:

使用Google Apps Script中的GmailApp回复Gmail线程中特定消息并引用原始消息。

Reply Email using the script:

使用Google Apps Script中的GmailApp回复Gmail线程中特定消息并引用原始消息。

References:

Class GmailApp

GetTo method

CreateDraft method

reply method

huangapple
  • 本文由 发表于 2023年7月10日 15:17:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76651480.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定