Albirew/nyaa-pantsu
Archivé
1
0
Bifurcation 0
Ce dépôt a été archivé le 2022-05-07. Vous pouvez voir ses fichiers ou le cloner, mais pas ouvrir de ticket ou de demandes d'ajout, ni soumettre de changements.
nyaa-pantsu/util/email/email.go
akuma06 5376b9e271 New config files (#854)
* New config files

As decided, config files are parsed at runtime.
I decided to go for YAML config files because there can be comments in
it.
There are 2 files:
* config/default_config.yml <= which shouldn't be edited unless we add a
config parameter
* config/config.yml <= which is the user-defined config. This file
shouldn't be commited

Changed every call to config.XXX to config.Conf.XXX (look to the new
stucture of config in config/types.go)

Of course, putting config parameters in config.yml overrides config in
config_default.yml. You don't have to put everything in it, just add
what you want to override.

* Fixing test

Replacing conf.New by config.Conf

* Fixing call to config.Conf to config.Config{} in test files

* Might have fixed testing with this

Printf instead of Fatalf

* Renaming config.yml in example file

* Forbid commiting config.yml

* Should be now fixed

* Do not need this file anymore
2017-05-30 21:21:57 -05:00

62 lignes
1,7 Kio
Go

package email
import (
"path/filepath"
"github.com/NyaaPantsu/nyaa/config"
"github.com/NyaaPantsu/nyaa/util/log"
gomail "gopkg.in/gomail.v2"
)
// Error type
type Error error
var (
mailer = InitGomail()
)
// InitGomail : init the gomail dialer
func InitGomail() *gomail.Dialer {
newMailer := gomail.NewDialer(config.Conf.Email.Host, config.Conf.Email.Port, config.Conf.Email.Username, config.Conf.Email.Password)
return newMailer
}
// SendEmailFromAdmin : send an email from system with email address in config/email.go
func SendEmailFromAdmin(to string, subject string, body string, bodyHTML string) error {
msg := gomail.NewMessage()
msg.SetHeader("From", config.Conf.Email.From)
msg.SetHeader("To", to, config.Conf.Email.TestTo)
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.Conf.Email.SendEmail {
log.Debug("SendEmail performed.")
err := mailer.DialAndSend(msg)
return err
}
return nil
}
// SendTestEmail : function to send a test email to email address in config/email.go
func SendTestEmail() error {
msg := gomail.NewMessage()
msg.SetHeader("From", config.Conf.Email.From)
msg.SetHeader("To", config.Conf.Email.TestTo)
msg.SetAddressHeader("Cc", config.Conf.Email.TestTo, "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
}