mail.go 2.0 KB

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