diff --git a/go.mod b/go.mod index 49b94b7..e86e7b0 100644 --- a/go.mod +++ b/go.mod @@ -3,11 +3,14 @@ module git.happydns.org/happydns go 1.14 require ( + github.com/go-mail/mail v2.3.1+incompatible github.com/go-sql-driver/mysql v1.5.0 github.com/julienschmidt/httprouter v1.3.0 github.com/miekg/dns v1.1.29 github.com/ovh/go-ovh v0.0.0-20181109152953-ba5adb4cf014 github.com/syndtr/goleveldb v1.0.0 + github.com/yuin/goldmark v1.1.30 golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550 + gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect gopkg.in/ini.v1 v1.55.0 // indirect ) diff --git a/go.sum b/go.sum index 2df38b5..1f5a59b 100644 --- a/go.sum +++ b/go.sum @@ -1,4 +1,6 @@ github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= +github.com/go-mail/mail v2.3.1+incompatible h1:UzNOn0k5lpfVtO31cK3hn6I4VEVGhe3lX8AJBAxXExM= +github.com/go-mail/mail v2.3.1+incompatible/go.mod h1:VPWjmmNyRsWXQZHVHT3g0YbIINUkSmuKOiLIDkWbL6M= github.com/go-sql-driver/mysql v1.5.0 h1:ozyZYNQW3x3HtqT1jira07DN2PArx2v7/mN66gGcHOs= github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= @@ -16,6 +18,8 @@ github.com/ovh/go-ovh v0.0.0-20181109152953-ba5adb4cf014 h1:37VE5TYj2m/FLA9SNr4z github.com/ovh/go-ovh v0.0.0-20181109152953-ba5adb4cf014/go.mod h1:joRatxRJaZBsY3JAOEMcoOp05CnZzsx4scTxi95DHyQ= github.com/syndtr/goleveldb v1.0.0 h1:fBdIW9lB4Iz0n9khmH8w27SJ3QEJ7+IgjPEwGSZiFdE= github.com/syndtr/goleveldb v1.0.0/go.mod h1:ZVVdQEZoIme9iO1Ch2Jdy24qqXrMMOU6lpPAyBWyWuQ= +github.com/yuin/goldmark v1.1.30 h1:j4d4Lw3zqZelDhBksEo3BnWg9xhXRQGJPPSL6OApZjI= +github.com/yuin/goldmark v1.1.30/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550 h1:ObdrDkeb4kJdCP557AjRjq69pTHfNouLtWZG7j9rPN8= golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= @@ -36,6 +40,8 @@ golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/tools v0.0.0-20191216052735-49a3e744a425 h1:VvQyQJN0tSuecqgcIxMWnnfG5kSmgy9KZR9sW3W5QeA= golang.org/x/tools v0.0.0-20191216052735-49a3e744a425/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc h1:2gGKlE2+asNV9m7xrywl36YYNnBG5ZQ0r/BOOxqPpmk= +gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc/go.mod h1:m7x9LTH6d71AHyAX77c9yqWCCa3UKHcVEj9y7hAtKDk= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= gopkg.in/ini.v1 v1.55.0 h1:E8yzL5unfpW3M6fz/eB7Cb5MQAYSZ7GKo4Qth+N2sgQ= diff --git a/static.go b/static.go index b9bcdaf..6780907 100644 --- a/static.go +++ b/static.go @@ -48,6 +48,8 @@ import ( //go:generate yarn --cwd htdocs --offline build //go:generate go-bindata -ignore "\\.go|\\.less" -pkg "main" -o "bindata.go" htdocs/dist/... //go:generate go fmt bindata.go +//go:generate go-bindata -ignore "\\.go|\\.less" -pkg "utils" -o "utils/bindata.go" htdocs/dist/img/happydns.png +//go:generate go fmt utils/bindata.go const StaticDir string = "htdocs/dist" diff --git a/utils/mail.go b/utils/mail.go new file mode 100644 index 0000000..0d4645c --- /dev/null +++ b/utils/mail.go @@ -0,0 +1,153 @@ +// Copyright or © or Copr. happyDNS (2020) +// +// contact@happydns.org +// +// This software is a computer program whose purpose is to provide a modern +// interface to interact with DNS systems. +// +// This software is governed by the CeCILL license under French law and abiding +// by the rules of distribution of free software. You can use, modify and/or +// redistribute the software under the terms of the CeCILL license as +// circulated by CEA, CNRS and INRIA at the following URL +// "http://www.cecill.info". +// +// As a counterpart to the access to the source code and rights to copy, modify +// and redistribute granted by the license, users are provided only with a +// limited warranty and the software's author, the holder of the economic +// rights, and the successive licensors have only limited liability. +// +// In this respect, the user's attention is drawn to the risks associated with +// loading, using, modifying and/or developing or reproducing the software by +// the user in light of its specific status of free software, that may mean +// that it is complicated to manipulate, and that also therefore means that it +// is reserved for developers and experienced professionals having in-depth +// computer knowledge. Users are therefore encouraged to load and test the +// software's suitability as regards their requirements in conditions enabling +// the security of their systems and/or data to be ensured and, more generally, +// to use and operate it in the same conditions as regards security. +// +// The fact that you are presently reading this means that you have had +// knowledge of the CeCILL license and that you accept its terms. + +package utils + +import ( + "bytes" + "io" + "net/mail" + "os" + "os/exec" + "text/template" + + gomail "github.com/go-mail/mail" + "github.com/yuin/goldmark" + "github.com/yuin/goldmark/extension" + "github.com/yuin/goldmark/parser" + "github.com/yuin/goldmark/renderer/html" +) + +const sendmail = "/usr/sbin/sendmail" + +var ( + MailFrom = mail.Address{Name: "Fred From happyDNS", Address: "contact@happyDNS.org"} + SendMethod gomail.Sender = &SystemSendmail{} +) + +func SendMail(to *mail.Address, subject, content string) (err error) { + m := gomail.NewMessage() + m.SetHeader("From", MailFrom.String()) + m.SetHeader("To", to.String()) + m.SetHeader("Subject", subject) + + toName := to.Name + if len(toName) == 0 { + toName = to.Address + } + + tplData := map[string]string{ + "Lang": "en", + "To": toName, + "ToAddress": to.Address, + "Subject": subject, + "From": MailFrom.Name, + "FromAddress": MailFrom.Address, + "Content": content, + } + + if t, err := template.New("mailText").Parse(mailTXTTpl); err != nil { + return err + } else { + m.SetBodyWriter("text/plain", func(w io.Writer) error { + return t.Execute(w, tplData) + }) + } + + // Convert text from Markdown to HTML + md := goldmark.New( + goldmark.WithExtensions(extension.GFM), + goldmark.WithParserOptions( + parser.WithAutoHeadingID(), + ), + goldmark.WithRendererOptions( + html.WithHardWraps(), + html.WithXHTML(), + ), + ) + + var buf bytes.Buffer + if err = md.Convert([]byte(content), &buf); err != nil { + return + } + + if data, err := Asset("htdocs/dist/img/happydns.png"); err != nil { + m.EmbedReader("happydns.png", bytes.NewReader(data)) + } + + if t, err := template.New("mailHTML").Parse(mailHTMLTpl); err != nil { + return err + } else { + m.AddAlternativeWriter("text/html", func(w io.Writer) error { + tplData["Content"] = buf.String() + return t.Execute(w, tplData) + }) + } + + if err = gomail.Send(SendMethod, m); err != nil { + return + } + + return +} + +type SystemSendmail struct{} + +// system_sendmail uses the sendmail command to submit the given message +func (t *SystemSendmail) Send(from string, to []string, msg io.WriterTo) error { + cmd := exec.Command(sendmail, "-t") + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + + pw, err := cmd.StdinPipe() + if err != nil { + return err + } + + err = cmd.Start() + if err != nil { + return err + } + + if _, err = msg.WriteTo(pw); err != nil { + return err + } + + if err = pw.Close(); err != nil { + return err + } + + if err = cmd.Wait(); err != nil { + return err + } + + return nil +} diff --git a/utils/mail_templates.go b/utils/mail_templates.go new file mode 100644 index 0000000..fb075d4 --- /dev/null +++ b/utils/mail_templates.go @@ -0,0 +1,169 @@ +// Copyright or © or Copr. happyDNS (2020) +// +// contact@happydns.org +// +// This software is a computer program whose purpose is to provide a modern +// interface to interact with DNS systems. +// +// This software is governed by the CeCILL license under French law and abiding +// by the rules of distribution of free software. You can use, modify and/or +// redistribute the software under the terms of the CeCILL license as +// circulated by CEA, CNRS and INRIA at the following URL +// "http://www.cecill.info". +// +// As a counterpart to the access to the source code and rights to copy, modify +// and redistribute granted by the license, users are provided only with a +// limited warranty and the software's author, the holder of the economic +// rights, and the successive licensors have only limited liability. +// +// In this respect, the user's attention is drawn to the risks associated with +// loading, using, modifying and/or developing or reproducing the software by +// the user in light of its specific status of free software, that may mean +// that it is complicated to manipulate, and that also therefore means that it +// is reserved for developers and experienced professionals having in-depth +// computer knowledge. Users are therefore encouraged to load and test the +// software's suitability as regards their requirements in conditions enabling +// the security of their systems and/or data to be ensured and, more generally, +// to use and operate it in the same conditions as regards security. +// +// The fact that you are presently reading this means that you have had +// knowledge of the CeCILL license and that you accept its terms. + +package utils + +const mailHTMLTpl = ` + + + + + + + {{ .Subject }} + + +
+
+ + + + + + +
+
+ + + + + + +
+ + + + + + +
+ happyDNS +
+
+
+
+ + + + + + +
+
+ + + + + + +
+
+ + + + + + + + + +
+ {{ .Content }} +
+
Regards,
Fred - customer support @ happyDNS
+
+
+
+
+
+
+
+ + + + + + +
+
+ + + + + + +
+
+ + + + + + +
+
happyDNS, finally a simple interface for domain names.
+
+
+
+
+
+ + + + + + +
+
+ + + + + + +
+ +
+
+
+
+
+
+
+ +` + +const mailTXTTpl = `{{ .Content }} +-- +Fred - customer support @ happyDNS +Legal notice: https://www.happydns.org/en/legal-notice/`