/
email.go
120 lines (105 loc) · 2.76 KB
/
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package tools
import (
"bytes"
"context"
"fmt"
"net/smtp"
"os/exec"
"strings"
"time"
"github.com/TUM-Dev/gocast/dao"
)
type Mailer struct {
Dao dao.DaoWrapper
MaxMailsPerMinute int
}
func NewMailer(dao dao.DaoWrapper, maxMailsPerMinute int) *Mailer {
if maxMailsPerMinute == 0 {
maxMailsPerMinute = 10
}
return &Mailer{Dao: dao, MaxMailsPerMinute: maxMailsPerMinute}
}
func (m *Mailer) Run() {
lastRun := time.Now().Add(-time.Minute)
for {
if time.Since(lastRun) < time.Minute {
time.Sleep(time.Until(lastRun.Add(time.Minute)))
}
lastRun = time.Now()
emails, err := m.Dao.EmailDao.GetDue(context.Background(), m.MaxMailsPerMinute)
if err != nil {
logger.Error("error getting due emails", "err", err)
continue
}
for _, email := range emails {
err := m.sendMail(Cfg.Mail.Server, email.From, email.Subject, email.Body, []string{email.To})
if err != nil {
email.LastTry = time.Now()
email.Retries++
email.Errors += fmt.Sprintf("%v\n", err)
} else {
email.Success = true
}
err = m.Dao.EmailDao.Save(context.Background(), &email)
if err != nil {
logger.Error("error saving email", "err", err)
}
sleepDur := time.Duration(1000 * (60 / m.MaxMailsPerMinute))
time.Sleep(sleepDur)
}
}
}
func (m *Mailer) sendMail(addr, from, subject, body string, to []string) error {
logger.Info("sending mail", "to", to, "subject", subject, "body", body)
r := strings.NewReplacer("\r\n", "", "\r", "", "\n", "", "%0a", "", "%0d", "")
signed, err := openssl([]byte(body), "smime", "-text", "-sign", "-signer", Cfg.Mail.SMIMECert, "-inkey", Cfg.Mail.SMIMEKey)
if err != nil {
fmt.Printf("can't encrypt: %v", err)
}
msg := "To: " + strings.Join(to, ",") + "\r\n" +
"From: " + from + "\r\n" +
"Subject: " + subject + "\r\n" +
strings.ReplaceAll(string(signed), "Content-Type: text/plain", "Content-Type: text/plain; charset=UTF-8")
// todo: Charset
c, err := smtp.Dial(addr)
if err != nil {
return err
}
defer c.Close()
if err = c.Mail(r.Replace(from)); err != nil {
return err
}
for i := range to {
to[i] = r.Replace(to[i])
if err = c.Rcpt(to[i]); err != nil {
return err
}
}
w, err := c.Data()
if err != nil {
return err
}
_, err = w.Write([]byte(msg))
if err != nil {
return err
}
err = w.Close()
if err != nil {
return err
}
return c.Quit()
}
func openssl(stdin []byte, args ...string) ([]byte, error) {
cmd := exec.Command("openssl", args...)
in := bytes.NewReader(stdin)
out := &bytes.Buffer{}
errs := &bytes.Buffer{}
cmd.Stdin, cmd.Stdout, cmd.Stderr = in, out, errs
if err := cmd.Run(); err != nil {
if len(errs.Bytes()) > 0 {
return nil, fmt.Errorf("error running %s (%s):\n %v", cmd.Args, err, errs.String())
}
return nil, err
}
return out.Bytes(), nil
}