diff --git a/dist/imap-json-fetcher b/dist/imap-json-fetcher index 98398cb..337c3a9 100755 Binary files a/dist/imap-json-fetcher and b/dist/imap-json-fetcher differ diff --git a/dist/imap-json-fetcher_darwin_amd64 b/dist/imap-json-fetcher_darwin_amd64 index 3078993..5e962ee 100755 Binary files a/dist/imap-json-fetcher_darwin_amd64 and b/dist/imap-json-fetcher_darwin_amd64 differ diff --git a/dist/imap-json-fetcher_darwin_arm64 b/dist/imap-json-fetcher_darwin_arm64 index d321ede..c161b7e 100755 Binary files a/dist/imap-json-fetcher_darwin_arm64 and b/dist/imap-json-fetcher_darwin_arm64 differ diff --git a/dist/imap-json-fetcher_linux_arm b/dist/imap-json-fetcher_linux_arm index 8ca4603..c5ffdba 100755 Binary files a/dist/imap-json-fetcher_linux_arm and b/dist/imap-json-fetcher_linux_arm differ diff --git a/dist/imap-json-fetcher_linux_arm64 b/dist/imap-json-fetcher_linux_arm64 index 468fbe8..2460bdc 100755 Binary files a/dist/imap-json-fetcher_linux_arm64 and b/dist/imap-json-fetcher_linux_arm64 differ diff --git a/dist/imap-json-fetcher_linux_arm64_static b/dist/imap-json-fetcher_linux_arm64_static index 9ad6af6..d833b9e 100755 Binary files a/dist/imap-json-fetcher_linux_arm64_static and b/dist/imap-json-fetcher_linux_arm64_static differ diff --git a/dist/imap-json-fetcher_linux_arm_static b/dist/imap-json-fetcher_linux_arm_static index b589e3b..6e7751b 100755 Binary files a/dist/imap-json-fetcher_linux_arm_static and b/dist/imap-json-fetcher_linux_arm_static differ diff --git a/dist/imap-json-fetcher_static b/dist/imap-json-fetcher_static index 98531bc..5098f62 100755 Binary files a/dist/imap-json-fetcher_static and b/dist/imap-json-fetcher_static differ diff --git a/email-20240504-084413-0.json b/email-20240504-084413-0.json new file mode 100644 index 0000000..ad53da7 --- /dev/null +++ b/email-20240504-084413-0.json @@ -0,0 +1,21 @@ +{ + "body": "test7\r\n", + "date": "2024-05-04T12:43:34Z", + "from": [ + { + "PersonalName": "", + "AtDomainList": "", + "MailboxName": "imapfetcher", + "HostName": "nixc.us" + } + ], + "subject": "test7", + "to": [ + { + "PersonalName": "", + "AtDomainList": "", + "MailboxName": "imapfetcher", + "HostName": "nixc.us" + } + ] +} \ No newline at end of file diff --git a/email-20240504-084413-1.json b/email-20240504-084413-1.json new file mode 100644 index 0000000..e9e45e5 --- /dev/null +++ b/email-20240504-084413-1.json @@ -0,0 +1,21 @@ +{ + "body": "test8\r\n", + "date": "2024-05-04T12:43:53Z", + "from": [ + { + "PersonalName": "", + "AtDomainList": "", + "MailboxName": "imapfetcher", + "HostName": "nixc.us" + } + ], + "subject": "test8", + "to": [ + { + "PersonalName": "", + "AtDomainList": "", + "MailboxName": "imapfetcher", + "HostName": "nixc.us" + } + ] +} \ No newline at end of file diff --git a/go.mod b/go.mod index 743bbb0..8e006f8 100644 --- a/go.mod +++ b/go.mod @@ -2,9 +2,13 @@ module imap-json-fetcher go 1.21.1 -require github.com/emersion/go-imap v1.2.1 +require ( + github.com/emersion/go-imap v1.2.1 + github.com/emersion/go-message v0.15.0 +) require ( github.com/emersion/go-sasl v0.0.0-20200509203442-7bfe0ed36a21 // indirect + github.com/emersion/go-textwrapper v0.0.0-20200911093747-65d896831594 // indirect golang.org/x/text v0.3.7 // indirect ) diff --git a/go.sum b/go.sum index 827662c..583c916 100644 --- a/go.sum +++ b/go.sum @@ -1,8 +1,10 @@ github.com/emersion/go-imap v1.2.1 h1:+s9ZjMEjOB8NzZMVTM3cCenz2JrQIGGo5j1df19WjTA= github.com/emersion/go-imap v1.2.1/go.mod h1:Qlx1FSx2FTxjnjWpIlVNEuX+ylerZQNFE5NsmKFSejY= +github.com/emersion/go-message v0.15.0 h1:urgKGqt2JAc9NFJcgncQcohHdiYb803YTH9OQwHBHIY= github.com/emersion/go-message v0.15.0/go.mod h1:wQUEfE+38+7EW8p8aZ96ptg6bAb1iwdgej19uXASlE4= github.com/emersion/go-sasl v0.0.0-20200509203442-7bfe0ed36a21 h1:OJyUGMJTzHTd1XQp98QTaHernxMYzRaOasRir9hUlFQ= github.com/emersion/go-sasl v0.0.0-20200509203442-7bfe0ed36a21/go.mod h1:iL2twTeMvZnrg54ZoPDNfJaJaqy0xIQFuBdrLsmspwQ= +github.com/emersion/go-textwrapper v0.0.0-20200911093747-65d896831594 h1:IbFBtwoTQyw0fIM5xv1HF+Y+3ZijDR839WMulgxCcUY= github.com/emersion/go-textwrapper v0.0.0-20200911093747-65d896831594/go.mod h1:aqO8z8wPrjkscevZJFVE1wXJrLpC5LtJG7fqLOsPb2U= golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk= diff --git a/main.go b/main.go index 5fbb153..a5329d9 100644 --- a/main.go +++ b/main.go @@ -3,9 +3,10 @@ package main import ( "encoding/json" "fmt" - "io/ioutil" + "io" "log" "os" + "strings" "time" "github.com/emersion/go-imap" @@ -68,20 +69,43 @@ func main() { } }() + counter := 0 for msg := range messages { if msg == nil { continue } - r := mail.NewReader(msg.GetBody(section)) - email, err := r.NextPart() - if err != nil { - log.Fatalf("Failed to read message body: %v", err) + body := msg.GetBody(section) + if body == nil { + log.Println("No body fetched for this message") + continue } - body, err := ioutil.ReadAll(email) + r, err := mail.CreateReader(body) if err != nil { - log.Fatalf("Failed to read body content: %v", err) + log.Fatalf("Failed to create mail reader: %v", err) + } + + var emailBody strings.Builder + for { + p, err := r.NextPart() + if err == io.EOF { + break + } + if err != nil { + log.Fatalf("Failed to read part: %v", err) + } + + switch h := p.Header.(type) { + case *mail.InlineHeader: + _, params, _ := h.ContentType() + if params["charset"] != "" { + _, err = io.Copy(&emailBody, p.Body) + if err != nil { + log.Fatalf("Failed to read body: %v", err) + } + } + } } emailData := map[string]interface{}{ @@ -89,7 +113,7 @@ func main() { "from": msg.Envelope.From, "to": msg.Envelope.To, "date": msg.Envelope.Date, - "body": string(body), + "body": emailBody.String(), } emailJSON, err := json.MarshalIndent(emailData, "", " ") @@ -98,7 +122,8 @@ func main() { } timestamp := time.Now().Format("20060102-150405") - fileName := fmt.Sprintf("email-%s.json", timestamp) + fileName := fmt.Sprintf("email-%s-%d.json", timestamp, counter) + counter++ err = os.WriteFile(fileName, emailJSON, 0644) if err != nil { log.Fatalf("Failed to write file %s: %v", fileName, err)