mail.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package main
  2. import(
  3. "crypto/tls"
  4. "gopkg.in/gomail.v2"
  5. "966.it/ini"
  6. "966.it/opt"
  7. //"gopkg.in/ini.v1"
  8. "fmt"
  9. //"os"
  10. )
  11. func main() {
  12. //var cfg *ini.File
  13. iniFile := optp.Start()
  14. fmt.Println("parametro passato",iniFile)
  15. inip.GetIni(iniFile)
  16. _ = inip.LoadIni()
  17. // sezione di prova per vedere cosa ritorna dal file
  18. fmt.Println(inip.GetMySection())
  19. /*
  20. var err error
  21. mymail,err = cfg.GetSection("mail")
  22. if err != nil {
  23. fmt.Println("Errore nella lettura della sezione")
  24. fmt.Println(err)
  25. os.Exit(99)
  26. }
  27. var mymail *ini.Section
  28. keys := mymail.KeyStrings()
  29. for _,x := range keys {
  30. fmt.Println(x)
  31. }
  32. */
  33. server := inip.GetMyKey("mail","server").String()
  34. porta,_ := inip.GetMyKey("mail","porta").Int()
  35. username := inip.GetMyKey("mail","username").String()
  36. password := inip.GetMyKey("mail","password").String()
  37. _from := inip.GetMyKey("mail","_from").String()
  38. _to := inip.GetMyKey("mail","_to").Strings(",")
  39. protocol := inip.GetMyKey("mail","protocol").String()
  40. _subject := inip.GetMyKey("mail","_subject").String()
  41. _body := inip.GetMyKey("mail","_text").String()
  42. for _,tos := range _to {
  43. m := gomail.NewMessage()
  44. m.SetHeader("From",_from)
  45. m.SetHeader("To",tos)
  46. m.SetHeader("Subject",_subject)
  47. m.SetBody("Text/plain",_body)
  48. d := gomail.NewDialer(server,porta,username,password)
  49. if protocol == "ssl" {
  50. d.SSL=true
  51. } else if protocol == "tls" {
  52. d.TLSConfig = &tls.Config{InsecureSkipVerify: true}
  53. }
  54. // Send the email to Bob, Cora and Dan.
  55. if err := d.DialAndSend(m); err != nil {
  56. panic(err)
  57. }
  58. }
  59. }