support for multiple emails at once

Signed-off-by: Michael <michael.lindman@gmail.com>
This commit is contained in:
Michael 2021-07-22 01:49:45 +01:00
parent b14b816097
commit 2ba4e0eddc
3 changed files with 25 additions and 18 deletions

View File

@ -9,10 +9,12 @@ import (
// Config application configuration // Config application configuration
type Config struct { type Config struct {
Addr string `yaml:"addr"` Addr string `yaml:"addr"`
Username string `yaml:"username"` Account []struct {
Password string `yaml:"password"` Username string `yaml:"username"`
Logger struct { Password string `yaml:"password"`
}
Logger struct {
Path string `yaml:"path"` Path string `yaml:"path"`
Mode string `yaml:"mode"` Mode string `yaml:"mode"`
Level int `yaml:"level"` Level int `yaml:"level"`

View File

@ -84,13 +84,13 @@ func (mail *Mail) GetMessages(mailbox string, msgs int32) (*Messages, error) {
return &messages, nil return &messages, nil
} }
func WriteMessages(messages *Messages) error { func WriteMessages(path string, messages *Messages) error {
for _, msg := range messages.Message { for _, msg := range messages.Message {
body, err := ioutil.ReadAll(msg.Body) body, err := ioutil.ReadAll(msg.Body)
if err != nil { if err != nil {
return err 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 return err
} }
} }

29
main.go
View File

@ -14,18 +14,23 @@ func main() {
cfg := NewConfig(*cfgFile) cfg := NewConfig(*cfgFile)
logger := logger.New(cfg.Logger.Path, cfg.Logger.Mode, cfg.Logger.Level) logger := logger.New(cfg.Logger.Path, cfg.Logger.Mode, cfg.Logger.Level)
conn, err := mail.Login(cfg.Addr, cfg.Username, cfg.Password) for _, account := range cfg.Account {
if err != nil { conn, err := mail.Login(cfg.Addr, account.Username, account.Password)
logger.Error.Fatal(err) if err != nil {
} logger.Error.Fatal(err)
defer conn.Client.Logout() }
defer conn.Client.Logout()
messages, err := conn.GetMessages(*mailbox, int32(*msgs)) messages, err := conn.GetMessages(*mailbox, int32(*msgs))
if err != nil { if err != nil {
logger.Error.Fatal(err) logger.Error.Fatal(err)
} }
if err := mail.WriteMessages(messages); err != nil { path := "./msgs/" + account.Username
logger.Error.Fatal(err) if _, err := os.Stat(path); os.IsNotExist(err) {
os.Mkdir(path, 0775)
}
if err := mail.WriteMessages(path, messages); err != nil {
logger.Error.Fatal(err)
}
} }
} }