| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- package main
- import(
- "crypto/tls"
- "strings"
- "gopkg.in/gomail.v2"
- "966.it/ini"
- "966.it/opt"
- "fmt"
- "os"
- "bufio"
- )
- func magagna(err error) bool {
- if err != nil {
- return true
- }
- return false
- }
-
- func main() {
- //var cfg *ini.File
- // var struttura optp.IniStru
- struttura := optp.Start()
- fmt.Println("parametri:",struttura)
- // questo parametro è fondamentale, indica il file di configurazione
- iniFile := struttura.IniFile
- fmt.Println("parametro passato",iniFile)
- inip.GetIni(*iniFile)
- _ = inip.LoadIni()
- // sezione di prova per vedere cosa ritorna dal file
- //fmt.Println(inip.GetMySection())
- inip.GetMySection()
- /*
- var err error
- mymail,err = cfg.GetSection("mail")
- if err != nil {
- fmt.Println("Errore nella lettura della sezione")
- fmt.Println(err)
- os.Exit(99)
- }
- var mymail *ini.Section
- keys := mymail.KeyStrings()
- for _,x := range keys {
- fmt.Println(x)
- }
- */
- server := inip.GetMyKey("mail","server").String()
- porta,_ := inip.GetMyKey("mail","porta").Int()
- authentication := inip.GetMyKey("mail","authentication").String()
- username := inip.GetMyKey("mail","username").String()
- password := inip.GetMyKey("mail","password").String()
- _from := inip.GetMyKey("mail","_from").String()
- _to := inip.GetMyKey("mail","_to").Strings(",")
- protocol := inip.GetMyKey("mail","protocol").String()
- _subject := inip.GetMyKey("mail","_subject").String()
- _body := inip.GetMyKey("mail","_body").String()
- _source := inip.GetMyKey("mail","_source").String()
- fmt.Println("dalla configurazione:",_source)
- // sostituisco alcuni valori con quelli passati da iniStru
- if len(*struttura.IniFrom) > 0 {
- _from = *struttura.IniFrom
- }
- if len(*struttura.IniTo) > 0 {
- _to = strings.Split(*struttura.IniTo,",")
- }
- if len(*struttura.IniSubject) > 0 {
- _subject = *struttura.IniSubject
- }
- if len(*struttura.IniBody) > 0 {
- _body = *struttura.IniBody
- }
- if len(*struttura.IniSource) > 0 {
- _source = *struttura.IniSource
- }
- for _,tos := range _to {
- m := gomail.NewMessage()
- m.SetHeader("From",_from)
- m.SetHeader("To",tos)
- m.SetHeader("Subject",_subject)
- m.SetBody("Text/plain",_body)
- var body string
- if len(_source) > 0 {
- fmt.Println("Lettura file",_source)
- f,err := os.Open(_source)
- if magagna(err) {
- fmt.Println("Errore di apertura file ",_source)
- panic(err)
- //m.SetBody("Text/Plain",err)
- } else {
- fmt.Println("Scanning file")
- myr := bufio.NewScanner(f)
- for myr.Scan() {
- body += myr.Text()
- body += "\n"
- if magagna(err) {
- fmt.Println("Errore lettura file",_source)
- }
- }
- fmt.Println(body)
- m.SetBody("Text/Plain",body)
- }
- }
- var d *gomail.Dialer
- if authentication == "yes" {
- d = gomail.NewDialer(server,porta,username,password)
- } else {
- d = new(gomail.Dialer)
- d.Host = server
- d.Port = porta
- }
- if protocol == "ssl" {
- d.SSL=true
- } else if protocol == "tls" {
- d.TLSConfig = &tls.Config{InsecureSkipVerify: true}
- } else {
- d.TLSConfig = &tls.Config{InsecureSkipVerify: true}
- }
- // Send the email to Bob, Cora and Dan.
- if err := d.DialAndSend(m); err != nil {
- panic(err)
- }
- }
- }
|