rempve text body

This commit is contained in:
Simon Rieger 2025-06-23 10:04:12 +02:00
parent fef0719277
commit b34bbdff95

58
main.go
View file

@ -3,16 +3,15 @@ package main
import (
"crypto/tls"
"fmt"
"io"
"log"
"net/http"
"os"
"strings"
"time"
"io"
"strings"
"github.com/emersion/go-imap"
"github.com/emersion/go-imap/client"
"github.com/emersion/go-message/mail"
)
func main() {
@ -96,22 +95,9 @@ func main() {
replyTo = from
}
var bodyText string
if r := msg.GetBody(section); r != nil {
bodyBytes, err := io.ReadAll(r)
if err != nil {
log.Printf("Fehler beim Lesen des Bodys: %v", err)
} else {
bodyText = extractTextFromBody(bodyBytes)
if len(bodyText) > 200 {
bodyText = bodyText[:200] + "..."
}
}
}
message := fmt.Sprintf(
"Betreff: %s\nVon: %s\nAntwort an: %s\n\n%s",
subject, from, replyTo, bodyText,
"Von: %s\nAntwort an: %s",
from, replyTo,
)
sendNtfyNotification(ntfyServer, ntfyTopic, ntfyUser, ntfyPass,
@ -133,40 +119,6 @@ func main() {
}
}
func extractTextFromBody(bodyBytes []byte) string {
r := strings.NewReader(string(bodyBytes))
mr, err := mail.CreateReader(r)
if err != nil {
return string(bodyBytes)
}
var textContent strings.Builder
for {
p, err := mr.NextPart()
if err == io.EOF {
break
}
if err != nil {
continue
}
switch h := p.Header.(type) {
case *mail.InlineHeader:
contentType, _, _ := h.ContentType()
if contentType == "text/plain" {
partBytes, _ := io.ReadAll(p.Body)
textContent.Write(partBytes)
}
}
}
if textContent.Len() > 0 {
return textContent.String()
}
return string(bodyBytes)
}
func sendNtfyNotification(server, topic, user, pass, title, message, replyTo string) {
url := fmt.Sprintf("%s/%s", server, topic)
log.Printf("Sende ntfy-Benachrichtigung an %s", url)
@ -175,10 +127,10 @@ func sendNtfyNotification(server, topic, user, pass, title, message, replyTo str
req.Header.Set("Title", title)
req.Header.Set("Content-Type", "text/plain")
// Action-Button für Antworten
if replyTo != "" {
action := fmt.Sprintf("view, Antworten, mailto:%s", replyTo)
req.Header.Set("Actions", action)
log.Printf("Füge Action-Button hinzu: %s", action)
}
if user != "" && pass != "" {