如何使用 Gmail 的 Node.js API 设置收件人
How to set recipients with Gmail's Node.js API
尽管看起来很简单,但我似乎无法弄清楚如何使用 Google 的 Gmail API 设置草稿的收件人。 documentation 表示 users.messages Resource
object 包含 payload
object,后者包含 headers
object,并且 headers
object 包含 name-value 对。
// example from google's gmail API documentation
"payload": {
"partId": string,
"mimeType": string,
"filename": string,
"headers": [
{
"name": string,
"value": string
}
],
"body": users.messages.attachments Resource,
"parts": [
(MessagePart)
]
},
它在这些 headers 中,我假设你设置了草稿的 "To" 部分,因为文档说
List of headers on this message part. For the top-level message part, representing the entire message payload, it will contain the standard RFC 2822 email headers such as To, From, and Subject.
然而,当我发出一个看起来像这样的请求时
"payload" : {
"headers" : [
{
"name" : "To",
"value" : "me"
// "me" should direct the draft to myself
}
]
}
草稿的 To 部分仍为空。有什么解决办法或建议吗?
在你的请求中你有这个:
"headers" : [ "name" : "To", "value" : "me" ]
"headers"
应该是一个对象数组,但您的数组包含 none.
相反,它应该是这样的:
"headers": [ { "name": "To", "value": "me" } ]
就像他们的例子一样:
"payload": {
"partId": string,
"mimeType": string,
"filename": string,
"headers": [
{
"name": "To",
"value": "me"
}
],
"body": users.messages.attachments Resource,
"parts": [
(MessagePart)
]
},
看来我误解了 Gmail 上的文档 API。当您向 drafts.create 发送请求时,您确实需要提供一个 users.messages Resource
,但是,并非所有内容都是可写的。只有 threadId
、labelIds
和 raw
是可写对象。事实证明,您根本不应该使用有效负载来设置 To
、From
等。您应该将它们包含在原始文件中。
我的新代码看起来像这样
let create = (toAddress, subject, content, callback) => {
gmail.users.drafts.create(
{
'userId' : 'me',
'resource' : {
'message' : {
'raw' : base64.encodeURI(
`To:${toAddress}\r\n` + // Who were are sending to
`Subject:${subject}\r\n` + // Subject
`Date:\r\n` + // Removing timestamp
`Message-Id:\r\n` + // Removing message id
`From:\r\n` + // Removing from
`${content}` // Adding our actual message
)
}
}
},
(err, response) => {
// Do stuff with response
callback(err, response);
}
)
}
尽管看起来很简单,但我似乎无法弄清楚如何使用 Google 的 Gmail API 设置草稿的收件人。 documentation 表示 users.messages Resource
object 包含 payload
object,后者包含 headers
object,并且 headers
object 包含 name-value 对。
// example from google's gmail API documentation
"payload": {
"partId": string,
"mimeType": string,
"filename": string,
"headers": [
{
"name": string,
"value": string
}
],
"body": users.messages.attachments Resource,
"parts": [
(MessagePart)
]
},
它在这些 headers 中,我假设你设置了草稿的 "To" 部分,因为文档说
List of headers on this message part. For the top-level message part, representing the entire message payload, it will contain the standard RFC 2822 email headers such as To, From, and Subject.
然而,当我发出一个看起来像这样的请求时
"payload" : {
"headers" : [
{
"name" : "To",
"value" : "me"
// "me" should direct the draft to myself
}
]
}
草稿的 To 部分仍为空。有什么解决办法或建议吗?
在你的请求中你有这个:
"headers" : [ "name" : "To", "value" : "me" ]
"headers"
应该是一个对象数组,但您的数组包含 none.
相反,它应该是这样的:
"headers": [ { "name": "To", "value": "me" } ]
就像他们的例子一样:
"payload": {
"partId": string,
"mimeType": string,
"filename": string,
"headers": [
{
"name": "To",
"value": "me"
}
],
"body": users.messages.attachments Resource,
"parts": [
(MessagePart)
]
},
看来我误解了 Gmail 上的文档 API。当您向 drafts.create 发送请求时,您确实需要提供一个 users.messages Resource
,但是,并非所有内容都是可写的。只有 threadId
、labelIds
和 raw
是可写对象。事实证明,您根本不应该使用有效负载来设置 To
、From
等。您应该将它们包含在原始文件中。
我的新代码看起来像这样
let create = (toAddress, subject, content, callback) => {
gmail.users.drafts.create(
{
'userId' : 'me',
'resource' : {
'message' : {
'raw' : base64.encodeURI(
`To:${toAddress}\r\n` + // Who were are sending to
`Subject:${subject}\r\n` + // Subject
`Date:\r\n` + // Removing timestamp
`Message-Id:\r\n` + // Removing message id
`From:\r\n` + // Removing from
`${content}` // Adding our actual message
)
}
}
},
(err, response) => {
// Do stuff with response
callback(err, response);
}
)
}