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
|
package smtp
import (
"dove/config"
"dove/messages"
"dove/utils/logger"
"fmt"
"time"
gosmtp "github.com/emersion/go-smtp"
)
var activeServers []serverInstance
func Start() {
plainAddress := fmt.Sprintf("%s:%d", config.SMTP.Host, config.SMTP.Port)
plainServer := createServer(plainAddress)
activeServers = append(activeServers, serverInstance{server: plainServer, label: LOG_PREFIX})
go startListener(plainServer, LOG_PREFIX, plainAddress)
}
func Shutdown() {
for _, instance := range activeServers {
if shutdownError := instance.server.Close(); shutdownError != nil {
logger.Errorf(LOG_PREFIX, messages.SMTPShutdownFailed, instance.label, shutdownError)
}
}
logger.Infof(LOG_PREFIX, messages.SMTPShutdownComplete)
}
func createServer(address string) *gosmtp.Server {
smtpServer := gosmtp.NewServer(gosmtp.BackendFunc(func(connection *gosmtp.Conn) (gosmtp.Session, error) {
logger.Debugf(LOG_PREFIX, messages.SMTPSessionStarted, connection.Hostname())
return &session{}, nil
}))
smtpServer.Addr = address
smtpServer.Domain = config.SMTP.Domain
smtpServer.ReadTimeout = time.Duration(config.SMTP.ReadTimeout) * time.Second
smtpServer.WriteTimeout = time.Duration(config.SMTP.WriteTimeout) * time.Second
smtpServer.MaxMessageBytes = int64(config.SMTP.MaxMessageSize)
smtpServer.AllowInsecureAuth = true
return smtpServer
}
func startListener(smtpServer *gosmtp.Server, label string, address string) {
logger.Successf(LOG_PREFIX, messages.SMTPServerStarting, label, address)
if listenError := smtpServer.ListenAndServe(); listenError != nil {
logger.Fatalf(LOG_PREFIX, messages.SMTPListenFailed, label, listenError)
}
}
|