forked from alash3al/smtp2http
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
47 lines (40 loc) · 1.26 KB
/
main.go
File metadata and controls
47 lines (40 loc) · 1.26 KB
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 (
"errors"
"fmt"
"log"
"time"
"github.com/alash3al/go-smtpsrv"
"github.com/go-resty/resty/v2"
)
func main() {
cfg := smtpsrv.ServerConfig{
ReadTimeout: time.Duration(*flagReadTimeout) * time.Second,
WriteTimeout: time.Duration(*flagWriteTimeout) * time.Second,
ListenAddr: *flagListenAddr,
MaxMessageBytes: int(*flagMaxMessageSize),
BannerDomain: *flagServerName,
Handler: smtpsrv.HandlerFunc(func(c *smtpsrv.Context) error {
msg, err := c.Parse()
if err != nil {
return errors.New("Cannot read your message: " + err.Error())
}
jsonDiscord := DiscordJson{
AvatarURL: *flagAvatarURL,
Username: *flagAuthor,
Content: msg.TextBody,
}
resp, err := resty.New().R().SetHeader("Content-Type", "application/json").SetBody(jsonDiscord).Post(*flagWebhook)
fmt.Println(resp.StatusCode())
if err != nil {
log.Println(err)
return errors.New("E1: Cannot accept your message due to internal error, please report that to our engineers")
} else if resp.StatusCode() != 204 {
log.Println(resp.Status())
return errors.New("E2: Cannot accept your message due to internal error, please report that to our engineers")
}
return nil
}),
}
fmt.Println(smtpsrv.ListenAndServe(&cfg))
}