gomail/main.go
Michael 2ba4e0eddc support for multiple emails at once
Signed-off-by: Michael <michael.lindman@gmail.com>
2021-07-22 01:49:45 +01:00

50 lines
1.2 KiB
Go

package main
import (
"flag"
"fmt"
"os"
"git.0cd.xyz/michael/gomail/mail"
"git.0cd.xyz/michael/gtools/logger"
)
func main() {
cfgFile, mailbox, msgs := ui()
cfg := NewConfig(*cfgFile)
logger := logger.New(cfg.Logger.Path, cfg.Logger.Mode, cfg.Logger.Level)
for _, account := range cfg.Account {
conn, err := mail.Login(cfg.Addr, account.Username, account.Password)
if err != nil {
logger.Error.Fatal(err)
}
defer conn.Client.Logout()
messages, err := conn.GetMessages(*mailbox, int32(*msgs))
if err != nil {
logger.Error.Fatal(err)
}
path := "./msgs/" + account.Username
if _, err := os.Stat(path); os.IsNotExist(err) {
os.Mkdir(path, 0775)
}
if err := mail.WriteMessages(path, messages); err != nil {
logger.Error.Fatal(err)
}
}
}
func ui() (cfgFile, mailbox *string, msgs *int) {
flag.Usage = func() {
fmt.Printf("Usage of %s:\n", os.Args[0])
flag.PrintDefaults()
}
cfgFile = flag.String("cfgFile", "./config.yaml", "path to config file")
mailbox = flag.String("mailbox", "inbox", "mailbox to scan messages")
msgs = flag.Int("msgs", 100, "Number of messages to pull")
flag.Parse()
return cfgFile, mailbox, msgs
}