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/utils/email/email.go

63 lignes
1,7 Kio
Go
Brut Vue normale Historique

2017-05-06 20:07:03 +02:00
package email
import (
"path/filepath"
2017-05-17 07:58:40 +02:00
"github.com/NyaaPantsu/nyaa/config"
"github.com/NyaaPantsu/nyaa/utils/log"
"gopkg.in/gomail.v2"
2017-05-06 20:07:03 +02:00
)
// Error type
type Error error
2017-05-06 20:07:03 +02:00
var (
2017-05-06 22:14:02 +02:00
mailer = InitGomail()
2017-05-06 20:07:03 +02:00
)
// InitGomail : init the gomail dialer
2017-05-06 22:14:02 +02:00
func InitGomail() *gomail.Dialer {
newMailer := gomail.NewDialer(config.Get().Email.Host, config.Get().Email.Port, config.Get().Email.Username, config.Get().Email.Password)
return newMailer
2017-05-06 20:07:03 +02:00
}
// SendEmailFromAdmin : send an email from system with email address in config/email.go
2017-05-06 20:07:03 +02:00
func SendEmailFromAdmin(to string, subject string, body string, bodyHTML string) error {
msg := gomail.NewMessage()
msg.SetHeader("From", config.Get().Email.From)
msg.SetHeader("To", to, config.Get().Email.TestTo)
2017-05-06 20:07:03 +02:00
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.Get().Email.SendEmail {
2017-05-06 20:07:03 +02:00
log.Debug("SendEmail performed.")
err := mailer.DialAndSend(msg)
2017-05-06 20:07:03 +02:00
return err
}
return nil
}
// SendTestEmail : function to send a test email to email address in config/email.go
2017-05-06 20:07:03 +02:00
func SendTestEmail() error {
msg := gomail.NewMessage()
msg.SetHeader("From", config.Get().Email.From)
msg.SetHeader("To", config.Get().Email.TestTo)
msg.SetAddressHeader("Cc", config.Get().Email.TestTo, "NyaaPantsu")
2017-05-06 20:07:03 +02:00
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)
}
2017-05-06 22:14:02 +02:00
msg.Attach(path)
2017-05-06 21:56:52 +02:00
err = mailer.DialAndSend(msg)
2017-05-06 20:07:03 +02:00
return err
}