英文:
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 = "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
});
}
});
};
答案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 && messageId) {
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}.`;
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 && messageId) {
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}.`;
message.getThread().reply(replyBody, { htmlBody: replyBody, replyTo: "" });
}
});
};
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 => {
t.getMessages().forEach(m => {
mA.push([m.getId(),m.getSubject()])
})
});
//Logger.log(JSON.stringify(mA))
return mA;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论