From 2ba4e0eddc6355c433f50f68965bf4ffb15d468d Mon Sep 17 00:00:00 2001 From: Michael Date: Thu, 22 Jul 2021 01:49:45 +0100 Subject: [PATCH] support for multiple emails at once Signed-off-by: Michael --- config.go | 10 ++++++---- mail/mail.go | 4 ++-- main.go | 29 +++++++++++++++++------------ 3 files changed, 25 insertions(+), 18 deletions(-) diff --git a/config.go b/config.go index 52f2093..302cec9 100644 --- a/config.go +++ b/config.go @@ -9,10 +9,12 @@ import ( // Config application configuration type Config struct { - Addr string `yaml:"addr"` - Username string `yaml:"username"` - Password string `yaml:"password"` - Logger struct { + Addr string `yaml:"addr"` + Account []struct { + Username string `yaml:"username"` + Password string `yaml:"password"` + } + Logger struct { Path string `yaml:"path"` Mode string `yaml:"mode"` Level int `yaml:"level"` diff --git a/mail/mail.go b/mail/mail.go index a5a13f3..40bf2d1 100644 --- a/mail/mail.go +++ b/mail/mail.go @@ -84,13 +84,13 @@ func (mail *Mail) GetMessages(mailbox string, msgs int32) (*Messages, error) { return &messages, nil } -func WriteMessages(messages *Messages) error { +func WriteMessages(path string, messages *Messages) error { for _, msg := range messages.Message { body, err := ioutil.ReadAll(msg.Body) if err != nil { return err } - if err := ioutil.WriteFile("./msgs/"+strconv.Itoa(int(msg.SeqNum))+"_"+msg.To[0].Address()+".eml", body, 0644); err != nil { + if err := ioutil.WriteFile(path+"/"+strconv.Itoa(int(msg.SeqNum))+"_"+msg.To[0].Address()+".eml", body, 0644); err != nil { return err } } diff --git a/main.go b/main.go index eebb80b..524da35 100644 --- a/main.go +++ b/main.go @@ -14,18 +14,23 @@ func main() { cfg := NewConfig(*cfgFile) logger := logger.New(cfg.Logger.Path, cfg.Logger.Mode, cfg.Logger.Level) - conn, err := mail.Login(cfg.Addr, cfg.Username, cfg.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) - } - if err := mail.WriteMessages(messages); err != nil { - logger.Error.Fatal(err) + 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) + } } }