mirror of
https://github.com/1f349/simplemail.git
synced 2024-12-22 08:04:11 +00:00
Initial commit
This commit is contained in:
commit
bd57c983cf
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
.idea/
|
26
fromaddress.go
Normal file
26
fromaddress.go
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
package simplemail
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"net/mail"
|
||||||
|
)
|
||||||
|
|
||||||
|
type FromAddress struct {
|
||||||
|
*mail.Address
|
||||||
|
}
|
||||||
|
|
||||||
|
var _ json.Unmarshaler = &FromAddress{}
|
||||||
|
|
||||||
|
func (f *FromAddress) UnmarshalJSON(b []byte) error {
|
||||||
|
var a string
|
||||||
|
err := json.Unmarshal(b, &a)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
address, err := mail.ParseAddress(a)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
f.Address = address
|
||||||
|
return nil
|
||||||
|
}
|
12
go.mod
Normal file
12
go.mod
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
module github.com/1f349/simplemail
|
||||||
|
|
||||||
|
go 1.22
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/1f349/overlapfs v0.0.1
|
||||||
|
github.com/emersion/go-message v0.18.0
|
||||||
|
github.com/emersion/go-sasl v0.0.0-20231106173351-e73c9f7bad43
|
||||||
|
github.com/emersion/go-smtp v0.20.2
|
||||||
|
)
|
||||||
|
|
||||||
|
require github.com/emersion/go-textwrapper v0.0.0-20200911093747-65d896831594 // indirect
|
50
go.sum
Normal file
50
go.sum
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
github.com/1f349/overlapfs v0.0.1 h1:LAxBolrXFAgU0yqZtXg/C/aaPq3eoQSPpBc49BHuTp0=
|
||||||
|
github.com/1f349/overlapfs v0.0.1/go.mod h1:I6aItQycr7nrzplmfNXp/QF9tTmKRSgY3fXmu/7Ky2o=
|
||||||
|
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
github.com/emersion/go-message v0.18.0 h1:7LxAXHRpSeoO/Wom3ZApVZYG7c3d17yCScYce8WiXA8=
|
||||||
|
github.com/emersion/go-message v0.18.0/go.mod h1:Zi69ACvzaoV/MBnrxfVBPV3xWEuCmC2nEN39oJF4B8A=
|
||||||
|
github.com/emersion/go-sasl v0.0.0-20200509203442-7bfe0ed36a21/go.mod h1:iL2twTeMvZnrg54ZoPDNfJaJaqy0xIQFuBdrLsmspwQ=
|
||||||
|
github.com/emersion/go-sasl v0.0.0-20231106173351-e73c9f7bad43 h1:hH4PQfOndHDlpzYfLAAfl63E8Le6F2+EL/cdhlkyRJY=
|
||||||
|
github.com/emersion/go-sasl v0.0.0-20231106173351-e73c9f7bad43/go.mod h1:iL2twTeMvZnrg54ZoPDNfJaJaqy0xIQFuBdrLsmspwQ=
|
||||||
|
github.com/emersion/go-smtp v0.20.2 h1:peX42Qnh5Q0q3vrAnRy43R/JwTnnv75AebxbkTL7Ia4=
|
||||||
|
github.com/emersion/go-smtp v0.20.2/go.mod h1:qm27SGYgoIPRot6ubfQ/GpiPy/g3PaZAVRxiO/sDUgQ=
|
||||||
|
github.com/emersion/go-textwrapper v0.0.0-20200911093747-65d896831594 h1:IbFBtwoTQyw0fIM5xv1HF+Y+3ZijDR839WMulgxCcUY=
|
||||||
|
github.com/emersion/go-textwrapper v0.0.0-20200911093747-65d896831594/go.mod h1:aqO8z8wPrjkscevZJFVE1wXJrLpC5LtJG7fqLOsPb2U=
|
||||||
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
|
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
|
||||||
|
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
|
||||||
|
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
|
||||||
|
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||||
|
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
|
||||||
|
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
|
||||||
|
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
|
||||||
|
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||||
|
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
|
||||||
|
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
|
||||||
|
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
|
||||||
|
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
|
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
|
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
|
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||||
|
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||||
|
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
|
||||||
|
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
|
||||||
|
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||||
|
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||||
|
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
|
||||||
|
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
|
||||||
|
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
|
||||||
|
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||||
|
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||||
|
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
|
||||||
|
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
|
||||||
|
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||||
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
|
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
96
mail.go
Normal file
96
mail.go
Normal file
@ -0,0 +1,96 @@
|
|||||||
|
package simplemail
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"github.com/emersion/go-message/mail"
|
||||||
|
"github.com/emersion/go-sasl"
|
||||||
|
"github.com/emersion/go-smtp"
|
||||||
|
"io"
|
||||||
|
"net"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Mail struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
Tls bool `json:"tls"`
|
||||||
|
Server string `json:"server"`
|
||||||
|
From FromAddress `json:"from"`
|
||||||
|
Username string `json:"username"`
|
||||||
|
Password string `json:"password"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Mail) loginInfo() sasl.Client {
|
||||||
|
return sasl.NewPlainClient("", m.Username, m.Password)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Mail) mailCall(to []string, r io.Reader) error {
|
||||||
|
host, _, err := net.SplitHostPort(m.Server)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if m.Tls {
|
||||||
|
return smtp.SendMailTLS(m.Server, m.loginInfo(), m.From.String(), to, r)
|
||||||
|
}
|
||||||
|
if host == "localhost" || host == "127.0.0.1" {
|
||||||
|
// internals of smtp.SendMail without STARTTLS for localhost testing
|
||||||
|
dial, err := smtp.Dial(m.Server)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
err = dial.Auth(m.loginInfo())
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return dial.SendMail(m.From.String(), to, r)
|
||||||
|
}
|
||||||
|
return smtp.SendMail(m.Server, m.loginInfo(), m.From.String(), to, r)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Mail) SendMail(subject string, to []*mail.Address, htmlBody, textBody io.Reader) error {
|
||||||
|
// generate the email in this template
|
||||||
|
buf := new(bytes.Buffer)
|
||||||
|
|
||||||
|
// setup mail headers
|
||||||
|
var h mail.Header
|
||||||
|
h.SetDate(time.Now())
|
||||||
|
h.SetSubject(subject)
|
||||||
|
h.SetAddressList("From", []*mail.Address{m.From.Address})
|
||||||
|
h.SetAddressList("To", to)
|
||||||
|
h.Set("Content-Type", "multipart/alternative")
|
||||||
|
|
||||||
|
// setup html and text alternative headers
|
||||||
|
var hHtml, hTxt mail.InlineHeader
|
||||||
|
hHtml.Set("Content-Type", "text/html; charset=utf-8")
|
||||||
|
hTxt.Set("Content-Type", "text/plain; charset=utf-8")
|
||||||
|
|
||||||
|
createWriter, err := mail.CreateWriter(buf, h)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
inline, err := createWriter.CreateInline()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
partHtml, err := inline.CreatePart(hHtml)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if _, err := io.Copy(partHtml, htmlBody); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
partTxt, err := inline.CreatePart(hTxt)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if _, err := io.Copy(partTxt, textBody); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// convert all to addresses to strings
|
||||||
|
toStr := make([]string, len(to))
|
||||||
|
for i := range toStr {
|
||||||
|
toStr[i] = to[i].String()
|
||||||
|
}
|
||||||
|
|
||||||
|
return m.mailCall(toStr, buf)
|
||||||
|
}
|
60
simplemail.go
Normal file
60
simplemail.go
Normal file
@ -0,0 +1,60 @@
|
|||||||
|
package simplemail
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"errors"
|
||||||
|
"github.com/1f349/overlapfs"
|
||||||
|
"github.com/emersion/go-message/mail"
|
||||||
|
htmlTemplate "html/template"
|
||||||
|
"io"
|
||||||
|
"io/fs"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
textTemplate "text/template"
|
||||||
|
)
|
||||||
|
|
||||||
|
type SimpleMail struct {
|
||||||
|
mailSender *Mail
|
||||||
|
htmlTemplates *htmlTemplate.Template
|
||||||
|
textTemplates *textTemplate.Template
|
||||||
|
}
|
||||||
|
|
||||||
|
func New(sender *Mail, wd string, normal fs.FS) (simpleMail *SimpleMail, err error) {
|
||||||
|
m := &SimpleMail{mailSender: sender}
|
||||||
|
if wd != "" {
|
||||||
|
mailDir := filepath.Join(wd, "mail-templates")
|
||||||
|
err = os.Mkdir(mailDir, os.ModePerm)
|
||||||
|
if err != nil && !errors.Is(err, os.ErrExist) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
wdFs := os.DirFS(mailDir)
|
||||||
|
normal = overlapfs.OverlapFS{A: normal, B: wdFs}
|
||||||
|
}
|
||||||
|
m.htmlTemplates, err = htmlTemplate.New("mail").ParseFS(normal, "*.go.html")
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
m.textTemplates, err = textTemplate.New("mail").ParseFS(normal, "*.go.txt")
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
return m, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *SimpleMail) render(wrHtml, wrTxt io.Writer, name string, data any) {
|
||||||
|
err := m.htmlTemplates.ExecuteTemplate(wrHtml, name+".go.html", data)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Failed to render mail html: %s: %s\n", name, err)
|
||||||
|
}
|
||||||
|
err = m.textTemplates.ExecuteTemplate(wrTxt, name+".go.txt", data)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Failed to render mail text: %s: %s\n", name, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *SimpleMail) Send(templateName, subject string, to *mail.Address, data map[string]any) error {
|
||||||
|
var bufHtml, bufTxt bytes.Buffer
|
||||||
|
m.render(&bufHtml, &bufTxt, templateName, data)
|
||||||
|
return m.mailSender.SendMail(subject, []*mail.Address{to}, &bufHtml, &bufTxt)
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user