【问题标题】:Nodejs - Google API Send email doesn't group by threadNodejs - Google API 发送电子邮件不按线程分组
【发布时间】:2019-04-06 07:48:26
【问题描述】:

作为小型电子邮件 CRM 项目的一部分,我创建了一个 Nodejs 应用程序。当我通过 gmail 发送 api 发送电子邮件时,邮件没有分组。

const sendObject = {}
output.data.messages.map(each => {
  sendObject.threadId = each.threadId // ThreadID is unique.
  each.payload.headers.map(head => {
    if (head.name === 'From') {
      sendObject.replyTo = head.value
    } else if (head.name === 'Subject') {
      if (head.value.indexOf('Re:') >= 0) {
        sendObject.subject = head.value  
      } else {
        sendObject.subject = 'Re: ' + head.value
      }
    } else if (head.name === 'Message-Id') {
      sendObject.inReplyTo = head.value // The last thread messageId is inserted into In-Reply-To tag
      sendObject.reference.push(head.value) // All the messageId are appended as part of References tag
    }
  })
})


const email_lines = []
email_lines.push('References:' + sendObject.reference.join(' '))
email_lines.push('In-Reply-To:' + sendObject.inReplyTo)
email_lines.push('Subject: ' + sendObject.subject)
email_lines.push('To:' + sendObject.replyTo)

email_lines.push('')
email_lines.push(req.body.body)

const email = email_lines.join('\r\n').trim()

let base64EncodedEmail = new Buffer(email).toString('base64')
base64EncodedEmail = base64EncodedEmail.replace(/\+/g, '-').replace(/\//g, '_')

emailConfig.gmail.users.messages.send({
  userId: 'me',
  threadId: sendObject.threadId,
  resource: {
    raw: base64EncodedEmail
  }
}, async (err) => {
  if (err) {
    console.log('The Threads API returned an error: ' + err)
  }
})

当我通过浏览器登录 gmail 时,我可以看到 2 封不同的电子邮件而不是一个线程。

【问题讨论】:

    标签: node.js gmail-api


    【解决方案1】:

    您应该将 threadId 放在资源中。

      const response = await this.gmail.users.messages.send({
        auth: this.oAuth2Client,
        userId: this.gmailAddress,
        uploadType: 'multipart',
        resource: {
          threadId: email.threadId,
          raw: raw
        }
      })
    

    更多详情可以查看我的Gmail-APIs-wrapperhere

    【讨论】:

      猜你喜欢
      • 2023-01-29
      • 2017-07-08
      • 1970-01-01
      • 2021-04-18
      • 1970-01-01
      • 2019-06-14
      • 2018-10-09
      • 2017-04-14
      • 1970-01-01
      相关资源
      最近更新 更多