c9b72206a5
* Checkpoint: it builds The config, db, model, network, os, and public packages have had some fixes to glaringly obvious flaws, dead code removed, and stylistic changes. * Style changes and old code removal in router Router needs a lot of work done to its (lack of) error handling. * Dead code removal and style changes Now up to util/email/email.go. After I'm finished with the initial sweep I'll go back and fix error handling and security issues. Then I'll fix the broken API. Then I'll go through to add documentation and fix code visibility. * Finish dead code removal and style changes Vendored libraries not touched. Everything still needs security fixes and documentation. There's also one case of broken functionality. * Fix accidental find-and-replace * Style, error checking, saftey, bug fix changes * Redo error checking erased during merge * Re-add merge-erased fix. Make Safe safe.
58 lignes
1,4 Kio
Go
58 lignes
1,4 Kio
Go
package email
|
|
|
|
import (
|
|
"path/filepath"
|
|
|
|
"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.Dialer {
|
|
newMailer := gomail.NewDialer(config.EmailHost, config.EmailPort, config.EmailUsername, config.EmailPassword)
|
|
return newMailer
|
|
}
|
|
|
|
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", "<p><b>Nowplay(나우플레이)</b> means <i>Let's play</i>!!?</p>")
|
|
path, err := filepath.Abs("img/megumin.png")
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
msg.Attach(path)
|
|
|
|
err = mailer.DialAndSend(msg)
|
|
return err
|
|
}
|