Bulk commit: November work
This commit is contained in:
161
cmd/mailingest/main.go
Normal file
161
cmd/mailingest/main.go
Normal file
@ -0,0 +1,161 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"io"
|
||||
"log"
|
||||
"log/slog"
|
||||
"os"
|
||||
|
||||
"github.com/emersion/go-imap/v2"
|
||||
"github.com/emersion/go-imap/v2/imapclient"
|
||||
"github.com/emersion/go-message/mail"
|
||||
)
|
||||
|
||||
func main() {
|
||||
logger := slog.New(slog.NewTextHandler(os.Stdout, nil))
|
||||
|
||||
// err := godotenv.Load()
|
||||
// if err != nil {
|
||||
// logger.Error("Error Loading Environment variables! ", "error", err)
|
||||
// }
|
||||
|
||||
// logger.Info("Setting up DB")
|
||||
// db := utils.SetupDatabase(os.Getenv("DB_DSN"), logger)
|
||||
// logger.Info("Done setting up DB")
|
||||
|
||||
// appCtx := utils.Application{Logger: logger, DB: db}
|
||||
|
||||
// Register to jobs.
|
||||
|
||||
// auth := smtp.PlainAuth("", "me@kocoder.xyz", "&,25,upMeddeEnTYPTifaccEptIonaAlErGiE", "mx.kocoder.xyz")
|
||||
|
||||
// logger.Info("Before sending Email")
|
||||
// err := smtp.SendMail("mx.kocoder.xyz:587", auth, "me@kocoder.xyz", []string{"oss@kocoder.xyz"}, []byte(
|
||||
// "From: Konstantin Hintermayer <me@kocoder.xyz>\r\n"+
|
||||
// "To: oss@kocoder.xyz\r\n"+
|
||||
// "Subject: discount Gophers!\r\n"+
|
||||
// "\r\n"+
|
||||
// "This is the email body lel.\r\n"))
|
||||
// if err != nil {
|
||||
// logger.Error("Unable to send EMail", "err", err)
|
||||
// } else {
|
||||
// logger.Info("Successfully sent Email")
|
||||
// }
|
||||
|
||||
c, err := imapclient.DialTLS("10.1.0.2:993", &imapclient.Options{TLSConfig: &tls.Config{ServerName: "mx.kocoder.xyz"}})
|
||||
if err != nil {
|
||||
log.Fatalf("failed to dial IMAP server: %v", err)
|
||||
}
|
||||
defer c.Close()
|
||||
|
||||
if err := c.Login("me@kocoder.xyz", "&,25,upMeddeEnTYPTifaccEptIonaAlErGiE").Wait(); err != nil {
|
||||
log.Fatalf("failed to login: %v", err)
|
||||
}
|
||||
log.Print("Connected")
|
||||
|
||||
mailboxes, err := c.List("", "%", nil).Collect()
|
||||
if err != nil {
|
||||
log.Fatalf("failed to list mailboxes: %v", err)
|
||||
}
|
||||
log.Printf("Found %v mailboxes", len(mailboxes))
|
||||
for _, mbox := range mailboxes {
|
||||
log.Printf(" - %v", mbox.Mailbox)
|
||||
}
|
||||
|
||||
selectedMbox, err := c.Select("INBOX", nil).Wait()
|
||||
if err != nil {
|
||||
log.Fatalf("failed to select INBOX: %v", err)
|
||||
}
|
||||
log.Printf("INBOX contains %v messages", selectedMbox.NumMessages)
|
||||
|
||||
i := 0
|
||||
|
||||
for {
|
||||
// Send a FETCH command to fetch the message body
|
||||
seqSet := imap.SeqSetNum(uint32(i))
|
||||
|
||||
bodySection := &imap.FetchItemBodySection{}
|
||||
fetchOptions := &imap.FetchOptions{
|
||||
BodySection: []*imap.FetchItemBodySection{bodySection},
|
||||
}
|
||||
fetchCmd := c.Fetch(seqSet, fetchOptions)
|
||||
defer fetchCmd.Close()
|
||||
|
||||
msg := fetchCmd.Next()
|
||||
for msg != nil {
|
||||
// Find the body section in the response
|
||||
var bodySectionData imapclient.FetchItemDataBodySection
|
||||
ok := false
|
||||
for {
|
||||
item := msg.Next()
|
||||
if item == nil {
|
||||
break
|
||||
}
|
||||
bodySectionData, ok = item.(imapclient.FetchItemDataBodySection)
|
||||
if ok {
|
||||
break
|
||||
}
|
||||
}
|
||||
if !ok {
|
||||
log.Fatalf("FETCH command did not return body section")
|
||||
}
|
||||
|
||||
// Read the message via the go-message library
|
||||
mr, err := mail.CreateReader(bodySectionData.Literal)
|
||||
if err != nil {
|
||||
log.Fatalf("failed to create mail reader: %v", err)
|
||||
}
|
||||
|
||||
// Print a few header fields
|
||||
h := mr.Header
|
||||
if date, err := h.Date(); err != nil {
|
||||
log.Printf("failed to parse Date header field: %v", err)
|
||||
} else {
|
||||
log.Printf("Date: %v", date)
|
||||
}
|
||||
if to, err := h.AddressList("To"); err != nil {
|
||||
log.Printf("failed to parse To header field: %v", err)
|
||||
} else {
|
||||
log.Printf("To: %v", to)
|
||||
}
|
||||
if subject, err := h.Text("Subject"); err != nil {
|
||||
log.Printf("failed to parse Subject header field: %v", err)
|
||||
} else {
|
||||
log.Printf("Subject: %v", subject)
|
||||
}
|
||||
|
||||
if err := fetchCmd.Close(); err != nil {
|
||||
logger.Error("FETCH command failed", "err", err)
|
||||
}
|
||||
|
||||
// Process the message's parts
|
||||
for {
|
||||
p, err := mr.NextPart()
|
||||
if err == io.EOF {
|
||||
break
|
||||
} else if err != nil {
|
||||
log.Fatalf("failed to read message part: %v", err)
|
||||
}
|
||||
|
||||
switch h := p.Header.(type) {
|
||||
case *mail.InlineHeader:
|
||||
// This is the message's text (can be plain-text or HTML)
|
||||
b, _ := io.ReadAll(p.Body)
|
||||
log.Printf("Inline text: %v", string(b))
|
||||
case *mail.AttachmentHeader:
|
||||
// This is an attachment
|
||||
filename, _ := h.Filename()
|
||||
log.Printf("Attachment: %v", filename)
|
||||
}
|
||||
}
|
||||
|
||||
msg = fetchCmd.Next()
|
||||
}
|
||||
|
||||
i++
|
||||
if i > 10 {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user