Documentation
¶
Overview ¶
Package inboxer is a Go package for checking your gmail inbox, it has the * following features: * * - Mark emails (read/unread/important/etc) * - Get labels used in inbox * - Get emails by query (eg "in:sent after:2017/01/01 before:2017/01/30") * - Get email metadata * - Get email main body ("text/plain", "text/html") * - Get the number of unread messages * - Convert email dates to human readable format * ******************************************************************************* * USE ******************************************************************************* * CREDENTIALS: * * For inboxer to work you must have a gmail account and a file named * "client_secret.json" containing your authorization info in the root directory * of your project. To obtain credentials please see step one of this guide: * https://developers.google.com/gmail/api/quickstart/go * * >Step 1: Turn on the Gmail API
* >Use this wizard (https://console.developers.google.com/start/api?id=gmail) to create or select a project in the Google Developers Console and automatically turn on the API. Click Continue, then Go to credentials. * >On the Add credentials to your project page, click the Cancel button. * >At the top of the page, select the OAuth consent screen tab. Select an Email address, enter a Product name if not already set, and click the Save button. * >Select the Credentials tab, click the Create credentials button and select OAuth client ID. * >Select the application type Other, enter the name "Gmail API Quickstart", and click the Create button. * >Click OK to dismiss the resulting dialog. * >Click the file_download (Download JSON) button to the right of the client ID. * >Move this file to your working directory and rename it client_secret.json.
package main
import (
"context" "fmt" "gitlab.com/hartsfield/gmailAPI" "gitlab.com/hartsfield/inboxer" gmail "google.golang.org/api/gmail/v1"
)
func main() { // Connect to the gmail API service. ctx := context.Background() srv := gmailAPI.ConnectToService(ctx, gmail.MailGoogleComScope) msgs, err := inboxer.Query(srv, "category:forums after:2017/01/01 before:2017/01/30") if err != nil { fmt.Println(err) } // Range over the messages for _, msg := range msgs { fmt.Println("========================================================") time, err := inboxer.ReceivedTime(msg.InternalDate) if err != nil { fmt.Println(err) } fmt.Println("Date: ", time) md := inboxer.GetPartialMetadata(msg) fmt.Println("From: ", md.From) fmt.Println("Sender: ", md.Sender) fmt.Println("Subject: ", md.Subject) fmt.Println("Delivered To: ", md.DeliveredTo) fmt.Println("To: ", md.To) fmt.Println("CC: ", md.CC) fmt.Println("Mailing List: ", md.MailingList) fmt.Println("Thread-Topic: ", md.ThreadTopic) fmt.Println("Snippet: ", msg.Snippet) body, err := inboxer.GetBody(msg, "text/plain") if err != nil { fmt.Println(err) } fmt.Println(body) } }
******************************************************************************* * QUERIES *******************************************************************************
func main() { // Connect to the gmail API service. ctx := context.Background() srv := gmailAPI.ConnectToService(ctx, gmail.GmailComposeScope) msgs, err := inboxer.Query(srv, "category:forums after:2017/01/01 before:2017/01/30") if err != nil { fmt.Println(err) } // Range over the messages for _, msg := range msgs { // do stuff } }
******************************************************************************* * MARKING EMAILS *******************************************************************************
func main() { // Connect to the gmail API service. ctx := context.Background() srv := gmailAPI.ConnectToService(ctx, gmail.GmailComposeScope) msgs, err := inboxer.Query(srv, "category:forums after:2017/01/01 before:2017/01/30") if err != nil { fmt.Println(err) } req := &gmail.ModifyMessageRequest{ RemoveLabelIds: []string{"UNREAD"}, AddLabelIds: []string{"OLD"} } // Range over the messages for _, msg := range msgs { msg, err := inboxer.MarkAs(srv, msg, req) } }
******************************************************************************* * MARK ALL "UNREAD" EMAILS AS "READ" *******************************************************************************
func main() { // Connect to the gmail API service. ctx := context.Background() srv := gmailAPI.ConnectToService(ctx, gmail.GmailComposeScope) inboxer.MarkAllAsRead(srv) }
******************************************************************************* * GETTING LABELS *******************************************************************************
func main() { // Connect to the gmail API service. ctx := context.Background() srv := gmailAPI.ConnectToService(ctx, gmail.GmailComposeScope) labels, err := inboxer.GetLabels(srv) if err != nil { fmt.Println(err) } for _, label := range labels { fmt.Println(label) } }
******************************************************************************* * METADATA *******************************************************************************
func main() { // Connect to the gmail API service. ctx := context.Background() srv := gmailAPI.ConnectToService(ctx, gmail.MailGoogleComScope) msgs, err := inboxer.Query(srv, "category:forums after:2017/01/01 before:2017/01/30") if err != nil { fmt.Println(err) } // Range over the messages for _, msg := range msgs { fmt.Println("========================================================") md := inboxer.GetPartialMetadata(msg) fmt.Println("From: ", md.From) fmt.Println("Sender: ", md.Sender) fmt.Println("Subject: ", md.Subject) fmt.Println("Delivered To: ", md.DeliveredTo) fmt.Println("To: ", md.To) fmt.Println("CC: ", md.CC) fmt.Println("Mailing List: ", md.MailingList) fmt.Println("Thread-Topic: ", md.ThreadTopic) } }
******************************************************************************* * GETTING THE EMAIL BODY *******************************************************************************
func main() { // Connect to the gmail API service. ctx := context.Background() srv := gmailAPI.ConnectToService(ctx, gmail.GmailComposeScope) msgs, err := inboxer.Query(srv, "category:forums after:2017/01/01 before:2017/01/30") if err != nil { fmt.Println(err) } // Range over the messages for _, msg := range msgs { body, err := inboxer.GetBody(msg, "text/plain") if err != nil { fmt.Println(err) } fmt.Println(body) } }
******************************************************************************* * GETTING THE NUMBER OF UNREAD MESSAGES ******************************************************************************* // NOTE: to actually view the email text use inboxer.Query and query for unread // emails.
func main() { // Connect to the gmail API service. ctx := context.Background() srv := gmailAPI.ConnectToService(ctx, gmail.GmailComposeScope) // num will be -1 on err num, err := inboxer.CheckForUnread(srv) if err != nil { fmt.Println(err) } fmt.Printf("You have %s unread emails.", num) }
******************************************************************************* * CONVERTING DATES ******************************************************************************* // Convert UNIX time stamps to human readable format
func main() { // Connect to the gmail API service. ctx := context.Background() srv := gmailAPI.ConnectToService(ctx, gmail.GmailComposeScope) msgs, err := inboxer.Query(srv, "category:forums after:2017/01/01 before:2017/01/30") if err != nil { fmt.Println(err) } // Range over the messages for _, msg := range msgs { // Convert the date time, err := inboxer.ReceivedTime(msg.InternalDate) if err != nil { fmt.Println(err) } fmt.Println("Date: ", time) } }
******************************************************************************* * SNIPPET ******************************************************************************* // Snippets are not really part of the package but I'm including them in the doc // because they'll likely be useful to anyone working with this package.
func main() { // Connect to the gmail API service. ctx := context.Background() srv := gmailAPI.ConnectToService(ctx, gmail.GmailComposeScope) msgs, err := inboxer.Query(srv, "category:forums after:2017/01/01 before:2017/01/30") if err != nil { fmt.Println(err) } // Range over the messages for _, msg := range msgs { // this one is part of the api fmt.Println(msg.Snippet) } }
Index ¶
- func CheckForUnread(srv *gmail.Service) (int64, error)
- func CheckForUnreadByLabel(srv *gmail.Service, label string) (int64, error)
- func GetBody(msg *gmail.Message, mimeType string) (string, error)
- func GetLabels(srv *gmail.Service) (*gmail.ListLabelsResponse, error)
- func GetMessages(srv *gmail.Service, howMany uint) ([]*gmail.Message, error)
- func MarkAllAsRead(srv *gmail.Service) error
- func MarkAs(srv *gmail.Service, msg *gmail.Message, req *gmail.ModifyMessageRequest) (*gmail.Message, error)
- func Query(srv *gmail.Service, query string) ([]*gmail.Message, error)
- func ReceivedTime(datetime int64) (time.Time, error)
- type PartialMetadata
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CheckForUnread ¶
CheckForUnread checks for mail labeled "UNREAD". NOTE: When checking your inbox for unread messages, it's not uncommon for it to return thousands of unread messages that you don't know about. To see them in gmail, query your mail for "label:unread". For CheckForUnread to work properly you need to mark all mail as read either through gmail or through the MarkAllAsRead() function found in this library.
func CheckForUnreadByLabel ¶
CheckForUnreadByLabel checks for unread mail matching the specified label. NOTE: When checking your inbox for unread messages, it's not uncommon for it to return thousands of unread messages that you don't know about. To see them in gmail, query your mail for "label:unread". For CheckForUnreadByLabel to work "properly" you need to mark all mail as read either through gmail or through the MarkAllAsRead() function found in this library (or deal with it).
func GetBody ¶
GetBody gets, decodes, and returns the body of the email. It returns an error if decoding goes wrong. mimeType is used to indicate whether you want the plain text or html encoding ("text/html", "text/plain").
func GetLabels ¶
func GetLabels(srv *gmail.Service) (*gmail.ListLabelsResponse, error)
GetLabels gets a list of the labels used in the users inbox.
func GetMessages ¶
GetMessages gets and returns gmail messages
func MarkAllAsRead ¶
MarkAllAsRead removes the UNREAD label from all emails.
func MarkAs ¶
func MarkAs(srv *gmail.Service, msg *gmail.Message, req *gmail.ModifyMessageRequest) (*gmail.Message, error)
MarkAs allows you to mark an email with a specific label using the gmail.ModifyMessageRequest struct.
Types ¶
type PartialMetadata ¶
type PartialMetadata struct { // Sender is the entity that originally created and sent the message Sender string // From is the entity that sent the message to you (e.g. googlegroups). Most // of the time this information is only relevant to mailing lists. From string // Subject is the email subject Subject string // Mailing list contains the name of the mailing list that the email was // posted to, if any. MailingList string // CC is the "carbon copy" list of addresses CC []string // To is the recipient(s) of the email (sometimes emails are sent to more // than one person). To []string // ThreadTopic contains the topic of the thread (e.g. google groups threads) ThreadTopic []string // DeliveredTo is who the email was sent to. This can contain multiple // addresses if the email was forwarded. DeliveredTo []string }
PartialMetadata stores email metadata. Some fields may sound redundant, but in fact have different contexts. Some are slices of string because the ones that have multiple values are still being sorted from those that don't.
func GetPartialMetadata ¶
func GetPartialMetadata(msg *gmail.Message) *PartialMetadata
GetPartialMetadata gets some of the useful metadata from the headers.