~gjabell/mfn

0e591347646ee327a47c86a36c2dddc2bf5af305 — Galen Abell 1 year, 3 months ago 7eaf903
Fix email date header not being set

Refactored email notification to use connection.
5 files changed, 83 insertions(+), 13 deletions(-)

M config.go
M email.go
M go.mod
M go.sum
M main.go
M config.go => config.go +1 -0
@@ 21,6 21,7 @@ type EmailConfig struct {
	Username           string    `toml:"username"`
	Password           string    `toml:"password"`
	Server             string    `toml:"server"`
	StartTLS           bool      `toml:"starttls"`
	HTML               bool      `toml:"html"`
}


M email.go => email.go +73 -12
@@ 2,9 2,14 @@ package main

import (
	"bytes"
	"net/mail"
	"crypto/tls"
	"errors"
	"io"
	gomail "net/mail"
	"strings"
	"time"

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


@@ 18,6 23,7 @@ type Email struct {
	username        string
	password        string
	server          string
	startTLS        bool
}

func NewEmail(conf *Config) *Email {


@@ 31,6 37,7 @@ func NewEmail(conf *Config) *Email {
		username:        ec.Username,
		password:        ec.Password,
		server:          ec.Server,
		startTLS:        ec.StartTLS,
	}
}



@@ 44,26 51,80 @@ func (e *Email) Notify(entry *mf.Entry) error {
		return err
	}

	host := e.server
	serverName := e.server
	if !strings.ContainsRune(host, ':') {
		host = host + ":587" // Default to submission port
	} else {
		serverName = host[:strings.IndexRune(host, ':')]
	}

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

	toAddr, err := mail.ParseAddress(e.emailTo)
	toAddr, err := gomail.ParseAddress(e.emailTo)
	if err != nil {
		return err
	}

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

	conn, err := smtp.Dial(host)
	if err != nil {
		return err
	}
	defer conn.Close()
	if sup, _ := conn.Extension("STARTTLS"); sup {
		if !e.startTLS {
			return errors.New("STARTTLS is supported by this server, " +
				"but is not set in config. " +
				"Add starttls=true under the email configuration.")
		}
		if err = conn.StartTLS(&tls.Config{
			ServerName: serverName,
		}); err != nil {
			return err
		}
	} else {
		if e.startTLS {
			return errors.New("STARTTLS requested, but not supported " +
				"by this SMTP server. Is someone tampering with your " +
				"connection?")
		}
	}
	if err := conn.Auth(auth); err != nil {
		return err
	}

	if err := conn.Mail(fromAddr.Address, nil); err != nil {
		return err
	}
	if err := conn.Rcpt(toAddr.Address); err != nil {
		return err
	}

	wc, err := conn.Data()
	if err != nil {
		return err
	}
	to := []string{toAddr.Address}
	defer wc.Close()

	var header mail.Header
	header.SetAddressList("To", []*mail.Address{(*mail.Address)(toAddr)})
	header.SetAddressList("From", []*mail.Address{(*mail.Address)(fromAddr)})
	header.SetSubject(subject.String())
	header.SetDate(time.Now())
	header.SetContentType("text/plain", map[string]string{"charset": "UTF-8"})

	fromAddr, err := mail.ParseAddress(e.emailFrom)
	w, err := mail.CreateSingleInlineWriter(wc, header)
	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 {
	defer w.Close()

	if _, err := io.Copy(w, &body); err != nil {
		return err
	}


M go.mod => go.mod +1 -0
@@ 5,6 5,7 @@ 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-message v0.11.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

M go.sum => go.sum +7 -0
@@ 8,18 8,25 @@ github.com/andybalholm/cascadia v1.0.0/go.mod h1:GsXiBklL0woXo1j/WYWtSYYC4ouU9Pq
github.com/cheekybits/is v0.0.0-20150225183255-68e9c0620927/go.mod h1:h/aW8ynjgkuj+NQRlZcDbAbM1ORAbXjXX77sX7T289U=
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/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
github.com/emersion/go-message v0.11.1 h1:0C/S4JIXDTSfXB1vpqdimAYyK4+79fgEAMQ0dSL+Kac=
github.com/emersion/go-message v0.11.1/go.mod h1:C4jnca5HOTo4bGN9YdqNQM9sITuT3Y0K6bSUw9RklvY=
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/emersion/go-textwrapper v0.0.0-20160606182133-d0e65e56babe h1:40SWqY0zE3qCi6ZrtTf5OUdNm5lDnGnjRSq9GgmeTrg=
github.com/emersion/go-textwrapper v0.0.0-20160606182133-d0e65e56babe/go.mod h1:aqO8z8wPrjkscevZJFVE1wXJrLpC5LtJG7fqLOsPb2U=
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=
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/gorilla/mux v1.7.3/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/martinlindhe/base36 v1.0.0 h1:eYsumTah144C0A8P1T/AVSUk5ZoLnhfYFM3OGQxB52A=
github.com/martinlindhe/base36 v1.0.0/go.mod h1:+AtEs8xrBpCeYgSLoY/aJ6Wf37jtBuR0s35750M27+8=
github.com/matryer/try v0.0.0-20161228173917-9ac251b645a2/go.mod h1:0KeJpeMD6o+O4hW7qJOT7vyQPKrWmj26uf5wMc/IiIs=
github.com/mattn/go-sqlite3 v2.0.2+incompatible h1:qzw9c2GNT8UFrgWNDhCTqRqYUSmu/Dav/9Z58LGpk7U=
github.com/mattn/go-sqlite3 v2.0.2+incompatible/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=

M main.go => main.go +1 -1
@@ 13,7 13,7 @@ import (
	mf "miniflux.app/client"
)

var version = "0.2.1"
var version = "0.2.2"

type Mfn struct {
	conf      *Config