Pārlūkot izejas kodu

aggiunta opzione "source" che consente di indicare un file da leggere al posto di body

mauro 5 dienas atpakaļ
vecāks
revīzija
402720cf54
3 mainītis faili ar 83 papildinājumiem un 9 dzēšanām
  1. 16 0
      mail-noauth.ini
  2. 60 7
      mail.go
  3. 7 2
      mail.ini

+ 16 - 0
mail-noauth.ini

@@ -0,0 +1,16 @@
+[mail]
+server= nuc1
+porta=25
+#protocol ssl/tls/no
+protocol = no
+# authentication yes/no
+authentication = no
+username = acliservice@altemica.local
+password = uei551hex
+_from = test@altemica.net
+_to = server@altemica.net, mauro@altemica.com
+_subject = Sperem
+_body = Sperem Body
+# nel caso fosse necessario inviare un file. (che annulla _text
+_source = 
+

+ 60 - 7
mail.go

@@ -6,8 +6,18 @@ import(
   "966.it/ini"
   "966.it/opt"
   "fmt"
-  //"os"
+  "os"
+  "bufio"
 )
+
+
+func magagna(err error) bool {
+    if err != nil {
+	    return true
+    }
+    return false
+}
+    
 func main() {
 
   //var cfg *ini.File
@@ -16,14 +26,16 @@ func main() {
   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())   
+  //fmt.Println(inip.GetMySection())   
+  inip.GetMySection()
 
 /*
   var err error
@@ -42,16 +54,19 @@ for _,x := range keys {
   }
 */
 
-
 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","_text").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 {
@@ -66,6 +81,9 @@ if len(*struttura.IniSubject) > 0 {
 if len(*struttura.IniBody) > 0 {
 	_body = *struttura.IniBody
 }
+if len(*struttura.IniSource) > 0 {
+	_source = *struttura.IniSource
+}
 
 for _,tos := range _to {
   m := gomail.NewMessage()
@@ -74,17 +92,52 @@ for _,tos := range _to {
   m.SetHeader("Subject",_subject)
   m.SetBody("Text/plain",_body)
 
-  d := gomail.NewDialer(server,porta,username,password)
+  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)
   }
+
+
 }
 }

+ 7 - 2
mail.ini

@@ -1,12 +1,17 @@
 [mail]
 server= mail.altemica.net
 porta=465
+// protocol ssl/tls/no
 protocol = ssl
+// authentication yes/no
+authentication = yes
 username = acliservice@altemica.local
 password = uei551hex
+// da presenti nel corpo della mail
 _from = test@altemica.net
 _to = server@altemica.net, mauro@altemica.com
 _subject = Sperem
-_text = Sperem Body
-
+_body = Sperem Body
+// nel caso fosse necessario inviare un file. (che annulla _text
+_source =