package main
import (
"fmt"
"os"
"time"
//"log"
//"net/smtp"
)
const myfile1 = "data1.txt"
const myfile2 = "data2.txt"
const myfile3 = "data3.txt"
const myfile4 = "data4.txt"
func main() {
c := time.Tick(5 * time.Second)
for _ = range c {
readLastLine(myfile1)
readLastLine(myfile2)
readLastLine(myfile3)
readLastLine(myfile4)
}
}
func readLastLine(fname string) {
file, err := os.Open(fname)
if err != nil {
panic(err)
}
defer file.Close()
fi, err := file.Stat()
if err != nil {
fmt.Println(err)
}
buf := make([]byte, 85)
n, err := file.ReadAt(buf, fi.Size()-int64(len(buf)))
if err != nil {
fmt.Println(err)
}
buf = buf[:n]
//fmt.Printf("%s", buf)
mkey := fmt.Sprintf("%s %s
","From: Server
" + "To: customermail@yahoo.in
" + "Subject: Last Saved on
"+"
",buf)
auth := smtp.PlainAuth("","","","",)
err = smtp.SendMail("",auth, "",[]string{""}, []byte(mkey),)
if err != nil {log.Printf("smtp error: %s", err)
return}
log.Print("Mail Sent.")
}
am trying to send 4 text files last line by email every 5 sec but result like this email sending 4 times. each file data separately :
last saved data from datafiles
sys1 last scan 2018-02-08 15:30:00
Mail Sent.
last saved data from datafiles
sys2 last scan 2018-02-08 15:35:00
Mail Sent.
last saved data from datafiles
sys3 last scan 2018-02-08 15:56:00
Mail Sent.
last saved data from datafiles
sys4 last scan 2018-02-08 15:30:00
Mail Sent.
but i want send email for 4 files last lines at once in one mail like this:
last saved data from datafiles
sys1 last scan 2018-02-08 15:30:00
sys2 last scan 2018-02-08 15:35:00
sys3 last scan 2018-02-08 15:56:00
sys4 last scan 2018-02-08 15:30:00
Mail Sent.
any suggestions plz.