mail.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. "bufio"
  11. )
  12. func magagna(err error) bool {
  13. if err != nil {
  14. return true
  15. }
  16. return false
  17. }
  18. func main() {
  19. //var cfg *ini.File
  20. // var struttura optp.IniStru
  21. struttura := optp.Start()
  22. fmt.Println("parametri:",struttura)
  23. // questo parametro è fondamentale, indica il file di configurazione
  24. iniFile := struttura.IniFile
  25. fmt.Println("parametro passato",iniFile)
  26. inip.GetIni(*iniFile)
  27. _ = inip.LoadIni()
  28. // sezione di prova per vedere cosa ritorna dal file
  29. //fmt.Println(inip.GetMySection())
  30. inip.GetMySection()
  31. /*
  32. var err error
  33. mymail,err = cfg.GetSection("mail")
  34. if err != nil {
  35. fmt.Println("Errore nella lettura della sezione")
  36. fmt.Println(err)
  37. os.Exit(99)
  38. }
  39. var mymail *ini.Section
  40. keys := mymail.KeyStrings()
  41. for _,x := range keys {
  42. fmt.Println(x)
  43. }
  44. */
  45. server := inip.GetMyKey("mail","server").String()
  46. porta,_ := inip.GetMyKey("mail","porta").Int()
  47. authentication := inip.GetMyKey("mail","authentication").String()
  48. username := inip.GetMyKey("mail","username").String()
  49. password := inip.GetMyKey("mail","password").String()
  50. _from := inip.GetMyKey("mail","_from").String()
  51. _to := inip.GetMyKey("mail","_to").Strings(",")
  52. protocol := inip.GetMyKey("mail","protocol").String()
  53. _subject := inip.GetMyKey("mail","_subject").String()
  54. _body := inip.GetMyKey("mail","_body").String()
  55. _source := inip.GetMyKey("mail","_source").String()
  56. fmt.Println("dalla configurazione:",_source)
  57. // sostituisco alcuni valori con quelli passati da iniStru
  58. if len(*struttura.IniFrom) > 0 {
  59. _from = *struttura.IniFrom
  60. }
  61. if len(*struttura.IniTo) > 0 {
  62. _to = strings.Split(*struttura.IniTo,",")
  63. }
  64. if len(*struttura.IniSubject) > 0 {
  65. _subject = *struttura.IniSubject
  66. }
  67. if len(*struttura.IniBody) > 0 {
  68. _body = *struttura.IniBody
  69. }
  70. if len(*struttura.IniSource) > 0 {
  71. _source = *struttura.IniSource
  72. }
  73. for _,tos := range _to {
  74. m := gomail.NewMessage()
  75. m.SetHeader("From",_from)
  76. m.SetHeader("To",tos)
  77. m.SetHeader("Subject",_subject)
  78. m.SetBody("Text/plain",_body)
  79. var body string
  80. if len(_source) > 0 {
  81. fmt.Println("Lettura file",_source)
  82. f,err := os.Open(_source)
  83. if magagna(err) {
  84. fmt.Println("Errore di apertura file ",_source)
  85. panic(err)
  86. //m.SetBody("Text/Plain",err)
  87. } else {
  88. fmt.Println("Scanning file")
  89. myr := bufio.NewScanner(f)
  90. for myr.Scan() {
  91. body += myr.Text()
  92. body += "\n"
  93. if magagna(err) {
  94. fmt.Println("Errore lettura file",_source)
  95. }
  96. }
  97. fmt.Println(body)
  98. m.SetBody("Text/Plain",body)
  99. }
  100. }
  101. var d *gomail.Dialer
  102. if authentication == "yes" {
  103. d = gomail.NewDialer(server,porta,username,password)
  104. } else {
  105. d = new(gomail.Dialer)
  106. d.Host = server
  107. d.Port = porta
  108. }
  109. if protocol == "ssl" {
  110. d.SSL=true
  111. } else if protocol == "tls" {
  112. d.TLSConfig = &tls.Config{InsecureSkipVerify: true}
  113. } else {
  114. d.TLSConfig = &tls.Config{InsecureSkipVerify: true}
  115. }
  116. // Send the email to Bob, Cora and Dan.
  117. if err := d.DialAndSend(m); err != nil {
  118. panic(err)
  119. }
  120. }
  121. }