Skip to content

Commit

Permalink
feat: html2text
Browse files Browse the repository at this point in the history
  • Loading branch information
ItsNotGoodName committed Jul 23, 2023
1 parent 642c8b2 commit 637e391
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
12 changes: 11 additions & 1 deletion internal/envelope/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"time"

"github.com/ItsNotGoodName/smtpbridge/pkg/pagination"
"github.com/jaytaylor/html2text"
"github.com/samber/lo"
)

Expand All @@ -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,
}
Expand Down
4 changes: 4 additions & 0 deletions internal/envelope/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,7 @@ func fileExtension(name string, mimeT string) string {
}
return extension
}

func isHTML(maybeHTML string) bool {
return strings.HasPrefix(maybeHTML, "<!DOCTYPE html>")
}

0 comments on commit 637e391

Please sign in to comment.