Plain text and HTML emails
This commit is contained in:
parent
527e995c1c
commit
4a7a87c306
22
core/mail.go
22
core/mail.go
|
@ -10,6 +10,7 @@ import (
|
||||||
|
|
||||||
"github.com/kirsle/blog/core/models/comments"
|
"github.com/kirsle/blog/core/models/comments"
|
||||||
"github.com/kirsle/blog/core/models/settings"
|
"github.com/kirsle/blog/core/models/settings"
|
||||||
|
"github.com/microcosm-cc/bluemonday"
|
||||||
gomail "gopkg.in/gomail.v2"
|
gomail "gopkg.in/gomail.v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -51,11 +52,28 @@ func (b *Blog) SendEmail(email Email) {
|
||||||
log.Error("SendEmail: template execution error: %s", err.Error())
|
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 := 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("To", email.To)
|
||||||
m.SetHeader("Subject", email.Subject)
|
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)
|
d := gomail.NewDialer(s.Mail.Host, s.Mail.Port, s.Mail.Username, s.Mail.Password)
|
||||||
if b.Debug {
|
if b.Debug {
|
||||||
|
|
|
@ -21,8 +21,6 @@ const (
|
||||||
// Request context.
|
// Request context.
|
||||||
func (b *Blog) SessionLoader(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
|
func (b *Blog) SessionLoader(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
|
||||||
session, _ := b.store.Get(r, "session")
|
session, _ := b.store.Get(r, "session")
|
||||||
|
|
||||||
log.Debug("REQUEST START: %s %s", r.Method, r.URL.Path)
|
|
||||||
ctx := context.WithValue(r.Context(), sessionKey, session)
|
ctx := context.WithValue(r.Context(), sessionKey, session)
|
||||||
next(w, r.WithContext(ctx))
|
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) {
|
func (b *Blog) AuthMiddleware(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
|
||||||
u, err := b.CurrentUser(r)
|
u, err := b.CurrentUser(r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("Error loading user from session: %v", err)
|
|
||||||
next(w, r)
|
next(w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
@ -156,7 +156,6 @@ func (c *Comment) ParseForm(r *http.Request) {
|
||||||
// attribute is currently empty.
|
// attribute is currently empty.
|
||||||
define := func(target *string, value string) {
|
define := func(target *string, value string) {
|
||||||
if value != "" {
|
if value != "" {
|
||||||
log.Info("SET DEFINE: %s", value)
|
|
||||||
*target = value
|
*target = value
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -35,7 +35,7 @@ func (b *Blog) PageHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
filepath, err := b.ResolvePath(path)
|
filepath, err := b.ResolvePath(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// See if it resolves as a blog entry.
|
// 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 {
|
if err != nil {
|
||||||
b.NotFound(w, r, "The page you were looking for was not found.")
|
b.NotFound(w, r, "The page you were looking for was not found.")
|
||||||
}
|
}
|
||||||
|
|
|
@ -42,7 +42,7 @@
|
||||||
|
|
||||||
{{ if .UnsubscribeURL }}
|
{{ if .UnsubscribeURL }}
|
||||||
<br><br>
|
<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 }}
|
{{ end }}
|
||||||
</font>
|
</font>
|
||||||
</td>
|
</td>
|
||||||
|
|
Loading…
Reference in New Issue
Block a user