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

@ -10,8 +10,10 @@ import (
// Config application configuration
type Config 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"`

View File

@ -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
}
}

11
main.go
View File

@ -14,20 +14,25 @@ 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)
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)
}
if err := mail.WriteMessages(messages); err != nil {
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() {