/
gmailparser.go
54 lines (45 loc) · 1.36 KB
/
gmailparser.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Package gmailparser implements a way to parse emails.
package gmailparser
import (
"google.golang.org/api/gmail/v1"
)
// GetMessageBody finds the HTML body of an email.
func GetMessageBody(parts []*gmail.MessagePart) string {
for _, part := range parts {
if len(part.Parts) > 0 {
return GetMessageBody(part.Parts)
} else {
if part.MimeType == "text/html" {
return part.Body.Data
}
}
}
return ""
}
// GetMessageSender goes through the headers to find the From header.
func GetMessageSender(headers []*gmail.MessagePartHeader) string {
return GetMessageHeader(headers, "From")
}
// GetMessageSubject goes through the headers to find the Subject header.
func GetMessageSubject(headers []*gmail.MessagePartHeader) string {
return GetMessageHeader(headers, "Subject")
}
// GetMessageHeader goes through a list of headers and returns the header where
// the name matches the one we want.
func GetMessageHeader(headers []*gmail.MessagePartHeader, wanted string) string {
for _, header := range headers {
if header.Name == wanted {
return header.Value
}
}
return ""
}
// Email represents an email fetched from your gmail account.
type Email struct {
Size int64 `json:"size"`
Subject string `json:"subject"`
Body string `json:"body"` // Base64.URLEncoding
ID string `json:"id"`
Sender string `json:"sender"`
Date string `json:"date"`
}