Able to send mail

This commit is contained in:
nemunaire 2020-05-23 18:44:18 +02:00
parent f996d69347
commit 187b9bb5dc
5 changed files with 333 additions and 0 deletions

3
go.mod
View File

@ -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
)

6
go.sum
View File

@ -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=

View File

@ -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"

153
utils/mail.go Normal file
View File

@ -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
}

169
utils/mail_templates.go Normal file
View File

@ -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 = `
<!DOCTYPE html>
<html lang="{{ .Lang }}">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>{{ .Subject }}</title>
</head>
<body style="margin: 0; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; background-color: rgb(255, 255, 255); text-decoration: none;">
<div style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration: none;">
<div style="margin: 0px auto; max-width: 600px;">
<table align="center" border="0" cellpadding="0" cellspacing="0" role="presentation" style="border-collapse: collapse; width: 600px;">
<tbody>
<tr>
<td style="direction: ltr; font-size: 0px; padding: 0px; text-align: center; vertical-align: top;">
<div class="mj-column-per-100 outlook-group-fix" style="max-width: 100%; width: 600px; font-size: 13px; text-align: left; direction: ltr; display: inline-block; vertical-align: top;">
<table border="0" cellpadding="0" cellspacing="0" role="presentation" width="100%" style="border-collapse: collapse; vertical-align: top;">
<tbody>
<tr>
<td align="left" style="border-collapse: collapse; font-size: 0px; padding: 16px 0px 0px 0px; word-break: break-word;">
<table border="0" cellpadding="0" cellspacing="0" role="presentation" style="border-collapse: collapse; border-spacing: 0px;">
<tbody>
<tr>
<td style="border-collapse: collapse; font-size: 30px;">
<img alt="happyDNS" height="24" src="cid:happydns.png" style="border: 0px; height: 24px; line-height: 0px; outline: none; text-decoration: none; display: block;">
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</div>
</td>
</tr>
</tbody>
</table>
<table align="center" border="0" cellpadding="0" cellspacing="0" role="presentation" style="border-collapse: collapse; background-color: rgb(255, 255, 255); width: 600px; background-position: initial initial; background-repeat: initial initial;">
<tbody>
<tr>
<td style="border-collapse: collapse; direction: ltr; font-size: 0px; padding: 0px; text-align: center; vertical-align: top;">
<div style="margin: 0px auto; max-width: 600px;">
<table align="center" border="0" cellpadding="0" cellspacing="0" role="presentation" style="border-collapse: collapse; width: 600px;">
<tbody>
<tr>
<td style="border-collapse: collapse; direction: ltr; font-size: 0px; padding: 0px 0px 8px 0px; text-align: center; vertical-align: top;">
<div class="mj-column-per-100 outlook-group-fix" style="max-width: 100%; width: 600px; font-size: 13px; text-align: left; direction: ltr; display: inline-block; vertical-align: top;">
<table border="0" cellpadding="0" cellspacing="0" role="presentation" width="100%" style="border-collapse: collapse; vertical-align: top;">
<tbody>
<tr>
<td align="left" style="border-collapse: collapse; font-family: Montserrat, Arial; font-size: 16px; line-height: 1.5; text-align: left; color: rgb(50, 54, 63); padding: 0px 24px 8px; word-break: break-word;">
{{ .Content }}
</td>
</tr>
<tr>
<td align="left" style="border-collapse: collapse; font-size: 0px; padding: 0px 24px 16px; word-break: break-word;">
<div style="font-family: Montserrat, Arial; font-size: 16px; line-height: 1.5; text-align: left; color: rgb(50, 54, 63);">Regards,<br>Fred - customer support @ happyDNS</div>
</td>
</tr>
</tbody>
</table>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<div style="margin: 0px auto; max-width: 600px;">
<table align="center" border="0" cellpadding="0" cellspacing="0" role="presentation" style="border-collapse: collapse; width: 600px;">
<tbody>
<tr>
<td style="border-collapse: collapse; direction: ltr; font-size: 0px; padding: 0px; text-align: center; vertical-align: top;">
<div style="margin: 0px auto; max-width: 600px;">
<table align="center" border="0" cellpadding="0" cellspacing="0" role="presentation" style="border-collapse: collapse; width: 600px;">
<tbody>
<tr>
<td style="border-collapse: collapse; direction: ltr; font-size: 0px; padding: 0px; text-align: center; vertical-align: top;">
<div class="mj-column-per-100 outlook-group-fix" style="max-width: 100%; width: 600px; font-size: 13px; text-align: left; direction: ltr; display: inline-block; vertical-align: top;">
<table border="0" cellpadding="0" cellspacing="0" role="presentation" width="100%" style="border-collapse: collapse; vertical-align: top;">
<tbody>
<tr>
<td align="center" style="border-collapse: collapse; font-size: 0px; padding: 0px; word-break: break-word;">
<div style="font-family: Montserrat, Arial; font-size: 16px; font-weight: bold; line-height: 1.5; text-align: center; color: rgb(28, 180, 135);">happyDNS, finally a simple interface for domain names.</div>
</td>
</tr>
</tbody>
</table>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<div style="margin: 0px auto; max-width: 600px;">
<table align="center" border="0" cellpadding="0" cellspacing="0" role="presentation" style="border-collapse: collapse; width: 600px;">
<tbody>
<tr>
<td style="border-collapse: collapse; direction: ltr; font-size: 0px; padding: 0px; text-align: center; vertical-align: top;">
<div class="mj-column-per-100 outlook-group-fix" style="max-width: 100%; width: 600px; font-size: 13px; text-align: left; direction: ltr; display: inline-block; vertical-align: top;">
<table border="0" cellpadding="0" cellspacing="0" role="presentation" width="100%" style="border-collapse: collapse; vertical-align: top;">
<tbody>
<tr>
<td align="center" style="border-collapse: collapse; font-size: 0px; padding: 10px 25px; word-break: break-word;">
<div style="font-family: Montserrat, Arial; font-size: 12px; line-height: 1.5; text-align: center; color: rgb(50, 54, 63);">
<a href="https://happydns.org/en/legal-notice" style="color: rgb(93, 97, 101);">Legal Notice</a>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</body>
</html>`
const mailTXTTpl = `{{ .Content }}
--
Fred - customer support @ happyDNS
Legal notice: https://www.happydns.org/en/legal-notice/`