Skip to content

Commit

Permalink
moved from multipart-formdata to json, as well any attachment as base64
Browse files Browse the repository at this point in the history
  • Loading branch information
alash3al committed Jun 7, 2020
1 parent 15c54ed commit ce19c7d
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 31 deletions.
15 changes: 15 additions & 0 deletions helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,18 @@ func extractEmails(addr []*mail.Address, _ ...error) []string {

return ret
}

func transformStdAddressToEmailAddress(addr []*mail.Address) []*EmailAddress {
ret := []*EmailAddress{}

for _, e := range addr {
ret = append(ret, &EmailAddress{
Address: e.Address,
Name: e.Name,
})
}

return ret
}

// func smtpsrvMesssage2EmailMessage(msg *smtpsrv.Context)
79 changes: 48 additions & 31 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package main

import (
"encoding/base64"
"errors"
"fmt"
"strconv"
"strings"
"io/ioutil"
"log"

"github.com/alash3al/go-smtpsrv/v3"
"github.com/go-resty/resty/v2"
Expand All @@ -23,44 +24,60 @@ func main() {

spfResult, _, _ := c.SPF()

req := resty.New().R()
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{},
}

jsonData.Body.HTML = string(msg.HTMLBody)
jsonData.Body.Text = string(msg.TextBody)

jsonData.Addresses.From = transformStdAddressToEmailAddress(msg.From)[0]
jsonData.Addresses.To = transformStdAddressToEmailAddress(msg.To)
jsonData.Addresses.Cc = transformStdAddressToEmailAddress(msg.Cc)
jsonData.Addresses.Bcc = transformStdAddressToEmailAddress(msg.Bcc)
jsonData.Addresses.ReplyTo = transformStdAddressToEmailAddress(msg.ReplyTo)
jsonData.Addresses.InReplyTo = msg.InReplyTo

formData := map[string]string{
"id": msg.MessageID,
"date": msg.Date.String(),
"subject": msg.Subject,
"body[text]": string(msg.TextBody),
"body[html]": string(msg.HTMLBody),
"addresses[from]": c.From().Address,
"addresses[to]": strings.Join(extractEmails(msg.To), ","),
"addresses[reply-to]": strings.Join(extractEmails(msg.ReplyTo), ","),
"addresses[resent-to]": strings.Join(extractEmails(msg.ResentTo), ","),
"addresses[resent-cc]": strings.Join(extractEmails(msg.ResentCc), ","),
"addresses[resent-bcc]": strings.Join(extractEmails(msg.ResentBcc), ","),
"addresses[resent-from]": strings.Join(extractEmails(msg.ResentFrom), ","),
"addresses[in-reply-to]": strings.Join(msg.InReplyTo, ","),
"addresses[cc]": strings.Join(extractEmails(msg.Cc), ","),
"addresses[bcc]": strings.Join(extractEmails(msg.Bcc), ","),
"resent-date": msg.ResentDate.String(),
"resent-id": msg.ResentMessageID,
"references": strings.Join(msg.References, "m"),
"spf_result": strings.ToLower(spfResult.String()),
if resentFrom := transformStdAddressToEmailAddress(msg.ResentFrom); len(resentFrom) > 0 {
jsonData.Addresses.ResentFrom = resentFrom[0]
}

// set the url-encoded-data
req.SetFormData(formData)
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),
})
}

// set the files "attachments"
for i, file := range msg.Attachments {
iStr := strconv.Itoa(i)
req.SetFileReader("file["+iStr+"]", file.Filename, (file.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),
})
}

// submit the form
resp, err := req.Post(*flagWebhook)
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")
}

Expand Down
56 changes: 56 additions & 0 deletions message.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package main

// EmailAddress ...
type EmailAddress struct {
Name string `json:"name,omitempty"`
Address string `json:"address,omitempty"`
}

// EmailAttachment ...
type EmailAttachment struct {
Filename string `json:"filename"`
ContentType string `json:"content_type"`
Data string `json:"data"`
}

// EmailEmbeddedFile ...
type EmailEmbeddedFile struct {
CID string `json:"cid"`
ContentType string `json:"content_type"`
Data string `json:"data"`
}

// EmailMessage ...
type EmailMessage struct {
References []string `json:"references,omitempty"`
SPFResult string `json:"spf,omitempty"`

ID string `json:"id,omitempty"`
Date string `json:"date,omitempty"`
Subject string `json:"subject,omitempty"`

ResentDate string `json:"resent_date,omitempty"`
ResentID string `json:"resent_id,omitempty"`

Body struct {
Text string `json:"text,omitempty"`
HTML string `json:"html,omitempty"`
} `json:"body"`

Addresses struct {
From *EmailAddress `json:"from"`
To []*EmailAddress `json:"to"`
ReplyTo []*EmailAddress `json:"reply_to,omitempty"`
Cc []*EmailAddress `json:"cc,omitempty"`
Bcc []*EmailAddress `json:"bcc,omitempty"`
InReplyTo []string `json:"in_reply_to,omitempty"`

ResentFrom *EmailAddress `json:"resent_from,omitempty"`
ResentTo []*EmailAddress `json:"resent_to,omitempty"`
ResentCc []*EmailAddress `json:"resent_cc,omitempty"`
ResentBcc []*EmailAddress `json:"resent_bcc,omitempty"`
} `json:"addresses"`

Attachments []*EmailAttachment `json:"attachments,omitempty"`
EmbeddedFiles []*EmailEmbeddedFile `json:"embedded_files,omitempty"`
}

0 comments on commit ce19c7d

Please sign in to comment.