From dd652ab74d207fb797b15f57e14b28806030ddb9 Mon Sep 17 00:00:00 2001 From: Darko Luketic Date: Fri, 25 Oct 2019 20:06:51 +0200 Subject: [PATCH] initial --- go.mod | 5 +++++ go.sum | 2 ++ mailer.go | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+) create mode 100644 go.mod create mode 100644 go.sum create mode 100644 mailer.go diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..8514b77 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module git.icod.de/essentials/mailer + +go 1.13 + +require github.com/go-mail/mail v2.3.1+incompatible diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..79c887a --- /dev/null +++ b/go.sum @@ -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= diff --git a/mailer.go b/mailer.go new file mode 100644 index 0000000..fb6e75a --- /dev/null +++ b/mailer.go @@ -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) +}