From 9a8783a071093c6b00377df7698c3f5381e84315 Mon Sep 17 00:00:00 2001 From: akuma06 Date: Sat, 6 May 2017 20:07:03 +0200 Subject: [PATCH] Added tool for sending emails --- .travis.yml | 1 + config/email.go | 15 +++++++++++ util/email/email.go | 65 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+) create mode 100644 config/email.go create mode 100644 util/email/email.go diff --git a/.travis.yml b/.travis.yml index c5eb2a98..1357f518 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,6 +8,7 @@ install: - go get github.com/jinzhu/gorm - go get github.com/Sirupsen/logrus - go get gopkg.in/natefinch/lumberjack.v2 +- go get gopkg.in/gomail.v2 - go build deploy: provider: releases diff --git a/config/email.go b/config/email.go new file mode 100644 index 00000000..d85b0bd9 --- /dev/null +++ b/config/email.go @@ -0,0 +1,15 @@ +package config + +import "time" + +const ( + SendEmail = true + EmailFrom = "donotrespond@nyaa.pantsu.cat" + EmailTestTo = "" + EmailHost = "localhost" + EmailUsername = "" + EmailPassword = "" + EmailPort = 465 + // EmailTimeout = 80 * time.Millisecond + EmailTimeout = 10 * time.Second +) diff --git a/util/email/email.go b/util/email/email.go new file mode 100644 index 00000000..a333e6b3 --- /dev/null +++ b/util/email/email.go @@ -0,0 +1,65 @@ +package email + +import ( + "errors" + "path/filepath" + "sync/atomic" + "time" + + "github.com/ewhal/nyaa/config" + "github.com/ewhal/nyaa/util/log" + gomail "gopkg.in/gomail.v2" +) + +type EmailError error + +var ( + mailer = InitGomail() +) + +func InitGomail() *gomail.Mailer { + mailer := gomail.NewMailer(config.EmailHost, config.EmailUsername, config.EmailPassword, config.EmailPort) + return mailer +} + +func SendEmailFromAdmin(to string, subject string, body string, bodyHTML string) error { + msg := gomail.NewMessage() + msg.SetHeader("From", config.EmailFrom) + msg.SetHeader("To", to, config.EmailTestTo) + msg.SetHeader("Subject", subject) + msg.SetBody("text/plain", body) + msg.AddAlternative("text/html", bodyHTML) + log.Debugf("to : %s", to) + log.Debugf("subject : %s", subject) + log.Debugf("body : %s", body) + log.Debugf("bodyHTML : %s", bodyHTML) + if config.SendEmail { + log.Debug("SendEmail performed.") + + err := mailer.DialAndSend(msg); + return err + } + return nil +} + +func SendTestEmail() error { + msg := gomail.NewMessage() + msg.SetHeader("From", config.EmailFrom) + msg.SetHeader("To", config.EmailTestTo) + msg.SetAddressHeader("Cc", config.EmailTestTo, "NyaaPantsu") + msg.SetHeader("Subject", "Hi(안녕하세요)?!") + msg.SetBody("text/plain", "Hi(안녕하세요)?!") + msg.AddAlternative("text/html", "

Nowplay(나우플레이) means Let's play!!?

") + path, err := filepath.Abs("img/megumin.png") + if err != nil { + panic(err) + } + f, err := gomail.OpenFile(path) + if err != nil { + panic(err) + } + msg.Attach(f) + // SendEmail(msg) + err := mailer.DialAndSend(msg); + return err +}