-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmtp.go
122 lines (102 loc) · 2.29 KB
/
smtp.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
121
122
package main
import (
"io"
"log"
"strings"
"time"
"github.com/emersion/go-smtp"
)
var ValidUsernames = []string{"accept", "softfail", "hardfail", "later"}
type Backend struct{}
type Session struct {
MailFrom string
Username string
}
func (bkd *Backend) NewSession(c *smtp.Conn) (smtp.Session, error) {
return &Session{}, nil
}
func (s *Session) AuthPlain(username, password string) error {
return nil
}
func (s *Session) Mail(from string, opts *smtp.MailOptions) error {
log.Println("smtp: mail from:", from)
s.MailFrom = from
return nil
}
func (s *Session) Rcpt(to string, opts *smtp.RcptOptions) error {
log.Println("smtp: rcpt to:", to)
username := getUsername(to)
if !contains(ValidUsernames, username) {
log.Println("smtp: 550 invalid recipient address")
return &smtp.SMTPError{
Code: 550,
Message: "Invalid recipient address",
}
}
s.Username = getUsername(to)
return nil
}
func (s *Session) Data(r io.Reader) error {
if b, err := io.ReadAll(r); err != nil {
return err
} else {
log.Println("smtp: data:", string(b))
}
switch s.Username {
case "accept":
log.Println("smtp: accepting message")
return nil
case "softfail":
log.Println("smtp: 450 mailbox is available")
return &smtp.SMTPError{
Code: 450,
Message: "Mailbox is unavailable at the moment",
}
case "later":
log.Println("smtp: 450 try again later")
return &smtp.SMTPError{
Code: 450,
Message: "Try again in 250 seconds",
}
default:
log.Println("smtp: 550 invalid recipient address")
return &smtp.SMTPError{
Code: 550,
Message: "Invalid recipient address",
}
}
}
func (s *Session) Reset() {}
func (s *Session) Logout() error {
return nil
}
func runSMTPServer() {
be := &Backend{}
s := smtp.NewServer(be)
s.Addr = ":2525"
s.Domain = "blackhole"
s.WriteTimeout = 10 * time.Second
s.ReadTimeout = 10 * time.Second
s.MaxMessageBytes = 1024 * 1024
s.MaxRecipients = 50
s.AllowInsecureAuth = true
log.Println("smtp: starting smtp server at", s.Addr)
if err := s.ListenAndServe(); err != nil {
log.Fatal(err)
}
}
func getUsername(s string) string {
parts := strings.Split(s, "@")
if len(parts) > 0 {
return parts[0]
}
return ""
}
func contains(arr []string, str string) bool {
for _, a := range arr {
if a == str {
return true
}
}
return false
}