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

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

Gmail API shows empty body when getting message

问题

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

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

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

我有什么遗漏吗?

func GetEmail(srv *gmail.Service, historyId uint64) (string, string) {
    hist := getHistory(srv, historyId)

    for _, h := range hist.History {
        for _, m := range h.MessagesAdded {
            id := m.Message.Id
            mailContent, err := srv.Users.Messages.Get("me", id).Format("full").Do()
            if err != nil {
                log.Println("error when getting mail content: ", err)
            }

            if mailContent != nil {
                if mailContent.Payload != nil {
                    payload := mailContent.Payload.Body
                    data, err := b64.RawURLEncoding.DecodeString(payload.Data)
                    if err != nil {
                        log.Println("error b64 decoding: ", err)
                    }
                    body := string(data)

                    if len(body) > 0 {
                        subject := getSubject(mailContent)
                        log.Println("subject ", subject)
                        return body, subject
                    }
                }
            } 
        }
    }

    return "No email to process, something's wrong - GetEmail func", ""
}
英文:

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?

func GetEmail(srv *gmail.Service, historyId uint64) (string, string) {
	hist := getHistory(srv, historyId)

	for _, h := range hist.History {
		for _, m := range h.MessagesAdded {
			id := m.Message.Id
			mailContent, err := srv.Users.Messages.Get("me", id).Format("full").Do()
			if err != nil {
				log.Println("error when getting mail content: ", err)
			}

			if mailContent != nil {
				if mailContent.Payload != nil {
					payload := mailContent.Payload.Body
					data, err := b64.RawURLEncoding.DecodeString(payload.Data)
					if err != nil {
						log.Println("error b64 decoding: ", err)
					}
					body := string(data)

					if len(body) > 0 {
						subject := getSubject(mailContent)
						log.Println("subject ", subject)
						return body, subject
					}
				}
			} 
		}
	}

	return "No email to process, something's wrong - GetEmail func", ""
}

答案1

得分: 1

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

func GetEmail(srv *gmail.Service, messageId string) {

    gmailMessageResposne, err := srv.Users.Messages.Get("me", messageId).Format("RAW").Do()
    if err != nil {
        log.Println("获取邮件内容时出错:", err)
    }

    if gmailMessageResposne != nil {

        decodedData, err := base64.RawURLEncoding.DecodeString(gmailMessageResposne.Raw)

        if err != nil {
            log.Println("b64解码时出错:", err)
        }
        fmt.Printf("- %s\n", decodedData)

    }
}

好问题,很有趣 😁

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

英文:

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

func GetEmail(srv *gmail.Service, messageId string) {

	gmailMessageResposne, err := srv.Users.Messages.Get("me", messageId).Format("RAW").Do()
	if err != nil {
		log.Println("error when getting mail content: ", err)
	}

	if gmailMessageResposne != nil {

		decodedData, err := base64.RawURLEncoding.DecodeString(gmailMessageResposne.Raw)

		if err != nil {
			log.Println("error b64 decoding: ", err)
		}
		fmt.Printf("- %s\n", decodedData)

	}
}

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:

确定