-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmail_notif.go
47 lines (41 loc) · 1.24 KB
/
mail_notif.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
package main
import (
"fmt"
"net"
"net/smtp"
)
type mailNotificator struct {
To string
From string
Server string
Login string
Password string
MinAmount uint64
}
var _ notificator = (*mailNotificator)(nil)
func NewMailNotificator(cfg notificatorConfig) *mailNotificator {
return &mailNotificator{To: cfg.Params["Target"], From: cfg.Params["From"],
Server: cfg.Params["SmtpServer"], Login: cfg.Params["Login"],
Password: cfg.Params["Password"], MinAmount: cfg.MinAmount}
}
func (m *mailNotificator) Notify(amount uint64, comment string) (err error) {
if amount < m.MinAmount {
return fmt.Errorf("amount is too small, required %d got %d", m.MinAmount, amount)
}
var host string
host, _, err = net.SplitHostPort(m.Server)
if err != nil {
return
}
auth := smtp.PlainAuth("", m.Login, m.Password, host)
if comment != "" {
comment = fmt.Sprintf("Sender said: \"%s\"", comment)
}
body := fmt.Sprintf("From: %s\nSubject: %s\n\nYou've received %d sats to your lightning address. %s", m.From,
"New lightning address payment", amount, comment)
err = smtp.SendMail(m.Server, auth, m.From, []string{m.To}, []byte(body))
return
}
func (m *mailNotificator) Target() string {
return fmt.Sprintf("%s => %s", m.From, m.To)
}