Commit: Bulk unfinished work
This commit is contained in:
84
integration/mailing/imap_integration.go
Normal file
84
integration/mailing/imap_integration.go
Normal file
@@ -0,0 +1,84 @@
|
||||
package mailing
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"git.kocoder.xyz/kocoded/vt/fx/interfaces"
|
||||
|
||||
imap "github.com/BrianLeishman/go-imap"
|
||||
)
|
||||
|
||||
type imapIntegration struct {
|
||||
id *imap.Dialer
|
||||
}
|
||||
|
||||
// Invoke implements interfaces.BasicIntegration.
|
||||
func (i *imapIntegration) Invoke() {
|
||||
fmt.Println("Invoked!")
|
||||
}
|
||||
|
||||
// OnStart implements interfaces.BasicIntegration.
|
||||
func (i *imapIntegration) OnStart(ctx context.Context) error {
|
||||
fmt.Println("Integration IMAP started")
|
||||
return nil
|
||||
}
|
||||
|
||||
// OnStop implements interfaces.BasicIntegration.
|
||||
func (i *imapIntegration) OnStop(ctx context.Context) error {
|
||||
fmt.Println("Integration IMAP stopped")
|
||||
return nil
|
||||
}
|
||||
|
||||
func NewImapIntegration() interfaces.BasicIntegration {
|
||||
fmt.Println("New Imap Integration")
|
||||
|
||||
imap.Verbose = true
|
||||
imap.RetryCount = 1
|
||||
imap.DialTimeout = 10 * time.Second
|
||||
imap.CommandTimeout = 30 * time.Second
|
||||
|
||||
m, err := imap.New("me@kocoder.xyz", "&,25,upMeddeEnTYPTifaccEptIonaAlErGiE", "mx.kocoder.xyz", 993)
|
||||
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
if err := m.SelectFolder("INBOX"); err != nil {
|
||||
panic("Failed to select inbox")
|
||||
}
|
||||
|
||||
uids, err := m.GetUIDs("UNSEEN")
|
||||
if err != nil {
|
||||
panic("Search")
|
||||
}
|
||||
|
||||
emails, err := m.GetEmails(uids[:5]...)
|
||||
if err != nil {
|
||||
panic("Getting EMails")
|
||||
}
|
||||
|
||||
for uid, email := range emails {
|
||||
fmt.Printf("\n--- Email UID %d ---\n", uid)
|
||||
fmt.Printf("From: %s\n", email.From)
|
||||
fmt.Printf("Subject: %s\n", email.Subject)
|
||||
fmt.Printf("Date: %s\n", email.Sent.Format("Jan 2, 2006 3:04 PM"))
|
||||
fmt.Printf("Size: %.1f KB\n", float64(email.Size)/1024)
|
||||
|
||||
if len(email.Text) > 100 {
|
||||
fmt.Printf("Preview: %.100s...\n", email.Text)
|
||||
} else if len(email.Text) > 0 {
|
||||
fmt.Printf("Preview: %s\n", email.Text)
|
||||
}
|
||||
|
||||
if len(email.Attachments) > 0 {
|
||||
fmt.Printf("Attachments: %d\n", len(email.Attachments))
|
||||
for _, att := range email.Attachments {
|
||||
fmt.Printf(" - %s (%.1f KB)\n", att.Name, float64(len(att.Content))/1024)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return &imapIntegration{id: m}
|
||||
}
|
||||
Reference in New Issue
Block a user