【问题标题】:How to reply to threadId using Gmail api in node js?如何在节点 js 中使用 Gmail api 回复 threadId?
【发布时间】:2021-07-19 13:56:50
【问题描述】:

在这里,我想使用 Gmail API 发送回复。 为此,我在检索时得到了成功的响应。 现在我有threadId 使用该 threadId 我需要发送回复而不是创建新线程

这是我对检索邮件的回应

{
  id: '178fe5f9cc632096',
  threadId: '178fe5f9cc632096',
  labelIds: [ 'IMPORTANT', 'CATEGORY_PERSONAL', 'INBOX' ],
  snippet: 'it's working --',
  payload: {
    partId: '',
    mimeType: 'multipart/alternative',
    filename: '',
    headers: [
      [Object], [Object], [Object],
      [Object], [Object], [Object],
      [Object], [Object], [Object],
      [Object], [Object], [Object],
      [Object], [Object], [Object],
      [Object], [Object], [Object],
      [Object], [Object], [Object],
      [Object]
    ],
    body: { size: 0 },
    parts: [ [Object], [Object] ]
  },
  sizeEstimate: 5218,
  historyId: '119777',
  internalDate: '1619175369000'
} 

还有我发送回复的代码

function makeBody(to, from, subject, message) {
    var str = ["Content-Type: text/plain; charset=\"UTF-8\"\n",
        "MIME-Version: 1.0\n",
        "Content-Transfer-Encoding: 7bit\n",
        "to: ", to, "\n",
        "from: ", from, "\n",
        "subject: ", subject, "\n\n",
        message
    ].join('');

    var encodedMail = Buffer.from(str).toString("base64").replace(/\+/g, '-').replace(/\//g, '_');
    return encodedMail
}
function sendMessage(auth) {
                    try {
                        var raw = makeBody('abc@gmail.com', 'xyz@gmail.com', 'test subject', 'workinggggggg...');
                        const gmail = google.gmail({ version: 'v1', auth });
                        gmail.users.messages.send({
                            auth: auth,
                            userId: 'me',
                            resource: {
                                raw: raw,
                                message_id: res.data.threadId
                            }
                        }, function (err, response) {
                            if (err) {
                                return console.log('The API returned an error: ' + err);
                            }
                            else
                                console.log(response)
                        });
                    } catch (error) {
                        console.log(error)
                    }
                }

但在使用此代码时,正在创建一个新线程。 需要帮助。

【问题讨论】:

    标签: node.js gmail-api


    【解决方案1】:

    根据documentation

    如果您尝试发送回复并希望将电子邮件串起来,请确保:

    主题标题匹配 References 和 In-Reply-To 标头遵循 RFC 2822 标准。

    因此,如果 'test subject' 不是您要回复的电子邮件的真实主题,那么证明 'test subject' 作为主题将不起作用。

    还有:

    由于你可以绘制Resource: Message,你应该将线程id传递给参数threadIdmessage_id不是一个有效的参数。

    【讨论】:

    • 如果您没有,那么您无需担心这部分。但其他两点(匹配主题和正确分配threadId)至关重要。
    • 看看this是否对你有帮助。您是否在 Gmail 用户界面中激活了线程视图?
    • 收件人是 Gmail 用户吗?他是否启用了转化视图?
    • In-Reply-To 应该进入电子邮件标题并包含您要回复的邮件 ID - 请参阅 here
    • 刷新令牌会在后者过期时自动创建一个新的访问令牌。但为此,您需要合并快速入门中提到的工作流程。除非您有不同的实现 - 请参阅 google-auth-library-nodejs。我建议你写一篇单独的文章,更详细地解释你的情况和用例。这将使用户更容易为您提供帮助。
    【解决方案2】:

    解决方案是将 Message-Id 保留为 In-Reply-To 和 References 您需要更新 In-Reply-To 和 References 值,如下所示

    function makeBody(ref, InReply, to, from, subject, message) {
        var str = ["Content-Type: text/plain; charset=\"UTF-8\"\n",
            "MIME-Version: 1.0\n",
            "Content-Transfer-Encoding: 7bit\n",
            "References:", ref, "\n" +
            "In-Reply-To: ", InReply, "\n" +
            "to: ", to, "\n",
            "from: ", from, "\n",
            "subject: ", subject, "\n\n",
            message
        ].join('');
    
        var encodedMail = Buffer.from(str).toString("base64").replace(/\+/g, '-').replace(/\//g, '_');
        return encodedMail
    }
    
    const headers = res.data.payload.headers
                            let subject
                            let to
                            let ref
                            let InReply
                            headers.forEach(element => {
                                if (element.name === 'Subject' || element.name === 'subject') {
                                    subject = element.value
                                }
                                if (element.name === 'From' || element.name === 'from') {
                                    to = element.value
                                }
                                if (element.name === 'Message-ID' || element.name === 'Message-Id') {
                                    ref = element.value
                                    InReply = element.value
                                }
                            });
      var raw = makeBody(ref, InReply, to, 'example1@gmail.com', subject, 'reply message text');
    

    【讨论】:

      猜你喜欢
      • 2018-07-17
      • 2018-01-20
      • 2011-07-10
      • 1970-01-01
      • 2019-04-15
      • 1970-01-01
      • 2021-06-20
      • 1970-01-01
      • 2015-12-11
      相关资源
      最近更新 更多