I'm trying to use the Gmail API to extract the bodies of emails, although I'm unable to actually retrieve the bodies of the emails, as all I get is the following:
<div dir="lt
<!DOCTYPE ht
I've written the following to collect the bodies of the emails:
messages, err := srv.Users.Messages.List(user).Do()
for _, l := range messages.Messages {
m, err := srv.Users.Messages.Get(user, l.Id).Do()
if err != nil {
log.Fatalf("Unable to retrieve labels: %v", err)
}
for _, part := range m.Payload.Parts {
if part.MimeType == "text/html" {
data, _ := base64.StdEncoding.DecodeString(part.Body.Data)
html := string(data)
fmt.Println(html)
}
}
}
Are there any ideas on how I can get the actual body of the email?
You should check the error returned for DecodeString
, but otherwise you should try using base64.URLEncoding
instead of base64.StdEncoding
to decode the data.
From the documentation on MessagePartBody
:
// Data: The body data of a MIME message part as a base64url encoded
// string. May be empty for MIME container types that have no message
// body or when the body data is sent as a separate attachment. An
// attachment ID is present if the body data is contained in a separate
// attachment.
Data string `json:"data,omitempty"`