~gjabell/mfn

2e65bde5555018cd5cdfc4a704c9da335fe38668 — Galen Abell 2 months ago 8feee3a
Add email notifier
4 files changed, 80 insertions(+), 0 deletions(-)

A email.go
M go.mod
M go.sum
M main.go
A email.go => email.go +72 -0
@@ 0,0 1,72 @@
package main

import (
	"bytes"
	"html/template"
	"net/mail"
	"strings"

	"github.com/emersion/go-sasl"
	"github.com/emersion/go-smtp"
	mf "miniflux.app/client"
)

type Email struct {
	subjectTemplate *template.Template
	bodyTemplate    *template.Template
	emailTo         string
	emailFrom       string
	username        string
	password        string
	server          string
}

func NewEmail(conf *Config) *Email {
	ec := conf.EmailConfig

	return &Email{
		subjectTemplate: ec.SubjectTemplate,
		bodyTemplate:    ec.BodyTemplate,
		emailTo:         ec.EmailTo,
		emailFrom:       ec.EmailFrom,
		username:        ec.Username,
		password:        ec.Password,
		server:          ec.Server,
	}
}

func (e *Email) Notify(entry *mf.Entry) error {
	var subject, body bytes.Buffer
	if err := e.subjectTemplate.Execute(&subject, entry); err != nil {
		return err
	}

	if err := e.bodyTemplate.Execute(&body, entry); err != nil {
		return err
	}

	auth := sasl.NewPlainClient("", e.username, e.password)

	toAddr, err := mail.ParseAddress(e.emailTo)
	if err != nil {
		return err
	}
	to := []string{toAddr.Address}

	fromAddr, err := mail.ParseAddress(e.emailFrom)
	if err != nil {
		return err
	}
	from := fromAddr.Address

	msg := strings.NewReader("To: " + e.emailTo + "\r\n" +
		"From: " + e.emailFrom + "\r\n" +
		"Subject: " + subject.String() + "\r\n" +
		"\r\n" +
		body.String() + "\r\n")
	if err := smtp.SendMail(e.server, auth, from, to, msg); err != nil {
		return err
	}

	return nil
}

M go.mod => go.mod +2 -0
@@ 5,6 5,8 @@ go 1.13
require (
	git.sr.ht/~sircmpwn/getopt v0.0.0-20190808004552-daaf1274538b
	github.com/BurntSushi/toml v0.3.1
	github.com/emersion/go-sasl v0.0.0-20191210011802-430746ea8b9b
	github.com/emersion/go-smtp v0.12.1
	github.com/mattn/go-sqlite3 v2.0.2+incompatible
	miniflux.app v0.0.0-20200125042750-15727f716a83
)

M go.sum => go.sum +5 -0
@@ 9,6 9,11 @@ github.com/cheekybits/is v0.0.0-20150225183255-68e9c0620927/go.mod h1:h/aW8ynjgk
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
github.com/emersion/go-sasl v0.0.0-20190817083125-240c8404624e/go.mod h1:G/dpzLu16WtQpBfQ/z3LYiYJn3ZhKSGWn83fyoyQe/k=
github.com/emersion/go-sasl v0.0.0-20191210011802-430746ea8b9b h1:uhWtEWBHgop1rqEk2klKaxPAkVDCXexai6hSuRQ7Nvs=
github.com/emersion/go-sasl v0.0.0-20191210011802-430746ea8b9b/go.mod h1:G/dpzLu16WtQpBfQ/z3LYiYJn3ZhKSGWn83fyoyQe/k=
github.com/emersion/go-smtp v0.12.1 h1:1R8BDqrR2HhlGwgFYcOi+BVTvK1bMjAB65QcVpJ5sNA=
github.com/emersion/go-smtp v0.12.1/go.mod h1:SD9V/xa4ndMw77lR3Mf7htkp8RBNYuPh9UeuBs9tpUQ=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=

M main.go => main.go +1 -0
@@ 78,6 78,7 @@ func main() {
	}

	notifiers := make(map[string]Notifier)
	notifiers["email"] = NewEmail(conf)
	notifiers["webhook"] = NewWebhook(conf)

	mfn := &Mfn{