forked from cds-snc/smtp-proxy-for-notify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
165 lines (141 loc) · 3.87 KB
/
server.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package main
import (
"crypto/tls"
b64 "encoding/base64"
"errors"
"fmt"
"io"
"strings"
"time"
"github.com/DusanKasan/parsemail"
"github.com/emersion/go-smtp"
"github.com/rs/zerolog/log"
)
type Backend struct {
Config *Config
}
func (bkd *Backend) NewSession(c *smtp.Conn) (smtp.Session, error) {
return &Session{
Authenticated: false,
Config: bkd.Config,
Email: &NotifyEmail{
TemplateId: bkd.Config.Notify.TemplateId,
},
}, nil
}
type Session struct {
Authenticated bool
Config *Config
Email *NotifyEmail
}
func (s *Session) AuthPlain(username, password string) error {
if username != s.Config.Smtp.Username || password != s.Config.Smtp.Password {
log.Error().Msgf("Invalid username or password: %s", username)
s.Authenticated = false
s.Logout()
return errors.New("invalid username or password")
}
log.Info().Msgf("User %s logged in", username)
s.Authenticated = true
return nil
}
func (s *Session) Mail(from string, opts *smtp.MailOptions) error {
if !s.Authenticated {
s.Logout()
return errors.New("not authenticated")
}
log.Info().Msgf("Mail from: %s", from)
return nil
}
func (s *Session) Rcpt(to string, opts *smtp.RcptOptions) error {
if !s.Authenticated {
s.Logout()
return errors.New("not authenticated")
}
log.Info().Msgf("Rcpt to: %s", to)
s.Email.Emails = append(s.Email.Emails, to)
return nil
}
func (s *Session) Data(r io.Reader) error {
if !s.Authenticated {
return errors.New("not authenticated")
}
if data, err := io.ReadAll(r); err != nil {
return err
} else {
// Parse the email
email, err := parsemail.Parse(strings.NewReader(string(data)))
if err != nil {
log.Error().Msgf("Error parsing email: %s", err)
return err
}
// Add cc emails
for _, address := range email.Cc {
s.Email.Emails = append(s.Email.Emails, address.Address)
}
// Add bcc emails
for _, address := range email.Bcc {
s.Email.Emails = append(s.Email.Emails, address.Address)
}
s.Email.Personalisation.Subject = email.Subject
s.Email.Personalisation.Body = email.TextBody
// Add attachments
for _, attachment := range email.Attachments {
attachment_data, err := io.ReadAll(attachment.Data)
if err != nil {
return err
}
s.Email.Attachments = append(s.Email.Attachments, Attachment{
File: b64.StdEncoding.EncodeToString(attachment_data),
Filename: attachment.Filename,
SendingMethod: "attach",
})
}
client := newNotifyClient(s.Config.Notify.ApiKey, s.Config.Notify.Hostname)
if err := sendEmail(client, s.Email); err != nil {
return err
}
}
return nil
}
func (s *Session) Reset() {
s.Authenticated = false
s.Email = new(NotifyEmail)
s.Email.TemplateId = s.Config.Notify.TemplateId
}
func (s *Session) Logout() error {
s.Authenticated = false
return nil
}
func startSmtpServer(config *Config) {
backend := &Backend{
Config: config,
}
s := smtp.NewServer(backend)
s.Addr = fmt.Sprintf("%s:%d", config.Smtp.Hostname, config.Smtp.Port)
s.Domain = config.Smtp.Hostname
s.WriteTimeout = 10 * time.Second
s.ReadTimeout = 10 * time.Second
s.MaxMessageBytes = 10485760
s.MaxRecipients = 10
if config.Smtp.UseTLS {
s.AllowInsecureAuth = false
s.EnableREQUIRETLS = true
cer, err := tls.LoadX509KeyPair(config.Smtp.TlsCertFile, config.Smtp.TlsKeyFile)
if err != nil {
log.Fatal().Err(err).Msg("Failed to load TLS certificate")
}
s.TLSConfig = &tls.Config{Certificates: []tls.Certificate{cer}}
log.Info().Msgf("SMTP server listening with TLS at %s", s.Addr)
if err := s.ListenAndServeTLS(); err != nil {
log.Fatal().Err(err).Msg("SMTP server failed with TLS")
}
} else {
s.AllowInsecureAuth = true
log.Warn().Msg("SMTP server listening without TLS! DO NOT USE IN PRODUCTION!")
log.Info().Msgf("SMTP server listening on %s", s.Addr)
if err := s.ListenAndServe(); err != nil {
log.Fatal().Err(err).Msg("SMTP server failed")
}
}
}