Plain text and HTML emails

This commit is contained in:
Noah 2017-11-26 19:05:31 -08:00
parent 527e995c1c
commit 4a7a87c306
5 changed files with 22 additions and 8 deletions

View File

@ -10,6 +10,7 @@ import (
"github.com/kirsle/blog/core/models/comments"
"github.com/kirsle/blog/core/models/settings"
"github.com/microcosm-cc/bluemonday"
gomail "gopkg.in/gomail.v2"
)
@ -51,11 +52,28 @@ func (b *Blog) SendEmail(email Email) {
log.Error("SendEmail: template execution error: %s", err.Error())
}
// Condense the body down to plain text, lazily. Who even has a plain text
// email client anymore?
rawLines := strings.Split(
bluemonday.StrictPolicy().Sanitize(html.String()),
"\n",
)
var lines []string
for _, line := range rawLines {
line = strings.TrimSpace(line)
if len(line) == 0 {
continue
}
lines = append(lines, line)
}
plaintext := strings.Join(lines, "\n\n")
m := gomail.NewMessage()
m.SetHeader("From", s.Mail.Sender)
m.SetHeader("From", fmt.Sprintf("%s <%s>", s.Site.Title, s.Mail.Sender))
m.SetHeader("To", email.To)
m.SetHeader("Subject", email.Subject)
m.SetBody("text/html", html.String())
m.SetBody("text/plain", plaintext)
m.AddAlternative("text/html", html.String())
d := gomail.NewDialer(s.Mail.Host, s.Mail.Port, s.Mail.Username, s.Mail.Password)
if b.Debug {

View File

@ -21,8 +21,6 @@ const (
// Request context.
func (b *Blog) SessionLoader(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
session, _ := b.store.Get(r, "session")
log.Debug("REQUEST START: %s %s", r.Method, r.URL.Path)
ctx := context.WithValue(r.Context(), sessionKey, session)
next(w, r.WithContext(ctx))
}
@ -95,7 +93,6 @@ func (b *Blog) LoggedIn(r *http.Request) bool {
func (b *Blog) AuthMiddleware(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
u, err := b.CurrentUser(r)
if err != nil {
log.Error("Error loading user from session: %v", err)
next(w, r)
return
}

View File

@ -156,7 +156,6 @@ func (c *Comment) ParseForm(r *http.Request) {
// attribute is currently empty.
define := func(target *string, value string) {
if value != "" {
log.Info("SET DEFINE: %s", value)
*target = value
}
}

View File

@ -35,7 +35,7 @@ func (b *Blog) PageHandler(w http.ResponseWriter, r *http.Request) {
filepath, err := b.ResolvePath(path)
if err != nil {
// See if it resolves as a blog entry.
err = b.viewPost(w, r, path)
err = b.viewPost(w, r, strings.TrimLeft(path, "/"))
if err != nil {
b.NotFound(w, r, "The page you were looking for was not found.")
}

View File

@ -42,7 +42,7 @@
{{ if .UnsubscribeURL }}
<br><br>
To unsubscribe from this message, visit <a href="{{ .UnsubscribeURL }}" target="_blank">{{ .UnsubscribeURL }}</a>
To unsubscribe from this comment thread, visit <a href="{{ .UnsubscribeURL }}" target="_blank">{{ .UnsubscribeURL }}</a>
{{ end }}
</font>
</td>