-
Notifications
You must be signed in to change notification settings - Fork 0
/
mail.go
67 lines (57 loc) · 1.37 KB
/
mail.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
package main
import (
"crypto/tls"
"gopkg.in/gomail.v2"
"log"
"time"
)
var MailChannel = make(chan *gomail.Message, 64)
func NewMail() *gomail.Message {
m := gomail.NewMessage()
m.SetHeader("From", Config.MailSender)
return m
}
func _initMailer(msg string) {
// queue up a test message to say that we have begun
go _MailerDaemon()
// return
m := NewMail()
m.SetHeader("To", "[email protected]")
// m.SetHeader("To", "[email protected]")
m.SetHeader("Subject", "CMMS has started on "+Config.Installation)
m.SetBody("text/html", "The CMMS server has been started @"+Config.Installation+":"+Config.MailServer+"\n"+msg)
MailChannel <- m
}
func _MailerDaemon() {
d := gomail.NewPlainDialer(Config.MailServer, Config.MailPort, Config.MailUser, Config.MailPasswd)
d.TLSConfig = &tls.Config{InsecureSkipVerify: true}
var s gomail.SendCloser
var err error
open := false
for {
select {
case m, ok := <-MailChannel:
if !ok {
return
}
if !open {
if s, err = d.Dial(); err != nil {
panic(err)
}
open = true
}
if err := gomail.Send(s, m); err != nil {
log.Print(err)
}
// Close the connection to the SMTP server if no email was sent in
// the last couple of minutes.
case <-time.After(120 * time.Second):
if open {
if err := s.Close(); err != nil {
panic(err)
}
open = false
}
}
}
}