From 637e391d77a67aebfd8c8f2a172b29ec9a0a4eb0 Mon Sep 17 00:00:00 2001 From: ItsNotGoodName Date: Sun, 23 Jul 2023 02:45:53 -0700 Subject: [PATCH] feat: html2text --- README.md | 2 +- go.mod | 2 +- internal/envelope/message.go | 12 +++++++++++- internal/envelope/utils.go | 4 ++++ 4 files changed, 17 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 961bff89..8d855e98 100644 --- a/README.md +++ b/README.md @@ -35,7 +35,7 @@ smtpbridge --version ## Config -Config file is loaded from one of the following locations. +Config file is loaded from one of the following locations. - `config.yaml` - `config.yml` diff --git a/go.mod b/go.mod index 9e2ccf8e..52d52973 100644 --- a/go.mod +++ b/go.mod @@ -11,6 +11,7 @@ require ( github.com/emersion/go-smtp v0.16.0 github.com/gofiber/fiber/v2 v2.47.0 github.com/gofiber/template/html/v2 v2.0.4 + github.com/jaytaylor/html2text v0.0.0-20200412013138-3577fbdbcff7 github.com/jhillyerd/enmime v1.0.0 github.com/labstack/gommon v0.4.0 github.com/rs/zerolog v1.29.1 @@ -29,7 +30,6 @@ require ( github.com/gofiber/utils v1.1.0 // indirect github.com/gogs/chardet v0.0.0-20191104214054-4b6791f73a28 // indirect github.com/google/uuid v1.3.0 // indirect - github.com/jaytaylor/html2text v0.0.0-20200412013138-3577fbdbcff7 // indirect github.com/jinzhu/inflection v1.0.0 // indirect github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect github.com/klauspost/compress v1.16.3 // indirect diff --git a/internal/envelope/message.go b/internal/envelope/message.go index 9b89454e..7a1745cc 100644 --- a/internal/envelope/message.go +++ b/internal/envelope/message.go @@ -4,6 +4,7 @@ import ( "time" "github.com/ItsNotGoodName/smtpbridge/pkg/pagination" + "github.com/jaytaylor/html2text" "github.com/samber/lo" ) @@ -17,12 +18,21 @@ type CreateMessage struct { } func NewMessage(r CreateMessage) *Message { + text := r.Text + if isHTML(r.Text) { + var err error + text, err = html2text.FromString(r.Text) + if err != nil { + text = r.Text + } + } + return &Message{ From: r.From, To: lo.Uniq(r.To), CreatedAt: time.Now(), Subject: r.Subject, - Text: r.Text, + Text: text, HTML: r.HTML, Date: r.Date, } diff --git a/internal/envelope/utils.go b/internal/envelope/utils.go index 6265e71a..32a51ca8 100644 --- a/internal/envelope/utils.go +++ b/internal/envelope/utils.go @@ -34,3 +34,7 @@ func fileExtension(name string, mimeT string) string { } return extension } + +func isHTML(maybeHTML string) bool { + return strings.HasPrefix(maybeHTML, "") +}