support for multiple emails at once
Signed-off-by: Michael <michael.lindman@gmail.com>
This commit is contained in:
parent
b14b816097
commit
2ba4e0eddc
@ -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"`
|
||||
|
@ -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
11
main.go
@ -14,19 +14,24 @@ 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) {
|
||||
|
Loading…
Reference in New Issue
Block a user