forked from alash3al/smtp2http
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
145 lines (121 loc) · 4.57 KB
/
main.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
package main
import (
"encoding/base64"
"errors"
"fmt"
"io/ioutil"
"log"
"net/mail"
"strings"
"golang.org/x/net/html/charset"
"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())
}
spfResult, _, _ := c.SPF()
// Initialize EmailMessage struct
jsonData := EmailMessage{
ID: msg.MessageID,
Date: msg.Date.String(),
References: msg.References,
SPFResult: spfResult.String(),
ResentDate: msg.ResentDate.String(),
ResentID: msg.ResentMessageID,
Subject: msg.Subject,
Attachments: []*EmailAttachment{},
EmbeddedFiles: []*EmailEmbeddedFile{},
}
// Decode email body content
jsonData.Body.HTML, jsonData.Body.Text = decodeCharset(msg.HTMLBody, msg.TextBody)
// Address handling
jsonData.Addresses.From = transformStdAddressToEmailAddress([]*mail.Address{c.From()})[0]
jsonData.Addresses.To = transformStdAddressToEmailAddress([]*mail.Address{c.To()})[0]
toSplited := strings.Split(jsonData.Addresses.To.Address, "@")
if len(*flagDomain) > 0 && (len(toSplited) < 2 || toSplited[1] != *flagDomain) {
log.Println("domain not allowed")
log.Println(*flagDomain)
return errors.New("Unauthorized TO domain")
}
jsonData.Addresses.Cc = transformStdAddressToEmailAddress(msg.Cc)
jsonData.Addresses.Bcc = transformStdAddressToEmailAddress(msg.Bcc)
jsonData.Addresses.ReplyTo = transformStdAddressToEmailAddress(msg.ReplyTo)
jsonData.Addresses.InReplyTo = msg.InReplyTo
if resentFrom := transformStdAddressToEmailAddress(msg.ResentFrom); len(resentFrom) > 0 {
jsonData.Addresses.ResentFrom = resentFrom[0]
}
jsonData.Addresses.ResentTo = transformStdAddressToEmailAddress(msg.ResentTo)
jsonData.Addresses.ResentCc = transformStdAddressToEmailAddress(msg.ResentCc)
jsonData.Addresses.ResentBcc = transformStdAddressToEmailAddress(msg.ResentBcc)
for _, a := range msg.Attachments {
data, _ := ioutil.ReadAll(a.Data)
jsonData.Attachments = append(jsonData.Attachments, &EmailAttachment{
Filename: a.Filename,
ContentType: a.ContentType,
Data: base64.StdEncoding.EncodeToString(data),
})
}
for _, a := range msg.EmbeddedFiles {
data, _ := ioutil.ReadAll(a.Data)
jsonData.EmbeddedFiles = append(jsonData.EmbeddedFiles, &EmailEmbeddedFile{
CID: a.CID,
ContentType: a.ContentType,
Data: base64.StdEncoding.EncodeToString(data),
})
}
resp, err := resty.New().R().SetHeader("Content-Type", "application/json").SetBody(jsonData).Post(*flagWebhook)
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() != 200 {
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))
}
// decodeCharset decodes the email body from its charset
func decodeCharset(htmlBody, textBody string) (string, string) {
htmlBodyDecoded, textBodyDecoded := htmlBody, textBody
// Handle HTML body charset conversion if needed
htmlCharset := "utf-8" // Default to UTF-8; adjust if needed
decodedHTMLBody, err := decodeCharsetFromString(htmlBody, htmlCharset)
if err == nil {
htmlBodyDecoded = decodedHTMLBody
}
// Handle Text body charset conversion if needed
textCharset := "utf-8" // Default to UTF-8; adjust if needed
decodedTextBody, err := decodeCharsetFromString(textBody, textCharset)
if err == nil {
textBodyDecoded = decodedTextBody
}
return htmlBodyDecoded, textBodyDecoded
}
// decodeCharsetFromString decodes a string from a given charset
func decodeCharsetFromString(body, charset string) (string, error) {
decodedBody := body
// Create a reader that decodes the charset
reader, err := charset.NewReader(strings.NewReader(body), charset)
if err != nil {
return "", err
}
// Read all content from the reader
bytes, err := ioutil.ReadAll(reader)
if err != nil {
return "", err
}
decodedBody = string(bytes)
return decodedBody, nil
}