-
Notifications
You must be signed in to change notification settings - Fork 0
/
session.go
68 lines (53 loc) · 1.34 KB
/
session.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
package meilo
import (
"errors"
"io"
"log"
"github.com/emersion/go-sasl"
"github.com/emersion/go-smtp"
)
var e = email{}
// A Session is returned after successful login.
type session struct {
username string
password string
}
// AuthMechanisms returns a slice of available auth mechanisms; only PLAIN is supported.
func (s *session) AuthMechanisms() []string {
return []string{sasl.Plain}
}
// Auth is the handler for supported authenticators.
func (s *session) Auth(mech string) (sasl.Server, error) {
return sasl.NewPlainServer(func(identity, username, password string) error {
if username != s.username || password != s.password {
return errors.New("invalid username or password")
}
return nil
}), nil
}
func (s *session) Mail(from string, opts *smtp.MailOptions) error {
e.From = from
return nil
}
func (s *session) Rcpt(to string, opts *smtp.RcptOptions) error {
e.To = append(e.To, to)
return nil
}
func (s *session) Data(r io.Reader) error {
_, err := io.Copy(&e.Body, r)
if err != nil {
return err
}
return nil
}
func (s *session) Logout() error { return nil }
func (s *session) Reset() {
if err := e.Parse(); err != nil {
log.Printf("meilo: failed to parse email: %v", err)
}
log.Println("Sending email...")
if err := send(e); err != nil {
log.Printf("meilo: failed to send email: %v", err)
}
e.Reset()
}