11package main
22
33import (
4+ "encoding/base64"
45 "errors"
56 "fmt"
6- "strconv "
7- "strings "
7+ "io/ioutil "
8+ "log "
89
910 "github.com/alash3al/go-smtpsrv/v3"
1011 "github.com/go-resty/resty/v2"
@@ -23,44 +24,60 @@ func main() {
2324
2425 spfResult , _ , _ := c .SPF ()
2526
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
2748
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 ]
4851 }
4952
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+ }
5265
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+ })
5773 }
5874
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 )
6176 if err != nil {
77+ log .Println (err )
6278 return errors .New ("E1: Cannot accept your message due to internal error, please report that to our engineers" )
6379 } else if resp .StatusCode () != 200 {
80+ log .Println (resp .Status ())
6481 return errors .New ("E2: Cannot accept your message due to internal error, please report that to our engineers" )
6582 }
6683
0 commit comments