自动发送电子邮件的功能没有在指定的线程内发送。

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

Function to automatically send email is not sending within specified thread

问题

我有一个包含两列的表格(列A包含消息ID值,列B可能为空)。我的函数“sendEmail”将遍历这两列,如果列B有值,那么它应该回复给列A中的消息ID的特定收件人(下面的收件人只是示例@example.com,但我在我的代码中放入了自己的电子邮件)。然而,下面的函数发送了一封全新的电子邮件,而不是在线程内回复。可能出了什么问题?

function sendEmail () {
    const messageIDandRecordID = generator.getRange(2, 1, generator.getLastRow() - 1, 2).getValues();

    messageIDandRecordID.forEach(function(row) {
        const messageId = row[0];
        const replyText = row[1];

        if (replyText) {
            const message = GmailApp.getMessageById(messageId);
            const replySubject = "Re: " + message.getSubject();
            const replyTextHyperlink = `<a href="https://mylink.com/${replyText}">${replyText}</a>`; 
            const replyBody = `Hello team, this has been addressed. Please see record ${replyTextHyperlink}.`;

            GmailApp.sendEmail("example@example.com", replySubject, "", {
                htmlBody: replyBody
            });
        }
    });
};
英文:

I have a sheet with 2 columns (column A has message ID values and column B may or may not be blank). My function "sendEmail" will loop through these two columns and if column B has a value, then it should reply to a specific recipient in the thread with the message ID from column A (the recipient below is just example@example.com but I put my own email in my code). However, my function below is sending a brand new email and not responding within the thread. What could be going wrong?

function sendEmail () {
    const messageIDandRecordID = generator.getRange(2, 1, generator.getLastRow() - 1, 2).getValues();

    messageIDandRecordID.forEach(function(row) {
        const messageId = row[0];
        const replyText = row[1];

        if (replyText) {
            const message = GmailApp.getMessageById(messageId);
            const replySubject = &quot;Re: &quot; + message.getSubject();
            const replyTextHyperlink = `&lt;a href=&quot;https://mylink.com/${replyText}&quot;&gt;${replyText}&lt;/a&gt;`; 
            const replyBody = `Hello team, this has been addressed. Please see record ${replyTextHyperlink}.`;

            GmailApp.sendEmail(&quot;example@example.com&quot;, replySubject, &quot;&quot;, {
                htmlBody: replyBody
            });
        }
    });
};

答案1

得分: 2

以下是代码的中文翻译:

这是线程中的回复

function sendEmail() {
  const vs = getIdsAndSubjects();
  vs.forEach(function (r) {
    const messageId = r[0];
    const replyText = r[1];

    if (replyText && messageId) {
      const message = GmailApp.getMessageById(messageId);
      const replySubject = "Re: " + message.getSubject();
      const replyTextHyperlink = `<a href="https://mylink.com/${replyText}">${replyText}</a>`;
      const replyBody = `团队,这个问题已经解决。请查看记录 ${replyTextHyperlink}。`;

      message.getThread().reply(replyBody, { htmlBody: replyBody });
    }
  });
};

在选项对象中使用 replyTo

function sendEmail() {
  const vs = getIdsAndSubjects();
  vs.forEach(function (r) {
    const messageId = r[0];
    const replyText = r[1];

    if (replyText && messageId) {
      const message = GmailApp.getMessageById(messageId);
      const replySubject = "Re: " + message.getSubject();
      const replyTextHyperlink = `<a href="https://mylink.com/${replyText}">${replyText}</a>`;
      const replyBody = `团队,这个问题已经解决。请查看记录 ${replyTextHyperlink}。`;

      message.getThread().reply(replyBody, { htmlBody: replyBody, replyTo: "" });
    }
  });
};

这是我获取测试数据的方式我将其格式与 getValues() 相同

function getIdsAndSubjects() {
  const ts = GmailApp.getInboxThreads();
  let mA = [];
  ts.forEach(t => {
    t.getMessages().forEach(m => {
      mA.push([m.getId(), m.getSubject()])
    })
  });
  //Logger.log(JSON.stringify(mA))
  return mA;
}

请注意,这些是代码的中文翻译,不包括问题中的其他内容。

英文:

This replys in the thread:

function sendEmail() {
const vs = getIdsAndSubjects();
vs.forEach(function (r) {
const messageId = r[0];
const replyText = r[1];
if (replyText &amp;&amp; messageId) {
const message = GmailApp.getMessageById(messageId);
const replySubject = &quot;Re: &quot; + message.getSubject();
const replyTextHyperlink = `&lt;a href=&quot;https://mylink.com/${replyText}&quot;&gt;${replyText}&lt;/a&gt;`;
const replyBody = `Hello team, this has been addressed. Please see record ${replyTextHyperlink}.`;
message.getThread().reply(replyBody, { htmlBody: replyBody });
}
});
};

Using replyTo in the options object

function sendEmail() {
const vs = getIdsAndSubjects();
vs.forEach(function (r) {
const messageId = r[0];
const replyText = r[1];
if (replyText &amp;&amp; messageId) {
const message = GmailApp.getMessageById(messageId);
const replySubject = &quot;Re: &quot; + message.getSubject();
const replyTextHyperlink = `&lt;a href=&quot;https://mylink.com/${replyText}&quot;&gt;${replyText}&lt;/a&gt;`;
const replyBody = `Hello team, this has been addressed. Please see record ${replyTextHyperlink}.`;
message.getThread().reply(replyBody, { htmlBody: replyBody, replyTo: &quot;&quot; });
}
});
};

This is how I got the data to test and I put it in the same format as getValues();

function getIdsAndSubjects() {
const ts = GmailApp.getInboxThreads();
let mA = [];
ts.forEach(t =&gt; {
t.getMessages().forEach(m =&gt; {
mA.push([m.getId(),m.getSubject()])
})
});
//Logger.log(JSON.stringify(mA))
return mA;
}

huangapple
  • 本文由 发表于 2023年6月29日 03:24:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76576157.html
匿名

发表评论

匿名网友

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

确定