cleaned up an edge.

This commit is contained in:
Colin 2024-05-04 08:44:47 -04:00
parent 19e22df93f
commit 31c56a3030
13 changed files with 83 additions and 10 deletions

BIN
dist/imap-json-fetcher vendored

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -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"
}
]
}

View File

@ -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"
}
]
}

6
go.mod
View File

@ -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
)

2
go.sum
View File

@ -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=

43
main.go
View File

@ -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)