1
1
package main
2
2
3
3
import (
4
+ "encoding/base64"
4
5
"errors"
5
6
"fmt"
6
- "strconv "
7
- "strings "
7
+ "io/ioutil "
8
+ "log "
8
9
9
10
"github.com/alash3al/go-smtpsrv/v3"
10
11
"github.com/go-resty/resty/v2"
@@ -23,44 +24,60 @@ func main() {
23
24
24
25
spfResult , _ , _ := c .SPF ()
25
26
26
- req := resty .New ().R ()
27
+ jsonData := EmailMessage {
28
+ ID : msg .MessageID ,
29
+ Date : msg .Date .String (),
30
+ References : msg .References ,
31
+ SPFResult : spfResult .String (),
32
+ ResentDate : msg .ResentDate .String (),
33
+ ResentID : msg .ResentMessageID ,
34
+ Subject : msg .Subject ,
35
+ Attachments : []* EmailAttachment {},
36
+ EmbeddedFiles : []* EmailEmbeddedFile {},
37
+ }
38
+
39
+ jsonData .Body .HTML = string (msg .HTMLBody )
40
+ jsonData .Body .Text = string (msg .TextBody )
41
+
42
+ jsonData .Addresses .From = transformStdAddressToEmailAddress (msg .From )[0 ]
43
+ jsonData .Addresses .To = transformStdAddressToEmailAddress (msg .To )
44
+ jsonData .Addresses .Cc = transformStdAddressToEmailAddress (msg .Cc )
45
+ jsonData .Addresses .Bcc = transformStdAddressToEmailAddress (msg .Bcc )
46
+ jsonData .Addresses .ReplyTo = transformStdAddressToEmailAddress (msg .ReplyTo )
47
+ jsonData .Addresses .InReplyTo = msg .InReplyTo
27
48
28
- formData := map [string ]string {
29
- "id" : msg .MessageID ,
30
- "date" : msg .Date .String (),
31
- "subject" : msg .Subject ,
32
- "body[text]" : string (msg .TextBody ),
33
- "body[html]" : string (msg .HTMLBody ),
34
- "addresses[from]" : c .From ().Address ,
35
- "addresses[to]" : strings .Join (extractEmails (msg .To ), "," ),
36
- "addresses[reply-to]" : strings .Join (extractEmails (msg .ReplyTo ), "," ),
37
- "addresses[resent-to]" : strings .Join (extractEmails (msg .ResentTo ), "," ),
38
- "addresses[resent-cc]" : strings .Join (extractEmails (msg .ResentCc ), "," ),
39
- "addresses[resent-bcc]" : strings .Join (extractEmails (msg .ResentBcc ), "," ),
40
- "addresses[resent-from]" : strings .Join (extractEmails (msg .ResentFrom ), "," ),
41
- "addresses[in-reply-to]" : strings .Join (msg .InReplyTo , "," ),
42
- "addresses[cc]" : strings .Join (extractEmails (msg .Cc ), "," ),
43
- "addresses[bcc]" : strings .Join (extractEmails (msg .Bcc ), "," ),
44
- "resent-date" : msg .ResentDate .String (),
45
- "resent-id" : msg .ResentMessageID ,
46
- "references" : strings .Join (msg .References , "m" ),
47
- "spf_result" : strings .ToLower (spfResult .String ()),
49
+ if resentFrom := transformStdAddressToEmailAddress (msg .ResentFrom ); len (resentFrom ) > 0 {
50
+ jsonData .Addresses .ResentFrom = resentFrom [0 ]
48
51
}
49
52
50
- // set the url-encoded-data
51
- req .SetFormData (formData )
53
+ jsonData .Addresses .ResentTo = transformStdAddressToEmailAddress (msg .ResentTo )
54
+ jsonData .Addresses .ResentCc = transformStdAddressToEmailAddress (msg .ResentCc )
55
+ jsonData .Addresses .ResentBcc = transformStdAddressToEmailAddress (msg .ResentBcc )
56
+
57
+ for _ , a := range msg .Attachments {
58
+ data , _ := ioutil .ReadAll (a .Data )
59
+ jsonData .Attachments = append (jsonData .Attachments , & EmailAttachment {
60
+ Filename : a .Filename ,
61
+ ContentType : a .ContentType ,
62
+ Data : base64 .StdEncoding .EncodeToString (data ),
63
+ })
64
+ }
52
65
53
- // set the files "attachments"
54
- for i , file := range msg .Attachments {
55
- iStr := strconv .Itoa (i )
56
- req .SetFileReader ("file[" + iStr + "]" , file .Filename , (file .Data ))
66
+ for _ , a := range msg .EmbeddedFiles {
67
+ data , _ := ioutil .ReadAll (a .Data )
68
+ jsonData .EmbeddedFiles = append (jsonData .EmbeddedFiles , & EmailEmbeddedFile {
69
+ CID : a .CID ,
70
+ ContentType : a .ContentType ,
71
+ Data : base64 .StdEncoding .EncodeToString (data ),
72
+ })
57
73
}
58
74
59
- // submit the form
60
- resp , err := req .Post (* flagWebhook )
75
+ resp , err := resty .New ().R ().SetHeader ("Content-Type" , "application/json" ).SetBody (jsonData ).Post (* flagWebhook )
61
76
if err != nil {
77
+ log .Println (err )
62
78
return errors .New ("E1: Cannot accept your message due to internal error, please report that to our engineers" )
63
79
} else if resp .StatusCode () != 200 {
80
+ log .Println (resp .Status ())
64
81
return errors .New ("E2: Cannot accept your message due to internal error, please report that to our engineers" )
65
82
}
66
83
0 commit comments