-
Notifications
You must be signed in to change notification settings - Fork 1
/
send_email.go
70 lines (56 loc) · 1.4 KB
/
send_email.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package main
import (
"fmt"
"io/ioutil"
"log"
"net/mail"
"net/smtp"
"path/filepath"
"time"
"github.com/BurntSushi/toml"
"github.com/scorredoira/email"
)
type config struct {
FromEmailID string
FromName string
Password string
ToEmailIds []string
FileTypes []string
}
func main() {
var conf config
if _, err := toml.DecodeFile("config.toml", &conf); err != nil {
panic(err)
}
todayDate := fmt.Sprintf("%d-%d-%d", time.Now().Day(), time.Now().Month(), time.Now().Year())
m := email.NewMessage("Lab Report "+todayDate, "Lab Report")
m.From = mail.Address{Name: conf.FromName, Address: conf.FromEmailID}
m.To = conf.ToEmailIds
files, err := ioutil.ReadDir(".")
if err != nil {
panic(err)
}
numberOfAttachedFiles := 0
for _, file := range files {
fileDate := fmt.Sprintf("%d-%d-%d", file.ModTime().Day(), file.ModTime().Month(), file.ModTime().Year())
validExtension := false
for _, fileType := range conf.FileTypes {
if fileType == filepath.Ext(file.Name()) {
validExtension = true
break
}
}
if validExtension && fileDate == todayDate {
err = m.Attach(file.Name())
numberOfAttachedFiles++
if err != nil {
panic(err)
}
}
}
fmt.Printf("Going to send %d fies\n", numberOfAttachedFiles)
err = email.Send("smtp.gmail.com:587", smtp.PlainAuth("", conf.FromEmailID, conf.Password, "smtp.gmail.com"), m)
if err != nil {
log.Println(err.Error())
}
}