diff options
| author | Nikolaus Gotsche <n@softwarefools.com> | 2018-02-21 03:25:05 +0100 |
|---|---|---|
| committer | Nikolaus Gotsche <n@softwarefools.com> | 2018-02-21 03:25:05 +0100 |
| commit | 27b333a73524b9a99145f0ca1f2a3a0181145351 (patch) | |
| tree | 23f26f7a9a31bd72b3664e684ed7d4a96441e8d3 /main.go | |
Diffstat (limited to 'main.go')
| -rw-r--r-- | main.go | 113 |
1 files changed, 113 insertions, 0 deletions
@@ -0,0 +1,113 @@ +package main + +import ( + "bytes" + "fmt" + "html/template" + "io" + "os" + "encoding/csv" + "strconv" + + "gopkg.in/gomail.v2" +) + +type Customer struct { + Name string + email string + Anrede string + gender int +} +type MailList struct { + customers []Customer +} + +var list []Customer +var filename string = "testlist.csv" + +func init() { + //cryptexample() + fmt.Println(" Starting MassMailer\n ##################################\n") + initConf() + + file, err := os.Open(config.Massmail.Listfile) + if err != nil { + panic(err) + }else{ + fmt.Println("\n",config.Massmail.Listfile, "opened Successfully!") + } + defer file.Close() + + reader := csv.NewReader(file) + reader.Comma = ';' + lineCount := 0 + + + + for { + line , erro := reader.Read() + if erro == io.EOF { + break + }else if erro != nil { + panic(erro) + } + gen, err := strconv.Atoi(line[2]) + if err != nil { + panic(err) + } + anrede := config.Massmail.Anrede[(gen+1)] + cn := Customer{Name: line[0], email: line[1], Anrede: anrede, gender: gen} + list = append(list, cn) + fmt.Println(cn) + lineCount += 1 + } + fmt.Println("\n All Customers:",lineCount) +} + +func main() { + if isSure("Send the Mail to all Customers?"){ + for _ , c := range list{ + err := SendMail(c) + if err==nil{ + fmt.Println("Sending Mail to",c.Anrede,c.Name,"Done Successful") + }else{ + panic(err) + } + } + }else{ + fmt.Println(" ok... bye then!") + } +} + +func SendMail(cust Customer) error { + t, err := template.ParseFiles(config.Massmail.Mailfile) + if err != nil { + return err + } + buf := new(bytes.Buffer) + err = t.Execute(buf, cust) + if err != nil { + return err + } + pass := Decrypt(config.Mailacc.Accountpwd) + + m := gomail.NewMessage() + m.SetHeader("From", m.FormatAddress(config.Mailacc.Address , config.Mailacc.Name)) + m.SetHeader("To", cust.email) + //m.SetAddressHeader("Cc", "test@mail.com", "Dan") + m.SetHeader("Subject", config.Massmail.Subject) + m.SetBody("text/html", buf.String()) + //m.Attach("someimage.jpg", gomail.Rename("logo.jpg")) + m.Embed(config.Massmail.Bannerfile) + + d := gomail.NewDialer(config.Mailacc.Smtpurl, config.Mailacc.Port, config.Mailacc.Accountname, pass) + + // Send the email to Bob, Cora and Dan. + if err := d.DialAndSend(m); err != nil { + fmt.Println("Connecting to Email Server Failed! Check your Accountname and Password\nError:") + panic(err) + } + return nil +} + + |
