Gmail API在获取邮件时显示空的正文内容。

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

Gmail API shows empty body when getting message

问题

当我发送请求以获取电子邮件正文时,Gmail API返回的负载对象上除了正文数据之外的所有内容。

到目前为止,我尝试过以下方法:

  • “Watch”方法已经实现并且正常工作。
  • 如您在屏幕截图中所见,响应显示了“snipped”,这意味着消息获取是有效的,但正文数据和“raw”字段仍为空。
  • 我正确使用了历史ID(将当前ID保存以供后续请求使用)。
  • 将所有依赖项升级到最新的稳定版本。

我有什么遗漏吗?

  1. func GetEmail(srv *gmail.Service, historyId uint64) (string, string) {
  2. hist := getHistory(srv, historyId)
  3. for _, h := range hist.History {
  4. for _, m := range h.MessagesAdded {
  5. id := m.Message.Id
  6. mailContent, err := srv.Users.Messages.Get("me", id).Format("full").Do()
  7. if err != nil {
  8. log.Println("error when getting mail content: ", err)
  9. }
  10. if mailContent != nil {
  11. if mailContent.Payload != nil {
  12. payload := mailContent.Payload.Body
  13. data, err := b64.RawURLEncoding.DecodeString(payload.Data)
  14. if err != nil {
  15. log.Println("error b64 decoding: ", err)
  16. }
  17. body := string(data)
  18. if len(body) > 0 {
  19. subject := getSubject(mailContent)
  20. log.Println("subject ", subject)
  21. return body, subject
  22. }
  23. }
  24. }
  25. }
  26. }
  27. return "No email to process, something's wrong - GetEmail func", ""
  28. }
英文:

When I send a request to get the email body, the Gmail API returns everything but the body data on the payload object.

Things I've tried so far

  • The "Watch" method is already implemented and working fine
  • As you can see from the screenshot, the response shows the "snipped", which means that the message get is working, but the body data and the "raw" field is still empty.
  • I am using the history id correctly (saving the current one to use for subsequent requests)
  • upgrade all the dependencies to the latest stable version

Am I missing anything?

  1. func GetEmail(srv *gmail.Service, historyId uint64) (string, string) {
  2. hist := getHistory(srv, historyId)
  3. for _, h := range hist.History {
  4. for _, m := range h.MessagesAdded {
  5. id := m.Message.Id
  6. mailContent, err := srv.Users.Messages.Get("me", id).Format("full").Do()
  7. if err != nil {
  8. log.Println("error when getting mail content: ", err)
  9. }
  10. if mailContent != nil {
  11. if mailContent.Payload != nil {
  12. payload := mailContent.Payload.Body
  13. data, err := b64.RawURLEncoding.DecodeString(payload.Data)
  14. if err != nil {
  15. log.Println("error b64 decoding: ", err)
  16. }
  17. body := string(data)
  18. if len(body) > 0 {
  19. subject := getSubject(mailContent)
  20. log.Println("subject ", subject)
  21. return body, subject
  22. }
  23. }
  24. }
  25. }
  26. }
  27. return "No email to process, something's wrong - GetEmail func", ""
  28. }

答案1

得分: 1

如果你想要原始的消息数据,你需要使用Format("RAW")

  1. func GetEmail(srv *gmail.Service, messageId string) {
  2. gmailMessageResposne, err := srv.Users.Messages.Get("me", messageId).Format("RAW").Do()
  3. if err != nil {
  4. log.Println("获取邮件内容时出错:", err)
  5. }
  6. if gmailMessageResposne != nil {
  7. decodedData, err := base64.RawURLEncoding.DecodeString(gmailMessageResposne.Raw)
  8. if err != nil {
  9. log.Println("b64解码时出错:", err)
  10. }
  11. fmt.Printf("- %s\n", decodedData)
  12. }
  13. }

好问题,很有趣 😁

如何使用Go读取Gmail电子邮件正文?

英文:

If you want the RAW message data then you need to use Format("RAW")

  1. func GetEmail(srv *gmail.Service, messageId string) {
  2. gmailMessageResposne, err := srv.Users.Messages.Get("me", messageId).Format("RAW").Do()
  3. if err != nil {
  4. log.Println("error when getting mail content: ", err)
  5. }
  6. if gmailMessageResposne != nil {
  7. decodedData, err := base64.RawURLEncoding.DecodeString(gmailMessageResposne.Raw)
  8. if err != nil {
  9. log.Println("error b64 decoding: ", err)
  10. }
  11. fmt.Printf("- %s\n", decodedData)
  12. }
  13. }

Good question that was fun 😁

How to read a gmail email body with Go?

huangapple
  • 本文由 发表于 2022年12月14日 18:59:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/74797190.html
匿名

发表评论

匿名网友

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

确定