Semble compliqué

This commit is contained in:
Laurent Ulrich
2025-07-28 15:52:55 +02:00
parent adfac31417
commit a559784462
3 changed files with 101 additions and 23 deletions

18
configuration.go Normal file
View File

@@ -0,0 +1,18 @@
package main
type MailBoxConfiguration struct {
Server string
Port int
User string
Password string
SSL string
}
type BlogConfiguration struct {
Title string
MailBox string
WWWRoot string
}
type Configuration struct {
MailBoxes map[string]MailBoxConfiguration
Blogs map[string]BlogConfiguration
}

View File

@@ -1,17 +1,22 @@
package main
import (
"bytes"
"io"
"log"
"mime"
"github.com/emersion/go-imap/v2"
"github.com/emersion/go-imap/v2/imapclient"
"github.com/emersion/go-message/charset"
"github.com/emersion/go-message/mail"
)
type MailBox struct {
Server string
User string
Password string
InBox string
Client *imapclient.Client
}
@@ -36,6 +41,61 @@ func (mb *MailBox) Connect() error {
}
log.Println("Logged in")
mailBoxes, err := mb.Client.List("", "%", nil).Collect()
if err != nil {
log.Fatal("Error listing mailboxes", err)
}
for _, mbox := range mailBoxes {
log.Println("mailbox:", mbox.Mailbox)
}
inbox, err := mb.Client.Select(mb.InBox, nil).Wait()
if err != nil {
log.Fatal("Error selecting mailbox:", mb.InBox, err)
}
log.Println("Inbox has", inbox.NumMessages, "messages")
for i := uint32(0); i <= inbox.NumMessages; i++ {
seqSet := imap.SeqSetNum(i)
bodySection := &imap.FetchItemBodySection{}
fetchOptions := &imap.FetchOptions{
Envelope: true,
BodySection: []*imap.FetchItemBodySection{bodySection},
}
messages, err := mb.Client.Fetch(seqSet, fetchOptions).Collect()
if err != nil {
log.Fatal("Error fetching mails:", err)
}
for _, msg := range messages {
log.Println(msg.Envelope.From)
log.Println(msg.Envelope.To)
log.Println(msg.Envelope.Date)
log.Println(msg.Envelope.Subject)
section := msg.FindBodySection(bodySection)
ioReader := bytes.NewReader(section)
mailReader, err := mail.CreateReader(ioReader)
if err != nil {
log.Fatalf("failed to create mail reader: %v", err)
}
for {
part, err := mailReader.NextPart()
if err == io.EOF {
log.Println("------------")
break
} else if err != nil {
log.Fatal("Error reading part", err)
}
switch header := part.Header.(type) {
case *mail.AttachmentHeader:
filename, _ := header.Filename()
log.Println("Attachment:", filename)
case *mail.InlineHeader:
body, _ := io.ReadAll(part.Body)
log.Println(string(body))
}
}
}
}
return nil
}

View File

@@ -5,6 +5,7 @@ import (
"net/http"
"net/url"
"os"
"strings"
)
func slash(w http.ResponseWriter, r *http.Request) {
@@ -15,8 +16,6 @@ func main() {
var mb MailBox
url, err := url.Parse(os.Getenv("MAILBOX"))
if err != nil {
log.Fatal("Bad parameter MAILBOX:", os.Getenv("MAILBOX"), "(", err, ")")
@@ -24,6 +23,7 @@ func main() {
mb.Server = url.Host
mb.User = os.Getenv("IMAP_USER")
mb.Password = os.Getenv("IMAP_PASSWORD")
mb.InBox, _ = strings.CutPrefix(url.Path, "/")
err = mb.Connect()
if err != nil {
log.Fatal(err)