This commit is contained in:
Darko Luketic 2019-10-25 20:06:51 +02:00
commit dd652ab74d
3 changed files with 45 additions and 0 deletions

5
go.mod Normal file
View File

@ -0,0 +1,5 @@
module git.icod.de/essentials/mailer
go 1.13
require github.com/go-mail/mail v2.3.1+incompatible

2
go.sum Normal file
View File

@ -0,0 +1,2 @@
github.com/go-mail/mail v2.3.1+incompatible h1:UzNOn0k5lpfVtO31cK3hn6I4VEVGhe3lX8AJBAxXExM=
github.com/go-mail/mail v2.3.1+incompatible/go.mod h1:VPWjmmNyRsWXQZHVHT3g0YbIINUkSmuKOiLIDkWbL6M=

38
mailer.go Normal file
View File

@ -0,0 +1,38 @@
package mailer
import (
"crypto/tls"
"github.com/go-mail/mail"
)
type Options struct {
From string
Host string
Port int
Username string
Password string
}
type Mailer struct {
dialer *mail.Dialer
options *Options
}
func New(options *Options) *Mailer {
dialer := mail.NewDialer(options.Host, options.Port, options.Username, options.Password)
dialer.TLSConfig = &tls.Config{InsecureSkipVerify: true}
return &Mailer{
dialer: dialer,
options: options,
}
}
func (mailer *Mailer) Send(to, subject, text string) error {
m := mail.NewMessage()
m.SetHeader("From", mailer.options.From)
m.SetHeader("To", to)
m.SetHeader("Subject", subject)
m.SetBody("text/plain", text)
return mailer.dialer.DialAndSend(m)
}