Skip to content

Commit

Permalink
use the mailbox attributes to determine the mailbox name to process. …
Browse files Browse the repository at this point in the history
…If specified via configuration, use as-is.
  • Loading branch information
AnalogJ committed Jan 20, 2022
1 parent 322bbf8 commit 8e11bc4
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
1 change: 0 additions & 1 deletion pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ func (c *configuration) Init() error {
c.SetDefault("imap-port", "993")
c.SetDefault("imap-username", "")
c.SetDefault("imap-password", "")
c.SetDefault("imap-mailbox-name", "[Gmail]/All Mail")

c.SetDefault("output-path", "sender_report.csv")

Expand Down
34 changes: 33 additions & 1 deletion pkg/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,16 @@ func (ee *EmailEngine) Start() error {
// Don't forget to logout
defer ee.client.Logout()

var mailboxName string
if ee.configuration.IsSet("imap-mailbox-name") {
mailboxName = ee.configuration.GetString("imap-mailbox-name")
ee.logger.Infof("Mailbox name from configuration: %s", mailboxName)
} else {
mailboxName = ee.getMailboxName()
ee.logger.Infof("Mailbox name: %s", mailboxName)

}

//retrieve messages from mailbox
//note: the number of messages may be absurdly large, so lets do this in batches for safety (sets of 100 messages)

Expand All @@ -77,7 +87,7 @@ func (ee *EmailEngine) Start() error {

// get latest mailbox information
//https://bitmapcake.blogspot.com/2018/07/gmail-mailbox-names-for-imap-connections.html
mbox, err := ee.client.Select(ee.configuration.GetString("imap-mailbox-name"), false)
mbox, err := ee.client.Select(mailboxName, false)
if err != nil {
ee.logger.Fatal(err)
}
Expand Down Expand Up @@ -156,6 +166,28 @@ func (ee *EmailEngine) Export() error {
return nil
}

//depending on the user's language settings, the Gmail/All Mail string may differ
//instead we'll look for the \All attribute, then findind the associated Gmail inbox name
func (ee *EmailEngine) getMailboxName() string {
// List mailboxes
mailboxes := make(chan *imap.MailboxInfo, 10)
done := make(chan error, 1)
go func() {
done <- ee.client.List("", "*", mailboxes)
}()

for m := range mailboxes {
for _, attr := range m.Attributes {
if attr == `\All` {
return m.Name
}
}
}

// we didn't find the the `\All` attribute on any mailbox, fallback to Gmail's default
return "[Gmail]/All Mail"
}

func (ee *EmailEngine) retrieveMessages(seqset *imap.SeqSet) {
section := &imap.BodySectionName{Peek: true}

Expand Down

0 comments on commit 8e11bc4

Please sign in to comment.